blob: 19b4479b682f214f652fd8a22275ab1bc137395e [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
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200128/* When using exists() don't auto-load a script. */
129static int no_autoload = FALSE;
130
Bram Moolenaar532c7802005-01-27 14:44:31 +0000131/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 * When recursively copying lists and dicts we need to remember which ones we
133 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135 */
136static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000137#define COPYID_INC 2
138#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139
140/*
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 */
159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
161 * Structure to hold info for a user function.
162 */
163typedef struct ufunc ufunc_T;
164
165struct ufunc
166{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000167 int uf_varargs; /* variable nr of arguments */
168 int uf_flags;
169 int uf_calls; /* nr of active calls */
170 garray_T uf_args; /* arguments */
171 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000172#ifdef FEAT_PROFILE
173 int uf_profiling; /* TRUE when func is being profiled */
174 /* profiling the function as a whole */
175 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000176 proftime_T uf_tm_total; /* time spent in function + children */
177 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000178 proftime_T uf_tm_children; /* time spent in children this call */
179 /* profiling the function per line */
180 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T *uf_tml_total; /* time spent in a line + children */
182 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tml_start; /* start time for current line */
184 proftime_T uf_tml_children; /* time spent in children for this line */
185 proftime_T uf_tml_wait; /* start wait time for current line */
186 int uf_tml_idx; /* index of line being timed; -1 if none */
187 int uf_tml_execed; /* line being timed was executed */
188#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 int uf_refcount; /* for numbered function: reference count */
192 char_u uf_name[1]; /* name of function (actually longer); can
193 start with <SNR>123_ (<SNR> is K_SPECIAL
194 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195};
196
197/* function flags */
198#define FC_ABORT 1 /* abort function on error */
199#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000200#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
202/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000203 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000205static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000207/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000208static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
209
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000210/* list heads for garbage collection */
211static dict_T *first_dict = NULL; /* list of all dicts */
212static list_T *first_list = NULL; /* list of all lists */
213
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000214/* From user function to hashitem and back. */
215static ufunc_T dumuf;
216#define UF2HIKEY(fp) ((fp)->uf_name)
217#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
218#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
219
220#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
221#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
Bram Moolenaar33570922005-01-25 22:26:29 +0000223#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
224#define VAR_SHORT_LEN 20 /* short variable name length */
225#define FIXVAR_CNT 12 /* number of fixed variables */
226
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000228typedef struct funccall_S funccall_T;
229
230struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231{
232 ufunc_T *func; /* function being called */
233 int linenr; /* next line to be executed */
234 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000235 struct /* fixed variables for arguments */
236 {
237 dictitem_T var; /* variable (without room for name) */
238 char_u room[VAR_SHORT_LEN]; /* room for the name */
239 } fixvar[FIXVAR_CNT];
240 dict_T l_vars; /* l: local function variables */
241 dictitem_T l_vars_var; /* variable for l: scope */
242 dict_T l_avars; /* a: argument variables */
243 dictitem_T l_avars_var; /* variable for a: scope */
244 list_T l_varlist; /* list for a:000 */
245 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
246 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 linenr_T breakpoint; /* next line with breakpoint or zero */
248 int dbg_tick; /* debug_tick when breakpoint was set */
249 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000250#ifdef FEAT_PROFILE
251 proftime_T prof_child; /* time spent in a child */
252#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000253 funccall_T *caller; /* calling function or NULL */
254};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257 * Info used by a ":for" loop.
258 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000259typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000260{
261 int fi_semicolon; /* TRUE if ending in '; var]' */
262 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 listwatch_T fi_lw; /* keep an eye on the item used. */
264 list_T *fi_list; /* list being used */
265} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000266
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000268 * Struct used by trans_function_name()
269 */
270typedef struct
271{
Bram Moolenaar33570922005-01-25 22:26:29 +0000272 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000273 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dictitem_T *fd_di; /* Dictionary item used */
275} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000276
Bram Moolenaara7043832005-01-21 11:56:39 +0000277
278/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 * Array to hold the value of v: variables.
280 * The value is in a dictitem, so that it can also be used in the v: scope.
281 * The reason to use this table anyway is for very quick access to the
282 * variables with the VV_ defines.
283 */
284#include "version.h"
285
286/* values for vv_flags: */
287#define VV_COMPAT 1 /* compatible, also used without "v:" */
288#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000289#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000290
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000291#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
293static struct vimvar
294{
295 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000296 dictitem_T vv_di; /* value and name for key */
297 char vv_filler[16]; /* space for LONGEST name below!!! */
298 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
299} vimvars[VV_LEN] =
300{
301 /*
302 * The order here must match the VV_ defines in vim.h!
303 * Initializing a union does not work, leave tv.vval empty to get zero's.
304 */
305 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
306 {VV_NAME("count1", VAR_NUMBER), VV_RO},
307 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
308 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
309 {VV_NAME("warningmsg", VAR_STRING), 0},
310 {VV_NAME("statusmsg", VAR_STRING), 0},
311 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
313 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
315 {VV_NAME("termresponse", VAR_STRING), VV_RO},
316 {VV_NAME("fname", VAR_STRING), VV_RO},
317 {VV_NAME("lang", VAR_STRING), VV_RO},
318 {VV_NAME("lc_time", VAR_STRING), VV_RO},
319 {VV_NAME("ctype", VAR_STRING), VV_RO},
320 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
321 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
322 {VV_NAME("fname_in", VAR_STRING), VV_RO},
323 {VV_NAME("fname_out", VAR_STRING), VV_RO},
324 {VV_NAME("fname_new", VAR_STRING), VV_RO},
325 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
326 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
327 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
328 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
330 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("progname", VAR_STRING), VV_RO},
332 {VV_NAME("servername", VAR_STRING), VV_RO},
333 {VV_NAME("dying", VAR_NUMBER), VV_RO},
334 {VV_NAME("exception", VAR_STRING), VV_RO},
335 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
336 {VV_NAME("register", VAR_STRING), VV_RO},
337 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
338 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000339 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
340 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000341 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000342 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
343 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000344 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
345 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000349 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000350 {VV_NAME("swapname", VAR_STRING), VV_RO},
351 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000352 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200353 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000354 {VV_NAME("mouse_win", VAR_NUMBER), 0},
355 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
356 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000357 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000358 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000359 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200360 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000361};
362
363/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364#define vv_type vv_di.di_tv.v_type
365#define vv_nr vv_di.di_tv.vval.v_number
366#define vv_float vv_di.di_tv.vval.v_float
367#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000368#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000369#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000370
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200371static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000372#define vimvarht vimvardict.dv_hashtab
373
Bram Moolenaara40058a2005-07-11 22:42:07 +0000374static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
375static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000376static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
377static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
378static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000379static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
380static void list_glob_vars __ARGS((int *first));
381static void list_buf_vars __ARGS((int *first));
382static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000383#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000384static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000385#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_vim_vars __ARGS((int *first));
387static void list_script_vars __ARGS((int *first));
388static void list_func_vars __ARGS((int *first));
389static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000390static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
391static int check_changedtick __ARGS((char_u *arg));
392static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
393static void clear_lval __ARGS((lval_T *lp));
394static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
395static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000396static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
397static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
398static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
399static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
400static void item_lock __ARGS((typval_T *tv, int deep, int lock));
401static int tv_islocked __ARGS((typval_T *tv));
402
Bram Moolenaar33570922005-01-25 22:26:29 +0000403static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
404static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000409static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
410static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000411
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000412static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000417static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100419static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
420static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
421static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000422static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000424static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
426static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000427static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000428static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100429static 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 +0000430static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000431static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200432static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
434static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000435static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000437static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000439static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
440static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000442#ifdef FEAT_FLOAT
443static int string2float __ARGS((char_u *text, float_T *value));
444#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
446static int find_internal_func __ARGS((char_u *name));
447static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
448static 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 +0200449static 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 +0000450static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000451static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200455static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100458static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200464static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
480static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
481#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000482static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000485static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000487#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000488static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000489static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
491#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#ifdef FEAT_FLOAT
495static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200496static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
501static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200512#ifdef FEAT_FLOAT
513static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000517static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523#ifdef FEAT_FLOAT
524static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200526static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000527#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000528static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000537static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000539static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000545static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000553static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000554static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000555static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000556static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200559static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000560static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000568static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000582static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100587static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000589static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000601#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200602static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000603static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
604#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200605#ifdef FEAT_LUA
606static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
607#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000612static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000613static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000614static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000616static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000617static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000620#ifdef vim_mkdir
621static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
622#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100624#ifdef FEAT_MZSCHEME
625static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
626#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100629static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000630static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000631#ifdef FEAT_FLOAT
632static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000635static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000636static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200637#ifdef FEAT_PYTHON3
638static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
640#ifdef FEAT_PYTHON
641static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000644static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000645static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#ifdef FEAT_FLOAT
658static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
659#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200660static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100662static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000665static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000667static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000674static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000675static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000676static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000677static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200679static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000680static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100682#ifdef FEAT_CRYPT
683static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
684#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000685static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200686static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000688#ifdef FEAT_FLOAT
689static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200690static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000693static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000694static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#ifdef FEAT_FLOAT
698static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
699static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
700#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000701static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200702static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703#ifdef HAVE_STRFTIME
704static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
705#endif
706static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200712static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200713static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000719static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200720static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000722static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000723static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000724static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000725static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000726static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000728static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200729#ifdef FEAT_FLOAT
730static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
732#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000736#ifdef FEAT_FLOAT
737static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200740static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200741static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100745static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000752static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000755static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100756static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000757
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000758static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000759static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760static int get_env_len __ARGS((char_u **arg));
761static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000762static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000763static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
764#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
765#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
766 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000767static 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 +0000768static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000769static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000770static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
771static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772static typval_T *alloc_tv __ARGS((void));
773static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static void init_tv __ARGS((typval_T *varp));
775static long get_tv_number __ARGS((typval_T *varp));
776static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000777static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static char_u *get_tv_string __ARGS((typval_T *varp));
779static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000780static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200782static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
784static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
785static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000786static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
787static 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 +0000788static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
789static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000790static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200791static int var_check_func_name __ARGS((char_u *name, int new_var));
792static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000793static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000794static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
796static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
797static int eval_fname_script __ARGS((char_u *p));
798static int eval_fname_sid __ARGS((char_u *p));
799static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static ufunc_T *find_func __ARGS((char_u *name));
801static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000802static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803#ifdef FEAT_PROFILE
804static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000805static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
806static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
807static int
808# ifdef __BORLANDC__
809 _RTLENTRYF
810# endif
811 prof_total_cmp __ARGS((const void *s1, const void *s2));
812static int
813# ifdef __BORLANDC__
814 _RTLENTRYF
815# endif
816 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200818static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000819static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000820static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000821static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static 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 +0000823static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
824static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000825static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000826static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
827static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000828static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000829static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000830static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200832
833#ifdef EBCDIC
834static int compare_func_name __ARGS((const void *s1, const void *s2));
835static void sortFunctions __ARGS(());
836#endif
837
Bram Moolenaar33570922005-01-25 22:26:29 +0000838/*
839 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000840 */
841 void
842eval_init()
843{
Bram Moolenaar33570922005-01-25 22:26:29 +0000844 int i;
845 struct vimvar *p;
846
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200847 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
848 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200849 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000850 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000851 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000852
853 for (i = 0; i < VV_LEN; ++i)
854 {
855 p = &vimvars[i];
856 STRCPY(p->vv_di.di_key, p->vv_name);
857 if (p->vv_flags & VV_RO)
858 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
859 else if (p->vv_flags & VV_RO_SBX)
860 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
861 else
862 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000863
864 /* add to v: scope dict, unless the value is not always available */
865 if (p->vv_type != VAR_UNKNOWN)
866 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000868 /* add to compat scope dict */
869 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000871 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200872 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200873
874#ifdef EBCDIC
875 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100876 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200877 */
878 sortFunctions();
879#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000880}
881
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000882#if defined(EXITFREE) || defined(PROTO)
883 void
884eval_clear()
885{
886 int i;
887 struct vimvar *p;
888
889 for (i = 0; i < VV_LEN; ++i)
890 {
891 p = &vimvars[i];
892 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000893 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000894 vim_free(p->vv_str);
895 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000896 }
897 else if (p->vv_di.di_tv.v_type == VAR_LIST)
898 {
899 list_unref(p->vv_list);
900 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000901 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000902 }
903 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000904 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000905 hash_clear(&compat_hashtab);
906
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100908# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200909 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100910# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911
912 /* global variables */
913 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000915 /* autoloaded script names */
916 ga_clear_strings(&ga_loaded);
917
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200918 /* script-local variables */
919 for (i = 1; i <= ga_scripts.ga_len; ++i)
920 {
921 vars_clear(&SCRIPT_VARS(i));
922 vim_free(SCRIPT_SV(i));
923 }
924 ga_clear(&ga_scripts);
925
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000926 /* unreferenced lists and dicts */
927 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000928
929 /* functions */
930 free_all_functions();
931 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000932}
933#endif
934
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 * Return the name of the executed function.
937 */
938 char_u *
939func_name(cookie)
940 void *cookie;
941{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000942 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943}
944
945/*
946 * Return the address holding the next breakpoint line for a funccall cookie.
947 */
948 linenr_T *
949func_breakpoint(cookie)
950 void *cookie;
951{
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the address holding the debug tick for a funccall cookie.
957 */
958 int *
959func_dbg_tick(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the nesting level for a funccall cookie.
967 */
968 int
969func_level(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000976funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000978/* pointer to list of previously used funccal, still around because some
979 * item in it is still being used. */
980funccall_T *previous_funccal = NULL;
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Return TRUE when a function was ended by a ":return" command.
984 */
985 int
986current_func_returned()
987{
988 return current_funccal->returned;
989}
990
991
992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 * Set an internal variable to a string value. Creates the variable if it does
994 * not already exist.
995 */
996 void
997set_internal_string_var(name, value)
998 char_u *name;
999 char_u *value;
1000{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001001 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001002 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004 val = vim_strsave(value);
1005 if (val != NULL)
1006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001007 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001010 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 }
1014}
1015
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001017static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018static char_u *redir_endp = NULL;
1019static char_u *redir_varname = NULL;
1020
1021/*
1022 * Start recording command output to a variable
1023 * Returns OK if successfully completed the setup. FAIL otherwise.
1024 */
1025 int
1026var_redir_start(name, append)
1027 char_u *name;
1028 int append; /* append to an existing variable */
1029{
1030 int save_emsg;
1031 int err;
1032 typval_T tv;
1033
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001034 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001035 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 {
1037 EMSG(_(e_invarg));
1038 return FAIL;
1039 }
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042 redir_varname = vim_strsave(name);
1043 if (redir_varname == NULL)
1044 return FAIL;
1045
1046 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1047 if (redir_lval == NULL)
1048 {
1049 var_redir_stop();
1050 return FAIL;
1051 }
1052
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001053 /* The output is stored in growarray "redir_ga" until redirection ends. */
1054 ga_init2(&redir_ga, (int)sizeof(char), 500);
1055
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001057 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1058 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1060 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001061 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp != NULL && *redir_endp != NUL)
1063 /* Trailing characters are present after the variable name */
1064 EMSG(_(e_trailing));
1065 else
1066 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001067 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068 var_redir_stop();
1069 return FAIL;
1070 }
1071
1072 /* check if we can write to the variable: set it to or append an empty
1073 * string */
1074 save_emsg = did_emsg;
1075 did_emsg = FALSE;
1076 tv.v_type = VAR_STRING;
1077 tv.vval.v_string = (char_u *)"";
1078 if (append)
1079 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1080 else
1081 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001082 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001084 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 if (err)
1086 {
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 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096 * Append "value[value_len]" to the variable set by var_redir_start().
1097 * The actual appending is postponed until redirection ends, because the value
1098 * appended may in fact be the string we write to, changing it may cause freed
1099 * memory to be used:
1100 * :redir => foo
1101 * :let foo
1102 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 */
1104 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001109 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110
1111 if (redir_lval == NULL)
1112 return;
1113
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 {
1121 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 }
1124 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126}
1127
1128/*
1129 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001130 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 */
1132 void
1133var_redir_stop()
1134{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 typval_T tv;
1136
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_lval != NULL)
1138 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001139 /* If there was no error: assign the text to the variable. */
1140 if (redir_endp != NULL)
1141 {
1142 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1143 tv.v_type = VAR_STRING;
1144 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001145 /* Call get_lval() again, if it's inside a Dict or List it may
1146 * have changed. */
1147 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1148 FALSE, FALSE, FALSE, FNE_CHECK_START);
1149 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1150 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1151 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* free the collected output */
1155 vim_free(redir_ga.ga_data);
1156 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 vim_free(redir_lval);
1159 redir_lval = NULL;
1160 }
1161 vim_free(redir_varname);
1162 redir_varname = NULL;
1163}
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165# if defined(FEAT_MBYTE) || defined(PROTO)
1166 int
1167eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1168 char_u *enc_from;
1169 char_u *enc_to;
1170 char_u *fname_from;
1171 char_u *fname_to;
1172{
1173 int err = FALSE;
1174
1175 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1176 set_vim_var_string(VV_CC_TO, enc_to, -1);
1177 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1178 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1179 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1180 err = TRUE;
1181 set_vim_var_string(VV_CC_FROM, NULL, -1);
1182 set_vim_var_string(VV_CC_TO, NULL, -1);
1183 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1184 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1185
1186 if (err)
1187 return FAIL;
1188 return OK;
1189}
1190# endif
1191
1192# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1193 int
1194eval_printexpr(fname, args)
1195 char_u *fname;
1196 char_u *args;
1197{
1198 int err = FALSE;
1199
1200 set_vim_var_string(VV_FNAME_IN, fname, -1);
1201 set_vim_var_string(VV_CMDARG, args, -1);
1202 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1203 err = TRUE;
1204 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1205 set_vim_var_string(VV_CMDARG, NULL, -1);
1206
1207 if (err)
1208 {
1209 mch_remove(fname);
1210 return FAIL;
1211 }
1212 return OK;
1213}
1214# endif
1215
1216# if defined(FEAT_DIFF) || defined(PROTO)
1217 void
1218eval_diff(origfile, newfile, outfile)
1219 char_u *origfile;
1220 char_u *newfile;
1221 char_u *outfile;
1222{
1223 int err = FALSE;
1224
1225 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1226 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1227 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1228 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1229 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1230 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1231 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1232}
1233
1234 void
1235eval_patch(origfile, difffile, outfile)
1236 char_u *origfile;
1237 char_u *difffile;
1238 char_u *outfile;
1239{
1240 int err;
1241
1242 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1243 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1244 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1245 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1246 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1248 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1249}
1250# endif
1251
1252/*
1253 * Top level evaluation function, returning a boolean.
1254 * Sets "error" to TRUE if there was an error.
1255 * Return TRUE or FALSE.
1256 */
1257 int
1258eval_to_bool(arg, error, nextcmd, skip)
1259 char_u *arg;
1260 int *error;
1261 char_u **nextcmd;
1262 int skip; /* only parse, don't execute */
1263{
Bram Moolenaar33570922005-01-25 22:26:29 +00001264 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 int retval = FALSE;
1266
1267 if (skip)
1268 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001269 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else
1272 {
1273 *error = FALSE;
1274 if (!skip)
1275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001276 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 }
1280 if (skip)
1281 --emsg_skip;
1282
1283 return retval;
1284}
1285
1286/*
1287 * Top level evaluation function, returning a string. If "skip" is TRUE,
1288 * only parsing to "nextcmd" is done, without reporting errors. Return
1289 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1290 */
1291 char_u *
1292eval_to_string_skip(arg, nextcmd, skip)
1293 char_u *arg;
1294 char_u **nextcmd;
1295 int skip; /* only parse, don't execute */
1296{
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 char_u *retval;
1299
1300 if (skip)
1301 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 retval = NULL;
1304 else
1305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 retval = vim_strsave(get_tv_string(&tv));
1307 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309 if (skip)
1310 --emsg_skip;
1311
1312 return retval;
1313}
1314
1315/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001316 * Skip over an expression at "*pp".
1317 * Return FAIL for an error, OK otherwise.
1318 */
1319 int
1320skip_expr(pp)
1321 char_u **pp;
1322{
Bram Moolenaar33570922005-01-25 22:26:29 +00001323 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001324
1325 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327}
1328
1329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001331 * When "convert" is TRUE convert a List into a sequence of lines and convert
1332 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Return pointer to allocated memory, or NULL for failure.
1334 */
1335 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 char_u *arg;
1338 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001343 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001344#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001345 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 retval = NULL;
1350 else
1351 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001353 {
1354 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001355 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001356 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001357 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001358 if (tv.vval.v_list->lv_len > 0)
1359 ga_append(&ga, NL);
1360 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001361 ga_append(&ga, NUL);
1362 retval = (char_u *)ga.ga_data;
1363 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364#ifdef FEAT_FLOAT
1365 else if (convert && tv.v_type == VAR_FLOAT)
1366 {
1367 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1368 retval = vim_strsave(numbuf);
1369 }
1370#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 else
1372 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375
1376 return retval;
1377}
1378
1379/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001380 * Call eval_to_string() without using current local variables and using
1381 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 */
1383 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001384eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 char_u *arg;
1386 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 char_u *retval;
1390 void *save_funccalp;
1391
1392 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 if (use_sandbox)
1394 ++sandbox;
1395 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001396 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 if (use_sandbox)
1398 --sandbox;
1399 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 restore_funccal(save_funccalp);
1401 return retval;
1402}
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404/*
1405 * Top level evaluation function, returning a number.
1406 * Evaluates "expr" silently.
1407 * Returns -1 for an error.
1408 */
1409 int
1410eval_to_number(expr)
1411 char_u *expr;
1412{
Bram Moolenaar33570922005-01-25 22:26:29 +00001413 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001415 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
1417 ++emsg_off;
1418
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 retval = -1;
1421 else
1422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001423 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426 --emsg_off;
1427
1428 return retval;
1429}
1430
Bram Moolenaara40058a2005-07-11 22:42:07 +00001431/*
1432 * Prepare v: variable "idx" to be used.
1433 * Save the current typeval in "save_tv".
1434 * When not used yet add the variable to the v: hashtable.
1435 */
1436 static void
1437prepare_vimvar(idx, save_tv)
1438 int idx;
1439 typval_T *save_tv;
1440{
1441 *save_tv = vimvars[idx].vv_tv;
1442 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1443 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1444}
1445
1446/*
1447 * Restore v: variable "idx" to typeval "save_tv".
1448 * When no longer defined, remove the variable from the v: hashtable.
1449 */
1450 static void
1451restore_vimvar(idx, save_tv)
1452 int idx;
1453 typval_T *save_tv;
1454{
1455 hashitem_T *hi;
1456
Bram Moolenaara40058a2005-07-11 22:42:07 +00001457 vimvars[idx].vv_tv = *save_tv;
1458 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1459 {
1460 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1461 if (HASHITEM_EMPTY(hi))
1462 EMSG2(_(e_intern2), "restore_vimvar()");
1463 else
1464 hash_remove(&vimvarht, hi);
1465 }
1466}
1467
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001468#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001469/*
1470 * Evaluate an expression to a list with suggestions.
1471 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001472 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001473 */
1474 list_T *
1475eval_spell_expr(badword, expr)
1476 char_u *badword;
1477 char_u *expr;
1478{
1479 typval_T save_val;
1480 typval_T rettv;
1481 list_T *list = NULL;
1482 char_u *p = skipwhite(expr);
1483
1484 /* Set "v:val" to the bad word. */
1485 prepare_vimvar(VV_VAL, &save_val);
1486 vimvars[VV_VAL].vv_type = VAR_STRING;
1487 vimvars[VV_VAL].vv_str = badword;
1488 if (p_verbose == 0)
1489 ++emsg_off;
1490
1491 if (eval1(&p, &rettv, TRUE) == OK)
1492 {
1493 if (rettv.v_type != VAR_LIST)
1494 clear_tv(&rettv);
1495 else
1496 list = rettv.vval.v_list;
1497 }
1498
1499 if (p_verbose == 0)
1500 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001501 restore_vimvar(VV_VAL, &save_val);
1502
1503 return list;
1504}
1505
1506/*
1507 * "list" is supposed to contain two items: a word and a number. Return the
1508 * word in "pp" and the number as the return value.
1509 * Return -1 if anything isn't right.
1510 * Used to get the good word and score from the eval_spell_expr() result.
1511 */
1512 int
1513get_spellword(list, pp)
1514 list_T *list;
1515 char_u **pp;
1516{
1517 listitem_T *li;
1518
1519 li = list->lv_first;
1520 if (li == NULL)
1521 return -1;
1522 *pp = get_tv_string(&li->li_tv);
1523
1524 li = li->li_next;
1525 if (li == NULL)
1526 return -1;
1527 return get_tv_number(&li->li_tv);
1528}
1529#endif
1530
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001531/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001532 * Top level evaluation function.
1533 * Returns an allocated typval_T with the result.
1534 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001535 */
1536 typval_T *
1537eval_expr(arg, nextcmd)
1538 char_u *arg;
1539 char_u **nextcmd;
1540{
1541 typval_T *tv;
1542
1543 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001544 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001545 {
1546 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001547 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 }
1549
1550 return tv;
1551}
1552
1553
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001555 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001556 * Uses argv[argc] for the function arguments. Only Number and String
1557 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001560 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001561call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 char_u *func;
1563 int argc;
1564 char_u **argv;
1565 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001566 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Bram Moolenaar33570922005-01-25 22:26:29 +00001569 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 long n;
1571 int len;
1572 int i;
1573 int doesrange;
1574 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001577 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001579 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581 for (i = 0; i < argc; i++)
1582 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001583 /* Pass a NULL or empty argument as an empty string */
1584 if (argv[i] == NULL || *argv[i] == NUL)
1585 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001586 argvars[i].v_type = VAR_STRING;
1587 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001588 continue;
1589 }
1590
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001591 if (str_arg_only)
1592 len = 0;
1593 else
1594 /* Recognize a number argument, the others must be strings. */
1595 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (len != 0 && len == (int)STRLEN(argv[i]))
1597 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001598 argvars[i].v_type = VAR_NUMBER;
1599 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 else
1602 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001603 argvars[i].v_type = VAR_STRING;
1604 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606 }
1607
1608 if (safe)
1609 {
1610 save_funccalp = save_funccal();
1611 ++sandbox;
1612 }
1613
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1615 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 if (safe)
1619 {
1620 --sandbox;
1621 restore_funccal(save_funccalp);
1622 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 vim_free(argvars);
1624
1625 if (ret == FAIL)
1626 clear_tv(rettv);
1627
1628 return ret;
1629}
1630
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001631/*
1632 * Call vimL function "func" and return the result as a number.
1633 * Returns -1 when calling the function fails.
1634 * Uses argv[argc] for the function arguments.
1635 */
1636 long
1637call_func_retnr(func, argc, argv, safe)
1638 char_u *func;
1639 int argc;
1640 char_u **argv;
1641 int safe; /* use the sandbox */
1642{
1643 typval_T rettv;
1644 long retval;
1645
1646 /* All arguments are passed as strings, no conversion to number. */
1647 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1648 return -1;
1649
1650 retval = get_tv_number_chk(&rettv, NULL);
1651 clear_tv(&rettv);
1652 return retval;
1653}
1654
1655#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1656 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1657
Bram Moolenaar4f688582007-07-24 12:34:30 +00001658# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001660 * Call vimL function "func" and return the result as a string.
1661 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001662 * Uses argv[argc] for the function arguments.
1663 */
1664 void *
1665call_func_retstr(func, argc, argv, safe)
1666 char_u *func;
1667 int argc;
1668 char_u **argv;
1669 int safe; /* use the sandbox */
1670{
1671 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001672 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001674 /* All arguments are passed as strings, no conversion to number. */
1675 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 return NULL;
1677
1678 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 return retval;
1681}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001682# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001684/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001685 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001687 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688 */
1689 void *
1690call_func_retlist(func, argc, argv, safe)
1691 char_u *func;
1692 int argc;
1693 char_u **argv;
1694 int safe; /* use the sandbox */
1695{
1696 typval_T rettv;
1697
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001698 /* All arguments are passed as strings, no conversion to number. */
1699 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001700 return NULL;
1701
1702 if (rettv.v_type != VAR_LIST)
1703 {
1704 clear_tv(&rettv);
1705 return NULL;
1706 }
1707
1708 return rettv.vval.v_list;
1709}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710#endif
1711
1712/*
1713 * Save the current function call pointer, and set it to NULL.
1714 * Used when executing autocommands and for ":source".
1715 */
1716 void *
1717save_funccal()
1718{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001719 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 current_funccal = NULL;
1722 return (void *)fc;
1723}
1724
1725 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001726restore_funccal(vfc)
1727 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729 funccall_T *fc = (funccall_T *)vfc;
1730
1731 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732}
1733
Bram Moolenaar05159a02005-02-26 23:04:13 +00001734#if defined(FEAT_PROFILE) || defined(PROTO)
1735/*
1736 * Prepare profiling for entering a child or something else that is not
1737 * counted for the script/function itself.
1738 * Should always be called in pair with prof_child_exit().
1739 */
1740 void
1741prof_child_enter(tm)
1742 proftime_T *tm; /* place to store waittime */
1743{
1744 funccall_T *fc = current_funccal;
1745
1746 if (fc != NULL && fc->func->uf_profiling)
1747 profile_start(&fc->prof_child);
1748 script_prof_save(tm);
1749}
1750
1751/*
1752 * Take care of time spent in a child.
1753 * Should always be called after prof_child_enter().
1754 */
1755 void
1756prof_child_exit(tm)
1757 proftime_T *tm; /* where waittime was stored */
1758{
1759 funccall_T *fc = current_funccal;
1760
1761 if (fc != NULL && fc->func->uf_profiling)
1762 {
1763 profile_end(&fc->prof_child);
1764 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1765 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1766 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1767 }
1768 script_prof_restore(tm);
1769}
1770#endif
1771
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#ifdef FEAT_FOLDING
1774/*
1775 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1776 * it in "*cp". Doesn't give error messages.
1777 */
1778 int
1779eval_foldexpr(arg, cp)
1780 char_u *arg;
1781 int *cp;
1782{
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 int retval;
1785 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001786 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1787 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
1789 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001790 if (use_sandbox)
1791 ++sandbox;
1792 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 retval = 0;
1796 else
1797 {
1798 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799 if (tv.v_type == VAR_NUMBER)
1800 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001801 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 retval = 0;
1803 else
1804 {
1805 /* If the result is a string, check if there is a non-digit before
1806 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (!VIM_ISDIGIT(*s) && *s != '-')
1809 *cp = *s++;
1810 retval = atol((char *)s);
1811 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
1814 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001815 if (use_sandbox)
1816 --sandbox;
1817 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
1819 return retval;
1820}
1821#endif
1822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 * ":let" list all variable values
1825 * ":let var1 var2" list variable values
1826 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001827 * ":let var += expr" assignment command.
1828 * ":let var -= expr" assignment command.
1829 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 */
1832 void
1833ex_let(eap)
1834 exarg_T *eap;
1835{
1836 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 int var_count = 0;
1841 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001842 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001843 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001844 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 argend = skip_var_list(arg, &var_count, &semicolon);
1847 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001849 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1850 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001851 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001854 /*
1855 * ":let" without "=": list variables
1856 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 if (*arg == '[')
1858 EMSG(_(e_invarg));
1859 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001861 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001865 list_glob_vars(&first);
1866 list_buf_vars(&first);
1867 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001868#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001869 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001870#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001871 list_script_vars(&first);
1872 list_func_vars(&first);
1873 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 eap->nextcmd = check_nextcmd(arg);
1876 }
1877 else
1878 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 op[0] = '=';
1880 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001881 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882 {
1883 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1884 op[0] = expr[-1]; /* +=, -= or .= */
1885 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (eap->skip)
1889 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (eap->skip)
1892 {
1893 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 --emsg_skip;
1896 }
1897 else if (i != FAIL)
1898 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
1903 }
1904}
1905
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906/*
1907 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1908 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 * When "nextchars" is not NULL it points to a string with characters that
1910 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1911 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 * Returns OK or FAIL;
1913 */
1914 static int
1915ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1916 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001917 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 int copy; /* copy values from "tv", don't move */
1919 int semicolon; /* from skip_var_list() */
1920 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922{
1923 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001926 listitem_T *item;
1927 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928
1929 if (*arg != '[')
1930 {
1931 /*
1932 * ":let var = expr" or ":for var in list"
1933 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 return FAIL;
1936 return OK;
1937 }
1938
1939 /*
1940 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1941 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001942 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 {
1944 EMSG(_(e_listreq));
1945 return FAIL;
1946 }
1947
1948 i = list_len(l);
1949 if (semicolon == 0 && var_count < i)
1950 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001951 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 return FAIL;
1953 }
1954 if (var_count - semicolon > i)
1955 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001956 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 return FAIL;
1958 }
1959
1960 item = l->lv_first;
1961 while (*arg != ']')
1962 {
1963 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 item = item->li_next;
1966 if (arg == NULL)
1967 return FAIL;
1968
1969 arg = skipwhite(arg);
1970 if (*arg == ';')
1971 {
1972 /* Put the rest of the list (may be empty) in the var after ';'.
1973 * Create a new list for this. */
1974 l = list_alloc();
1975 if (l == NULL)
1976 return FAIL;
1977 while (item != NULL)
1978 {
1979 list_append_tv(l, &item->li_tv);
1980 item = item->li_next;
1981 }
1982
1983 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001984 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 ltv.vval.v_list = l;
1986 l->lv_refcount = 1;
1987
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001988 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1989 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 clear_tv(&ltv);
1991 if (arg == NULL)
1992 return FAIL;
1993 break;
1994 }
1995 else if (*arg != ',' && *arg != ']')
1996 {
1997 EMSG2(_(e_intern2), "ex_let_vars()");
1998 return FAIL;
1999 }
2000 }
2001
2002 return OK;
2003}
2004
2005/*
2006 * Skip over assignable variable "var" or list of variables "[var, var]".
2007 * Used for ":let varvar = expr" and ":for varvar in expr".
2008 * For "[var, var]" increment "*var_count" for each variable.
2009 * for "[var, var; var]" set "semicolon".
2010 * Return NULL for an error.
2011 */
2012 static char_u *
2013skip_var_list(arg, var_count, semicolon)
2014 char_u *arg;
2015 int *var_count;
2016 int *semicolon;
2017{
2018 char_u *p, *s;
2019
2020 if (*arg == '[')
2021 {
2022 /* "[var, var]": find the matching ']'. */
2023 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002024 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 {
2026 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2027 s = skip_var_one(p);
2028 if (s == p)
2029 {
2030 EMSG2(_(e_invarg2), p);
2031 return NULL;
2032 }
2033 ++*var_count;
2034
2035 p = skipwhite(s);
2036 if (*p == ']')
2037 break;
2038 else if (*p == ';')
2039 {
2040 if (*semicolon == 1)
2041 {
2042 EMSG(_("Double ; in list of variables"));
2043 return NULL;
2044 }
2045 *semicolon = 1;
2046 }
2047 else if (*p != ',')
2048 {
2049 EMSG2(_(e_invarg2), p);
2050 return NULL;
2051 }
2052 }
2053 return p + 1;
2054 }
2055 else
2056 return skip_var_one(arg);
2057}
2058
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002059/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002060 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002061 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002063 static char_u *
2064skip_var_one(arg)
2065 char_u *arg;
2066{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002067 if (*arg == '@' && arg[1] != NUL)
2068 return arg + 2;
2069 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2070 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002071}
2072
Bram Moolenaara7043832005-01-21 11:56:39 +00002073/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 * List variables for hashtab "ht" with prefix "prefix".
2075 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002077 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002078list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002082 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083{
Bram Moolenaar33570922005-01-25 22:26:29 +00002084 hashitem_T *hi;
2085 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 int todo;
2087
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002088 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2090 {
2091 if (!HASHITEM_EMPTY(hi))
2092 {
2093 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002094 di = HI2DI(hi);
2095 if (empty || di->di_tv.v_type != VAR_STRING
2096 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002097 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 }
2099 }
2100}
2101
2102/*
2103 * List global variables.
2104 */
2105 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002106list_glob_vars(first)
2107 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002108{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002110}
2111
2112/*
2113 * List buffer variables.
2114 */
2115 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116list_buf_vars(first)
2117 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002118{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119 char_u numbuf[NUMBUFLEN];
2120
Bram Moolenaar429fa852013-04-15 12:27:36 +02002121 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123
2124 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2126 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002127}
2128
2129/*
2130 * List window variables.
2131 */
2132 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133list_win_vars(first)
2134 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002135{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002136 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002138}
2139
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002140#ifdef FEAT_WINDOWS
2141/*
2142 * List tab page variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_tab_vars(first)
2146 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002148 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002150}
2151#endif
2152
Bram Moolenaara7043832005-01-21 11:56:39 +00002153/*
2154 * List Vim variables.
2155 */
2156 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157list_vim_vars(first)
2158 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161}
2162
2163/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164 * List script-local variables, if there is a script.
2165 */
2166 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167list_script_vars(first)
2168 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002169{
2170 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2172 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002173}
2174
2175/*
2176 * List function variables, if there is a function.
2177 */
2178 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179list_func_vars(first)
2180 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002181{
2182 if (current_funccal != NULL)
2183 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185}
2186
2187/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 * List variables in "arg".
2189 */
2190 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 exarg_T *eap;
2193 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195{
2196 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002199 char_u *name_start;
2200 char_u *arg_subsc;
2201 char_u *tofree;
2202 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203
2204 while (!ends_excmd(*arg) && !got_int)
2205 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002208 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2210 {
2211 emsg_severe = TRUE;
2212 EMSG(_(e_trailing));
2213 break;
2214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 /* get_name_len() takes care of expanding curly braces */
2219 name_start = name = arg;
2220 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2221 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 /* This is mainly to keep test 49 working: when expanding
2224 * curly braces fails overrule the exception error message. */
2225 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002227 emsg_severe = TRUE;
2228 EMSG2(_(e_invarg2), arg);
2229 break;
2230 }
2231 error = TRUE;
2232 }
2233 else
2234 {
2235 if (tofree != NULL)
2236 name = tofree;
2237 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 else
2240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* handle d.key, l[idx], f(expr) */
2242 arg_subsc = arg;
2243 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002250 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 case 'g': list_glob_vars(first); break;
2252 case 'b': list_buf_vars(first); break;
2253 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002254#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002255 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002256#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002257 case 'v': list_vim_vars(first); break;
2258 case 's': list_script_vars(first); break;
2259 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 default:
2261 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 }
2264 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 {
2266 char_u numbuf[NUMBUFLEN];
2267 char_u *tf;
2268 int c;
2269 char_u *s;
2270
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002271 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 c = *arg;
2273 *arg = NUL;
2274 list_one_var_a((char_u *)"",
2275 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002276 tv.v_type,
2277 s == NULL ? (char_u *)"" : s,
2278 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 *arg = c;
2280 vim_free(tf);
2281 }
2282 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286
2287 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289
2290 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
2292
2293 return arg;
2294}
2295
2296/*
2297 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2298 * Returns a pointer to the char just after the var name.
2299 * Returns NULL if there is an error.
2300 */
2301 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002304 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002306 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308{
2309 int c1;
2310 char_u *name;
2311 char_u *p;
2312 char_u *arg_end = NULL;
2313 int len;
2314 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316
2317 /*
2318 * ":let $VAR = expr": Set environment variable.
2319 */
2320 if (*arg == '$')
2321 {
2322 /* Find the end of the name. */
2323 ++arg;
2324 name = arg;
2325 len = get_env_len(&arg);
2326 if (len == 0)
2327 EMSG2(_(e_invarg2), name - 1);
2328 else
2329 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 if (op != NULL && (*op == '+' || *op == '-'))
2331 EMSG2(_(e_letwrong), op);
2332 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002333 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002335 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 {
2337 c1 = name[len];
2338 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002339 p = get_tv_string_chk(tv);
2340 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341 {
2342 int mustfree = FALSE;
2343 char_u *s = vim_getenv(name, &mustfree);
2344
2345 if (s != NULL)
2346 {
2347 p = tofree = concat_str(s, p);
2348 if (mustfree)
2349 vim_free(s);
2350 }
2351 }
2352 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002353 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 if (STRICMP(name, "HOME") == 0)
2356 init_homedir();
2357 else if (didset_vim && STRICMP(name, "VIM") == 0)
2358 didset_vim = FALSE;
2359 else if (didset_vimruntime
2360 && STRICMP(name, "VIMRUNTIME") == 0)
2361 didset_vimruntime = FALSE;
2362 arg_end = arg;
2363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366 }
2367 }
2368 }
2369
2370 /*
2371 * ":let &option = expr": Set option value.
2372 * ":let &l:option = expr": Set local option value.
2373 * ":let &g:option = expr": Set global option value.
2374 */
2375 else if (*arg == '&')
2376 {
2377 /* Find the end of the name. */
2378 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002379 if (p == NULL || (endchars != NULL
2380 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 EMSG(_(e_letunexp));
2382 else
2383 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 long n;
2385 int opt_type;
2386 long numval;
2387 char_u *stringval = NULL;
2388 char_u *s;
2389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 c1 = *p;
2391 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392
2393 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 s = get_tv_string_chk(tv); /* != NULL if number or string */
2395 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 {
2397 opt_type = get_option_value(arg, &numval,
2398 &stringval, opt_flags);
2399 if ((opt_type == 1 && *op == '.')
2400 || (opt_type == 0 && *op != '.'))
2401 EMSG2(_(e_letwrong), op);
2402 else
2403 {
2404 if (opt_type == 1) /* number */
2405 {
2406 if (*op == '+')
2407 n = numval + n;
2408 else
2409 n = numval - n;
2410 }
2411 else if (opt_type == 0 && stringval != NULL) /* string */
2412 {
2413 s = concat_str(stringval, s);
2414 vim_free(stringval);
2415 stringval = s;
2416 }
2417 }
2418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002419 if (s != NULL)
2420 {
2421 set_option_value(arg, n, s, opt_flags);
2422 arg_end = p;
2423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002425 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 }
2427 }
2428
2429 /*
2430 * ":let @r = expr": Set register contents.
2431 */
2432 else if (*arg == '@')
2433 {
2434 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 if (op != NULL && (*op == '+' || *op == '-'))
2436 EMSG2(_(e_letwrong), op);
2437 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002438 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 EMSG(_(e_letunexp));
2440 else
2441 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002442 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 char_u *s;
2444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 p = get_tv_string_chk(tv);
2446 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002448 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 if (s != NULL)
2450 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002451 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 vim_free(s);
2453 }
2454 }
2455 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 arg_end = arg + 1;
2459 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002460 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 }
2462 }
2463
2464 /*
2465 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002468 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002470 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002472 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2476 EMSG(_(e_letunexp));
2477 else
2478 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 arg_end = p;
2481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485
2486 else
2487 EMSG2(_(e_invarg2), arg);
2488
2489 return arg_end;
2490}
2491
2492/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2494 */
2495 static int
2496check_changedtick(arg)
2497 char_u *arg;
2498{
2499 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2500 {
2501 EMSG2(_(e_readonlyvar), arg);
2502 return TRUE;
2503 }
2504 return FALSE;
2505}
2506
2507/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 * Get an lval: variable, Dict item or List item that can be assigned a value
2509 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2510 * "name.key", "name.key[expr]" etc.
2511 * Indexing only works if "name" is an existing List or Dictionary.
2512 * "name" points to the start of the name.
2513 * If "rettv" is not NULL it points to the value to be assigned.
2514 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2515 * wrong; must end in space or cmd separator.
2516 *
2517 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002518 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 */
2521 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002522get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002524 typval_T *rettv;
2525 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 int unlet;
2527 int skip;
2528 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002529 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 char_u *expr_start, *expr_end;
2533 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002534 dictitem_T *v;
2535 typval_T var1;
2536 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545
2546 if (skip)
2547 {
2548 /* When skipping just find the end of the name. */
2549 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002550 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 }
2552
2553 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002554 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (expr_start != NULL)
2556 {
2557 /* Don't expand the name when we already know there is an error. */
2558 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2559 && *p != '[' && *p != '.')
2560 {
2561 EMSG(_(e_trailing));
2562 return NULL;
2563 }
2564
2565 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2566 if (lp->ll_exp_name == NULL)
2567 {
2568 /* Report an invalid expression in braces, unless the
2569 * expression evaluation has been cancelled due to an
2570 * aborting error, an interrupt, or an exception. */
2571 if (!aborting() && !quiet)
2572 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002573 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 EMSG2(_(e_invarg2), name);
2575 return NULL;
2576 }
2577 }
2578 lp->ll_name = lp->ll_exp_name;
2579 }
2580 else
2581 lp->ll_name = name;
2582
2583 /* Without [idx] or .key we are done. */
2584 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2585 return p;
2586
2587 cc = *p;
2588 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002589 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (v == NULL && !quiet)
2591 EMSG2(_(e_undefvar), lp->ll_name);
2592 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 if (v == NULL)
2594 return NULL;
2595
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 /*
2597 * Loop until no more [idx] or .key is following.
2598 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002599 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002601 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2603 && !(lp->ll_tv->v_type == VAR_DICT
2604 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (!quiet)
2607 EMSG(_("E689: Can only index a List or Dictionary"));
2608 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (!quiet)
2613 EMSG(_("E708: [:] must come last"));
2614 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002615 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002616
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 len = -1;
2618 if (*p == '.')
2619 {
2620 key = p + 1;
2621 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2622 ;
2623 if (len == 0)
2624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (!quiet)
2626 EMSG(_(e_emptykey));
2627 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 }
2629 p = key + len;
2630 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 else
2632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 if (*p == ':')
2636 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 else
2638 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 empty1 = FALSE;
2640 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002642 if (get_tv_string_chk(&var1) == NULL)
2643 {
2644 /* not a number or string */
2645 clear_tv(&var1);
2646 return NULL;
2647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 }
2649
2650 /* Optionally get the second index [ :expr]. */
2651 if (*p == ':')
2652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002656 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002657 if (!empty1)
2658 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (rettv != NULL && (rettv->v_type != VAR_LIST
2662 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
2665 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 if (!empty1)
2667 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
2670 p = skipwhite(p + 1);
2671 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 else
2674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2677 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002682 if (get_tv_string_chk(&var2) == NULL)
2683 {
2684 /* not a number or string */
2685 if (!empty1)
2686 clear_tv(&var1);
2687 clear_tv(&var2);
2688 return NULL;
2689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (!quiet)
2699 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 if (!empty1)
2701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
2706
2707 /* Skip to past ']'. */
2708 ++p;
2709 }
2710
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 {
2713 if (len == -1)
2714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002716 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (*key == NUL)
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
2724 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 lp->ll_list = NULL;
2726 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002727 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002728
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002729 /* When assigning to a scope dictionary check that a function and
2730 * variable name is valid (only variable name unless it is l: or
2731 * g: dictionary). Disallow overwriting a builtin function. */
2732 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002733 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002734 int prevval;
2735 int wrong;
2736
2737 if (len != -1)
2738 {
2739 prevval = key[len];
2740 key[len] = NUL;
2741 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002742 else
2743 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002744 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2745 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002746 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002747 || !valid_varname(key);
2748 if (len != -1)
2749 key[len] = prevval;
2750 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002751 return NULL;
2752 }
2753
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756 /* Can't add "v:" variable. */
2757 if (lp->ll_dict == &vimvardict)
2758 {
2759 EMSG2(_(e_illvar), name);
2760 return NULL;
2761 }
2762
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002763 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002767 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 if (len == -1)
2769 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 }
2772 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 if (len == -1)
2777 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 p = NULL;
2780 break;
2781 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782 /* existing variable, need to check if it can be changed */
2783 else if (var_check_ro(lp->ll_di->di_flags, name))
2784 return NULL;
2785
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 if (len == -1)
2787 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 }
2790 else
2791 {
2792 /*
2793 * Get the number and item for the only or first index of the List.
2794 */
2795 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 else
2798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002799 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 clear_tv(&var1);
2801 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002802 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_list = lp->ll_tv->vval.v_list;
2804 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2805 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002807 if (lp->ll_n1 < 0)
2808 {
2809 lp->ll_n1 = 0;
2810 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2811 }
2812 }
2813 if (lp->ll_li == NULL)
2814 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002817 if (!quiet)
2818 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 }
2821
2822 /*
2823 * May need to find the item or absolute index for the second
2824 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 * When no index given: "lp->ll_empty2" is TRUE.
2826 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002830 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002836 {
2837 if (!quiet)
2838 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002840 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 }
2843
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2845 if (lp->ll_n1 < 0)
2846 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2847 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002848 {
2849 if (!quiet)
2850 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002852 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002853 }
2854
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002856 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002857 }
2858
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 return p;
2860}
2861
2862/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002863 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 */
2865 static void
2866clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868{
2869 vim_free(lp->ll_exp_name);
2870 vim_free(lp->ll_newkey);
2871}
2872
2873/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002874 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002876 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 */
2878 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002879set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002880 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002884 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885{
2886 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002887 listitem_T *ri;
2888 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889
2890 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002891 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 cc = *endp;
2895 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 if (op != NULL && *op != '=')
2897 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002899
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002900 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002901 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002902 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903 {
2904 if (tv_op(&tv, rettv, op) == OK)
2905 set_var(lp->ll_name, &tv, FALSE);
2906 clear_tv(&tv);
2907 }
2908 }
2909 else
2910 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002914 else if (tv_check_lock(lp->ll_newkey == NULL
2915 ? lp->ll_tv->v_lock
2916 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2917 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 else if (lp->ll_range)
2919 {
2920 /*
2921 * Assign the List values to the list items.
2922 */
2923 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002924 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 if (op != NULL && *op != '=')
2926 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2927 else
2928 {
2929 clear_tv(&lp->ll_li->li_tv);
2930 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2931 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 ri = ri->li_next;
2933 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2934 break;
2935 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002936 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002938 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 ri = NULL;
2941 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002942 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 lp->ll_li = lp->ll_li->li_next;
2945 ++lp->ll_n1;
2946 }
2947 if (ri != NULL)
2948 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 else if (lp->ll_empty2
2950 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 : lp->ll_n1 != lp->ll_n2)
2952 EMSG(_("E711: List value has not enough items"));
2953 }
2954 else
2955 {
2956 /*
2957 * Assign to a List or Dictionary item.
2958 */
2959 if (lp->ll_newkey != NULL)
2960 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 if (op != NULL && *op != '=')
2962 {
2963 EMSG2(_(e_letwrong), op);
2964 return;
2965 }
2966
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002968 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 if (di == NULL)
2970 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002971 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2972 {
2973 vim_free(di);
2974 return;
2975 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002977 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002978 else if (op != NULL && *op != '=')
2979 {
2980 tv_op(lp->ll_tv, rettv, op);
2981 return;
2982 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002985
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986 /*
2987 * Assign the value to the variable or list item.
2988 */
2989 if (copy)
2990 copy_tv(rettv, lp->ll_tv);
2991 else
2992 {
2993 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002994 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002996 }
2997 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002998}
2999
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003000/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3002 * Returns OK or FAIL.
3003 */
3004 static int
3005tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003006 typval_T *tv1;
3007 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003008 char_u *op;
3009{
3010 long n;
3011 char_u numbuf[NUMBUFLEN];
3012 char_u *s;
3013
3014 /* Can't do anything with a Funcref or a Dict on the right. */
3015 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3016 {
3017 switch (tv1->v_type)
3018 {
3019 case VAR_DICT:
3020 case VAR_FUNC:
3021 break;
3022
3023 case VAR_LIST:
3024 if (*op != '+' || tv2->v_type != VAR_LIST)
3025 break;
3026 /* List += List */
3027 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3028 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3029 return OK;
3030
3031 case VAR_NUMBER:
3032 case VAR_STRING:
3033 if (tv2->v_type == VAR_LIST)
3034 break;
3035 if (*op == '+' || *op == '-')
3036 {
3037 /* nr += nr or nr -= nr*/
3038 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039#ifdef FEAT_FLOAT
3040 if (tv2->v_type == VAR_FLOAT)
3041 {
3042 float_T f = n;
3043
3044 if (*op == '+')
3045 f += tv2->vval.v_float;
3046 else
3047 f -= tv2->vval.v_float;
3048 clear_tv(tv1);
3049 tv1->v_type = VAR_FLOAT;
3050 tv1->vval.v_float = f;
3051 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003053#endif
3054 {
3055 if (*op == '+')
3056 n += get_tv_number(tv2);
3057 else
3058 n -= get_tv_number(tv2);
3059 clear_tv(tv1);
3060 tv1->v_type = VAR_NUMBER;
3061 tv1->vval.v_number = n;
3062 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003063 }
3064 else
3065 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003066 if (tv2->v_type == VAR_FLOAT)
3067 break;
3068
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 /* str .= str */
3070 s = get_tv_string(tv1);
3071 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3072 clear_tv(tv1);
3073 tv1->v_type = VAR_STRING;
3074 tv1->vval.v_string = s;
3075 }
3076 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003077
3078#ifdef FEAT_FLOAT
3079 case VAR_FLOAT:
3080 {
3081 float_T f;
3082
3083 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3084 && tv2->v_type != VAR_NUMBER
3085 && tv2->v_type != VAR_STRING))
3086 break;
3087 if (tv2->v_type == VAR_FLOAT)
3088 f = tv2->vval.v_float;
3089 else
3090 f = get_tv_number(tv2);
3091 if (*op == '+')
3092 tv1->vval.v_float += f;
3093 else
3094 tv1->vval.v_float -= f;
3095 }
3096 return OK;
3097#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003098 }
3099 }
3100
3101 EMSG2(_(e_letwrong), op);
3102 return FAIL;
3103}
3104
3105/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003106 * Add a watcher to a list.
3107 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003108 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003109list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003110 list_T *l;
3111 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003112{
3113 lw->lw_next = l->lv_watch;
3114 l->lv_watch = lw;
3115}
3116
3117/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003118 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119 * No warning when it isn't found...
3120 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003121 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003123 list_T *l;
3124 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003125{
Bram Moolenaar33570922005-01-25 22:26:29 +00003126 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127
3128 lwp = &l->lv_watch;
3129 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3130 {
3131 if (lw == lwrem)
3132 {
3133 *lwp = lw->lw_next;
3134 break;
3135 }
3136 lwp = &lw->lw_next;
3137 }
3138}
3139
3140/*
3141 * Just before removing an item from a list: advance watchers to the next
3142 * item.
3143 */
3144 static void
3145list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003146 list_T *l;
3147 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148{
Bram Moolenaar33570922005-01-25 22:26:29 +00003149 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150
3151 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3152 if (lw->lw_item == item)
3153 lw->lw_item = item->li_next;
3154}
3155
3156/*
3157 * Evaluate the expression used in a ":for var in expr" command.
3158 * "arg" points to "var".
3159 * Set "*errp" to TRUE for an error, FALSE otherwise;
3160 * Return a pointer that holds the info. Null when there is an error.
3161 */
3162 void *
3163eval_for_line(arg, errp, nextcmdp, skip)
3164 char_u *arg;
3165 int *errp;
3166 char_u **nextcmdp;
3167 int skip;
3168{
Bram Moolenaar33570922005-01-25 22:26:29 +00003169 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003171 typval_T tv;
3172 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173
3174 *errp = TRUE; /* default: there is an error */
3175
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 if (fi == NULL)
3178 return NULL;
3179
3180 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3181 if (expr == NULL)
3182 return fi;
3183
3184 expr = skipwhite(expr);
3185 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3186 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003187 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 return fi;
3189 }
3190
3191 if (skip)
3192 ++emsg_skip;
3193 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3194 {
3195 *errp = FALSE;
3196 if (!skip)
3197 {
3198 l = tv.vval.v_list;
3199 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003200 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003202 clear_tv(&tv);
3203 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204 else
3205 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003206 /* No need to increment the refcount, it's already set for the
3207 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 fi->fi_list = l;
3209 list_add_watch(l, &fi->fi_lw);
3210 fi->fi_lw.lw_item = l->lv_first;
3211 }
3212 }
3213 }
3214 if (skip)
3215 --emsg_skip;
3216
3217 return fi;
3218}
3219
3220/*
3221 * Use the first item in a ":for" list. Advance to the next.
3222 * Assign the values to the variable (list). "arg" points to the first one.
3223 * Return TRUE when a valid item was found, FALSE when at end of list or
3224 * something wrong.
3225 */
3226 int
3227next_for_item(fi_void, arg)
3228 void *fi_void;
3229 char_u *arg;
3230{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003231 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234
3235 item = fi->fi_lw.lw_item;
3236 if (item == NULL)
3237 result = FALSE;
3238 else
3239 {
3240 fi->fi_lw.lw_item = item->li_next;
3241 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3242 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3243 }
3244 return result;
3245}
3246
3247/*
3248 * Free the structure used to store info used by ":for".
3249 */
3250 void
3251free_for_info(fi_void)
3252 void *fi_void;
3253{
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003256 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003257 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003259 list_unref(fi->fi_list);
3260 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 vim_free(fi);
3262}
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3265
3266 void
3267set_context_for_expression(xp, arg, cmdidx)
3268 expand_T *xp;
3269 char_u *arg;
3270 cmdidx_T cmdidx;
3271{
3272 int got_eq = FALSE;
3273 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 if (cmdidx == CMD_let)
3277 {
3278 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003279 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 {
3281 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003282 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 {
3284 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003285 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 if (vim_iswhite(*p))
3287 break;
3288 }
3289 return;
3290 }
3291 }
3292 else
3293 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3294 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 while ((xp->xp_pattern = vim_strpbrk(arg,
3296 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3297 {
3298 c = *xp->xp_pattern;
3299 if (c == '&')
3300 {
3301 c = xp->xp_pattern[1];
3302 if (c == '&')
3303 {
3304 ++xp->xp_pattern;
3305 xp->xp_context = cmdidx != CMD_let || got_eq
3306 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3307 }
3308 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003311 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3312 xp->xp_pattern += 2;
3313
3314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 }
3316 else if (c == '$')
3317 {
3318 /* environment variable */
3319 xp->xp_context = EXPAND_ENV_VARS;
3320 }
3321 else if (c == '=')
3322 {
3323 got_eq = TRUE;
3324 xp->xp_context = EXPAND_EXPRESSION;
3325 }
3326 else if (c == '<'
3327 && xp->xp_context == EXPAND_FUNCTIONS
3328 && vim_strchr(xp->xp_pattern, '(') == NULL)
3329 {
3330 /* Function name can start with "<SNR>" */
3331 break;
3332 }
3333 else if (cmdidx != CMD_let || got_eq)
3334 {
3335 if (c == '"') /* string */
3336 {
3337 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3338 if (c == '\\' && xp->xp_pattern[1] != NUL)
3339 ++xp->xp_pattern;
3340 xp->xp_context = EXPAND_NOTHING;
3341 }
3342 else if (c == '\'') /* literal string */
3343 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003344 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3346 /* skip */ ;
3347 xp->xp_context = EXPAND_NOTHING;
3348 }
3349 else if (c == '|')
3350 {
3351 if (xp->xp_pattern[1] == '|')
3352 {
3353 ++xp->xp_pattern;
3354 xp->xp_context = EXPAND_EXPRESSION;
3355 }
3356 else
3357 xp->xp_context = EXPAND_COMMANDS;
3358 }
3359 else
3360 xp->xp_context = EXPAND_EXPRESSION;
3361 }
3362 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003363 /* Doesn't look like something valid, expand as an expression
3364 * anyway. */
3365 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 arg = xp->xp_pattern;
3367 if (*arg != NUL)
3368 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3369 /* skip */ ;
3370 }
3371 xp->xp_pattern = arg;
3372}
3373
3374#endif /* FEAT_CMDL_COMPL */
3375
3376/*
3377 * ":1,25call func(arg1, arg2)" function call.
3378 */
3379 void
3380ex_call(eap)
3381 exarg_T *eap;
3382{
3383 char_u *arg = eap->arg;
3384 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003386 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003388 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 linenr_T lnum;
3390 int doesrange;
3391 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003392 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003394 if (eap->skip)
3395 {
3396 /* trans_function_name() doesn't work well when skipping, use eval0()
3397 * instead to skip to any following command, e.g. for:
3398 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003399 ++emsg_skip;
3400 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3401 clear_tv(&rettv);
3402 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003403 return;
3404 }
3405
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003406 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003407 if (fudi.fd_newkey != NULL)
3408 {
3409 /* Still need to give an error message for missing key. */
3410 EMSG2(_(e_dictkey), fudi.fd_newkey);
3411 vim_free(fudi.fd_newkey);
3412 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003413 if (tofree == NULL)
3414 return;
3415
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003416 /* Increase refcount on dictionary, it could get deleted when evaluating
3417 * the arguments. */
3418 if (fudi.fd_dict != NULL)
3419 ++fudi.fd_dict->dv_refcount;
3420
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003421 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003422 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003423 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
Bram Moolenaar532c7802005-01-27 14:44:31 +00003425 /* Skip white space to allow ":call func ()". Not good, but required for
3426 * backward compatibility. */
3427 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003428 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430 if (*startarg != '(')
3431 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003432 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 goto end;
3434 }
3435
3436 /*
3437 * When skipping, evaluate the function once, to find the end of the
3438 * arguments.
3439 * When the function takes a range, this is discovered after the first
3440 * call, and the loop is broken.
3441 */
3442 if (eap->skip)
3443 {
3444 ++emsg_skip;
3445 lnum = eap->line2; /* do it once, also with an invalid range */
3446 }
3447 else
3448 lnum = eap->line1;
3449 for ( ; lnum <= eap->line2; ++lnum)
3450 {
3451 if (!eap->skip && eap->addr_count > 0)
3452 {
3453 curwin->w_cursor.lnum = lnum;
3454 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003455#ifdef FEAT_VIRTUALEDIT
3456 curwin->w_cursor.coladd = 0;
3457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
3459 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003460 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003461 eap->line1, eap->line2, &doesrange,
3462 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
3464 failed = TRUE;
3465 break;
3466 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003467
3468 /* Handle a function returning a Funcref, Dictionary or List. */
3469 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3470 {
3471 failed = TRUE;
3472 break;
3473 }
3474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003475 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (doesrange || eap->skip)
3477 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003478
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003480 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003481 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003482 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (aborting())
3484 break;
3485 }
3486 if (eap->skip)
3487 --emsg_skip;
3488
3489 if (!failed)
3490 {
3491 /* Check for trailing illegal characters and a following command. */
3492 if (!ends_excmd(*arg))
3493 {
3494 emsg_severe = TRUE;
3495 EMSG(_(e_trailing));
3496 }
3497 else
3498 eap->nextcmd = check_nextcmd(arg);
3499 }
3500
3501end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003502 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003503 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504}
3505
3506/*
3507 * ":unlet[!] var1 ... " command.
3508 */
3509 void
3510ex_unlet(eap)
3511 exarg_T *eap;
3512{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003513 ex_unletlock(eap, eap->arg, 0);
3514}
3515
3516/*
3517 * ":lockvar" and ":unlockvar" commands
3518 */
3519 void
3520ex_lockvar(eap)
3521 exarg_T *eap;
3522{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003524 int deep = 2;
3525
3526 if (eap->forceit)
3527 deep = -1;
3528 else if (vim_isdigit(*arg))
3529 {
3530 deep = getdigits(&arg);
3531 arg = skipwhite(arg);
3532 }
3533
3534 ex_unletlock(eap, arg, deep);
3535}
3536
3537/*
3538 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3539 */
3540 static void
3541ex_unletlock(eap, argstart, deep)
3542 exarg_T *eap;
3543 char_u *argstart;
3544 int deep;
3545{
3546 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003549 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
3551 do
3552 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003553 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003554 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3555 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003556 if (lv.ll_name == NULL)
3557 error = TRUE; /* error but continue parsing */
3558 if (name_end == NULL || (!vim_iswhite(*name_end)
3559 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003561 if (name_end != NULL)
3562 {
3563 emsg_severe = TRUE;
3564 EMSG(_(e_trailing));
3565 }
3566 if (!(eap->skip || error))
3567 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 break;
3569 }
3570
3571 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003572 {
3573 if (eap->cmdidx == CMD_unlet)
3574 {
3575 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3576 error = TRUE;
3577 }
3578 else
3579 {
3580 if (do_lock_var(&lv, name_end, deep,
3581 eap->cmdidx == CMD_lockvar) == FAIL)
3582 error = TRUE;
3583 }
3584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003586 if (!eap->skip)
3587 clear_lval(&lv);
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 arg = skipwhite(name_end);
3590 } while (!ends_excmd(*arg));
3591
3592 eap->nextcmd = check_nextcmd(arg);
3593}
3594
Bram Moolenaar8c711452005-01-14 21:53:12 +00003595 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003597 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 int forceit;
3600{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601 int ret = OK;
3602 int cc;
3603
3604 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 cc = *name_end;
3607 *name_end = NUL;
3608
3609 /* Normal name or expanded name. */
3610 if (check_changedtick(lp->ll_name))
3611 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003612 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3617 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 else if (lp->ll_range)
3619 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003620 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621
3622 /* Delete a range of List items. */
3623 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3624 {
3625 li = lp->ll_li->li_next;
3626 listitem_remove(lp->ll_list, lp->ll_li);
3627 lp->ll_li = li;
3628 ++lp->ll_n1;
3629 }
3630 }
3631 else
3632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 /* unlet a List item. */
3635 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003638 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 }
3640
3641 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003642}
3643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644/*
3645 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 */
3648 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003649do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003651 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
Bram Moolenaar33570922005-01-25 22:26:29 +00003653 hashtab_T *ht;
3654 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003655 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003656 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 ht = find_var_ht(name, &varname);
3659 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003661 hi = hash_find(ht, varname);
3662 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003663 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003664 di = HI2DI(hi);
3665 if (var_check_fixed(di->di_flags, name)
3666 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667 return FAIL;
3668 delete_var(ht, hi);
3669 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 if (forceit)
3673 return OK;
3674 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 return FAIL;
3676}
3677
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678/*
3679 * Lock or unlock variable indicated by "lp".
3680 * "deep" is the levels to go (-1 for unlimited);
3681 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3682 */
3683 static int
3684do_lock_var(lp, name_end, deep, lock)
3685 lval_T *lp;
3686 char_u *name_end;
3687 int deep;
3688 int lock;
3689{
3690 int ret = OK;
3691 int cc;
3692 dictitem_T *di;
3693
3694 if (deep == 0) /* nothing to do */
3695 return OK;
3696
3697 if (lp->ll_tv == NULL)
3698 {
3699 cc = *name_end;
3700 *name_end = NUL;
3701
3702 /* Normal name or expanded name. */
3703 if (check_changedtick(lp->ll_name))
3704 ret = FAIL;
3705 else
3706 {
3707 di = find_var(lp->ll_name, NULL);
3708 if (di == NULL)
3709 ret = FAIL;
3710 else
3711 {
3712 if (lock)
3713 di->di_flags |= DI_FLAGS_LOCK;
3714 else
3715 di->di_flags &= ~DI_FLAGS_LOCK;
3716 item_lock(&di->di_tv, deep, lock);
3717 }
3718 }
3719 *name_end = cc;
3720 }
3721 else if (lp->ll_range)
3722 {
3723 listitem_T *li = lp->ll_li;
3724
3725 /* (un)lock a range of List items. */
3726 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3727 {
3728 item_lock(&li->li_tv, deep, lock);
3729 li = li->li_next;
3730 ++lp->ll_n1;
3731 }
3732 }
3733 else if (lp->ll_list != NULL)
3734 /* (un)lock a List item. */
3735 item_lock(&lp->ll_li->li_tv, deep, lock);
3736 else
3737 /* un(lock) a Dictionary item. */
3738 item_lock(&lp->ll_di->di_tv, deep, lock);
3739
3740 return ret;
3741}
3742
3743/*
3744 * Lock or unlock an item. "deep" is nr of levels to go.
3745 */
3746 static void
3747item_lock(tv, deep, lock)
3748 typval_T *tv;
3749 int deep;
3750 int lock;
3751{
3752 static int recurse = 0;
3753 list_T *l;
3754 listitem_T *li;
3755 dict_T *d;
3756 hashitem_T *hi;
3757 int todo;
3758
3759 if (recurse >= DICT_MAXNEST)
3760 {
3761 EMSG(_("E743: variable nested too deep for (un)lock"));
3762 return;
3763 }
3764 if (deep == 0)
3765 return;
3766 ++recurse;
3767
3768 /* lock/unlock the item itself */
3769 if (lock)
3770 tv->v_lock |= VAR_LOCKED;
3771 else
3772 tv->v_lock &= ~VAR_LOCKED;
3773
3774 switch (tv->v_type)
3775 {
3776 case VAR_LIST:
3777 if ((l = tv->vval.v_list) != NULL)
3778 {
3779 if (lock)
3780 l->lv_lock |= VAR_LOCKED;
3781 else
3782 l->lv_lock &= ~VAR_LOCKED;
3783 if (deep < 0 || deep > 1)
3784 /* recursive: lock/unlock the items the List contains */
3785 for (li = l->lv_first; li != NULL; li = li->li_next)
3786 item_lock(&li->li_tv, deep - 1, lock);
3787 }
3788 break;
3789 case VAR_DICT:
3790 if ((d = tv->vval.v_dict) != NULL)
3791 {
3792 if (lock)
3793 d->dv_lock |= VAR_LOCKED;
3794 else
3795 d->dv_lock &= ~VAR_LOCKED;
3796 if (deep < 0 || deep > 1)
3797 {
3798 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003799 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003800 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3801 {
3802 if (!HASHITEM_EMPTY(hi))
3803 {
3804 --todo;
3805 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3806 }
3807 }
3808 }
3809 }
3810 }
3811 --recurse;
3812}
3813
Bram Moolenaara40058a2005-07-11 22:42:07 +00003814/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003815 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3816 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003817 */
3818 static int
3819tv_islocked(tv)
3820 typval_T *tv;
3821{
3822 return (tv->v_lock & VAR_LOCKED)
3823 || (tv->v_type == VAR_LIST
3824 && tv->vval.v_list != NULL
3825 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3826 || (tv->v_type == VAR_DICT
3827 && tv->vval.v_dict != NULL
3828 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3829}
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3832/*
3833 * Delete all "menutrans_" variables.
3834 */
3835 void
3836del_menutrans_vars()
3837{
Bram Moolenaar33570922005-01-25 22:26:29 +00003838 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003839 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
Bram Moolenaar33570922005-01-25 22:26:29 +00003841 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003842 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003843 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003844 {
3845 if (!HASHITEM_EMPTY(hi))
3846 {
3847 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3849 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 }
3851 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003852 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853}
3854#endif
3855
3856#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3857
3858/*
3859 * Local string buffer for the next two functions to store a variable name
3860 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3861 * get_user_var_name().
3862 */
3863
3864static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3865
3866static char_u *varnamebuf = NULL;
3867static int varnamebuflen = 0;
3868
3869/*
3870 * Function to concatenate a prefix and a variable name.
3871 */
3872 static char_u *
3873cat_prefix_varname(prefix, name)
3874 int prefix;
3875 char_u *name;
3876{
3877 int len;
3878
3879 len = (int)STRLEN(name) + 3;
3880 if (len > varnamebuflen)
3881 {
3882 vim_free(varnamebuf);
3883 len += 10; /* some additional space */
3884 varnamebuf = alloc(len);
3885 if (varnamebuf == NULL)
3886 {
3887 varnamebuflen = 0;
3888 return NULL;
3889 }
3890 varnamebuflen = len;
3891 }
3892 *varnamebuf = prefix;
3893 varnamebuf[1] = ':';
3894 STRCPY(varnamebuf + 2, name);
3895 return varnamebuf;
3896}
3897
3898/*
3899 * Function given to ExpandGeneric() to obtain the list of user defined
3900 * (global/buffer/window/built-in) variable names.
3901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 char_u *
3903get_user_var_name(xp, idx)
3904 expand_T *xp;
3905 int idx;
3906{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003907 static long_u gdone;
3908 static long_u bdone;
3909 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003910#ifdef FEAT_WINDOWS
3911 static long_u tdone;
3912#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003913 static int vidx;
3914 static hashitem_T *hi;
3915 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
3917 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003918 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003919 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003920#ifdef FEAT_WINDOWS
3921 tdone = 0;
3922#endif
3923 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003924
3925 /* Global variables */
3926 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003929 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003930 else
3931 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 while (HASHITEM_EMPTY(hi))
3933 ++hi;
3934 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3935 return cat_prefix_varname('g', hi->hi_key);
3936 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003938
3939 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003940 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003945 else
3946 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 while (HASHITEM_EMPTY(hi))
3948 ++hi;
3949 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 return (char_u *)"b:changedtick";
3955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003956
3957 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003958 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003961 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003963 else
3964 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 while (HASHITEM_EMPTY(hi))
3966 ++hi;
3967 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003969
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003970#ifdef FEAT_WINDOWS
3971 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003972 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003973 if (tdone < ht->ht_used)
3974 {
3975 if (tdone++ == 0)
3976 hi = ht->ht_array;
3977 else
3978 ++hi;
3979 while (HASHITEM_EMPTY(hi))
3980 ++hi;
3981 return cat_prefix_varname('t', hi->hi_key);
3982 }
3983#endif
3984
Bram Moolenaar33570922005-01-25 22:26:29 +00003985 /* v: variables */
3986 if (vidx < VV_LEN)
3987 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988
3989 vim_free(varnamebuf);
3990 varnamebuf = NULL;
3991 varnamebuflen = 0;
3992 return NULL;
3993}
3994
3995#endif /* FEAT_CMDL_COMPL */
3996
3997/*
3998 * types for expressions.
3999 */
4000typedef enum
4001{
4002 TYPE_UNKNOWN = 0
4003 , TYPE_EQUAL /* == */
4004 , TYPE_NEQUAL /* != */
4005 , TYPE_GREATER /* > */
4006 , TYPE_GEQUAL /* >= */
4007 , TYPE_SMALLER /* < */
4008 , TYPE_SEQUAL /* <= */
4009 , TYPE_MATCH /* =~ */
4010 , TYPE_NOMATCH /* !~ */
4011} exptype_T;
4012
4013/*
4014 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4017 */
4018
4019/*
4020 * Handle zero level expression.
4021 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004023 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 * Return OK or FAIL.
4025 */
4026 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 char_u **nextcmd;
4031 int evaluate;
4032{
4033 int ret;
4034 char_u *p;
4035
4036 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004037 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 if (ret == FAIL || !ends_excmd(*p))
4039 {
4040 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004041 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 /*
4043 * Report the invalid expression unless the expression evaluation has
4044 * been cancelled due to an aborting error, an interrupt, or an
4045 * exception.
4046 */
4047 if (!aborting())
4048 EMSG2(_(e_invexpr2), arg);
4049 ret = FAIL;
4050 }
4051 if (nextcmd != NULL)
4052 *nextcmd = check_nextcmd(p);
4053
4054 return ret;
4055}
4056
4057/*
4058 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004059 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 *
4061 * "arg" must point to the first non-white of the expression.
4062 * "arg" is advanced to the next non-white after the recognized expression.
4063 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004064 * Note: "rettv.v_lock" is not set.
4065 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 * Return OK or FAIL.
4067 */
4068 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 int evaluate;
4073{
4074 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004075 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077 /*
4078 * Get the first variable.
4079 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 return FAIL;
4082
4083 if ((*arg)[0] == '?')
4084 {
4085 result = FALSE;
4086 if (evaluate)
4087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 int error = FALSE;
4089
4090 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004093 if (error)
4094 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096
4097 /*
4098 * Get the second variable.
4099 */
4100 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 return FAIL;
4103
4104 /*
4105 * Check for the ":".
4106 */
4107 if ((*arg)[0] != ':')
4108 {
4109 EMSG(_("E109: Missing ':' after '?'"));
4110 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 return FAIL;
4113 }
4114
4115 /*
4116 * Get the third variable.
4117 */
4118 *arg = skipwhite(*arg + 1);
4119 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4120 {
4121 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124 }
4125 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004126 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
4128
4129 return OK;
4130}
4131
4132/*
4133 * Handle first level expression:
4134 * expr2 || expr2 || expr2 logical OR
4135 *
4136 * "arg" must point to the first non-white of the expression.
4137 * "arg" is advanced to the next non-white after the recognized expression.
4138 *
4139 * Return OK or FAIL.
4140 */
4141 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 int evaluate;
4146{
Bram Moolenaar33570922005-01-25 22:26:29 +00004147 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 long result;
4149 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004150 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
4152 /*
4153 * Get the first variable.
4154 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 return FAIL;
4157
4158 /*
4159 * Repeat until there is no following "||".
4160 */
4161 first = TRUE;
4162 result = FALSE;
4163 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4164 {
4165 if (evaluate && first)
4166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004170 if (error)
4171 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 first = FALSE;
4173 }
4174
4175 /*
4176 * Get the second variable.
4177 */
4178 *arg = skipwhite(*arg + 2);
4179 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4180 return FAIL;
4181
4182 /*
4183 * Compute the result.
4184 */
4185 if (evaluate && !result)
4186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (error)
4191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 if (evaluate)
4194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 rettv->v_type = VAR_NUMBER;
4196 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198 }
4199
4200 return OK;
4201}
4202
4203/*
4204 * Handle second level expression:
4205 * expr3 && expr3 && expr3 logical AND
4206 *
4207 * "arg" must point to the first non-white of the expression.
4208 * "arg" is advanced to the next non-white after the recognized expression.
4209 *
4210 * Return OK or FAIL.
4211 */
4212 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 int evaluate;
4217{
Bram Moolenaar33570922005-01-25 22:26:29 +00004218 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 long result;
4220 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004221 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223 /*
4224 * Get the first variable.
4225 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 return FAIL;
4228
4229 /*
4230 * Repeat until there is no following "&&".
4231 */
4232 first = TRUE;
4233 result = TRUE;
4234 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4235 {
4236 if (evaluate && first)
4237 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 if (error)
4242 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 first = FALSE;
4244 }
4245
4246 /*
4247 * Get the second variable.
4248 */
4249 *arg = skipwhite(*arg + 2);
4250 if (eval4(arg, &var2, evaluate && result) == FAIL)
4251 return FAIL;
4252
4253 /*
4254 * Compute the result.
4255 */
4256 if (evaluate && result)
4257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (error)
4262 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264 if (evaluate)
4265 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 rettv->v_type = VAR_NUMBER;
4267 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 }
4270
4271 return OK;
4272}
4273
4274/*
4275 * Handle third level expression:
4276 * var1 == var2
4277 * var1 =~ var2
4278 * var1 != var2
4279 * var1 !~ var2
4280 * var1 > var2
4281 * var1 >= var2
4282 * var1 < var2
4283 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004284 * var1 is var2
4285 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 *
4287 * "arg" must point to the first non-white of the expression.
4288 * "arg" is advanced to the next non-white after the recognized expression.
4289 *
4290 * Return OK or FAIL.
4291 */
4292 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 int evaluate;
4297{
Bram Moolenaar33570922005-01-25 22:26:29 +00004298 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 char_u *p;
4300 int i;
4301 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004302 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 int len = 2;
4304 long n1, n2;
4305 char_u *s1, *s2;
4306 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4307 regmatch_T regmatch;
4308 int ic;
4309 char_u *save_cpo;
4310
4311 /*
4312 * Get the first variable.
4313 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 return FAIL;
4316
4317 p = *arg;
4318 switch (p[0])
4319 {
4320 case '=': if (p[1] == '=')
4321 type = TYPE_EQUAL;
4322 else if (p[1] == '~')
4323 type = TYPE_MATCH;
4324 break;
4325 case '!': if (p[1] == '=')
4326 type = TYPE_NEQUAL;
4327 else if (p[1] == '~')
4328 type = TYPE_NOMATCH;
4329 break;
4330 case '>': if (p[1] != '=')
4331 {
4332 type = TYPE_GREATER;
4333 len = 1;
4334 }
4335 else
4336 type = TYPE_GEQUAL;
4337 break;
4338 case '<': if (p[1] != '=')
4339 {
4340 type = TYPE_SMALLER;
4341 len = 1;
4342 }
4343 else
4344 type = TYPE_SEQUAL;
4345 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004346 case 'i': if (p[1] == 's')
4347 {
4348 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4349 len = 5;
4350 if (!vim_isIDc(p[len]))
4351 {
4352 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4353 type_is = TRUE;
4354 }
4355 }
4356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 }
4358
4359 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004360 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 */
4362 if (type != TYPE_UNKNOWN)
4363 {
4364 /* extra question mark appended: ignore case */
4365 if (p[len] == '?')
4366 {
4367 ic = TRUE;
4368 ++len;
4369 }
4370 /* extra '#' appended: match case */
4371 else if (p[len] == '#')
4372 {
4373 ic = FALSE;
4374 ++len;
4375 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004376 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 else
4378 ic = p_ic;
4379
4380 /*
4381 * Get the second variable.
4382 */
4383 *arg = skipwhite(p + len);
4384 if (eval5(arg, &var2, evaluate) == FAIL)
4385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004386 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 return FAIL;
4388 }
4389
4390 if (evaluate)
4391 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004392 if (type_is && rettv->v_type != var2.v_type)
4393 {
4394 /* For "is" a different type always means FALSE, for "notis"
4395 * it means TRUE. */
4396 n1 = (type == TYPE_NEQUAL);
4397 }
4398 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4399 {
4400 if (type_is)
4401 {
4402 n1 = (rettv->v_type == var2.v_type
4403 && rettv->vval.v_list == var2.vval.v_list);
4404 if (type == TYPE_NEQUAL)
4405 n1 = !n1;
4406 }
4407 else if (rettv->v_type != var2.v_type
4408 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4409 {
4410 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004411 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004412 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004413 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004414 clear_tv(rettv);
4415 clear_tv(&var2);
4416 return FAIL;
4417 }
4418 else
4419 {
4420 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004421 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4422 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004423 if (type == TYPE_NEQUAL)
4424 n1 = !n1;
4425 }
4426 }
4427
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004428 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4429 {
4430 if (type_is)
4431 {
4432 n1 = (rettv->v_type == var2.v_type
4433 && rettv->vval.v_dict == var2.vval.v_dict);
4434 if (type == TYPE_NEQUAL)
4435 n1 = !n1;
4436 }
4437 else if (rettv->v_type != var2.v_type
4438 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4439 {
4440 if (rettv->v_type != var2.v_type)
4441 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4442 else
4443 EMSG(_("E736: Invalid operation for Dictionary"));
4444 clear_tv(rettv);
4445 clear_tv(&var2);
4446 return FAIL;
4447 }
4448 else
4449 {
4450 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004451 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4452 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004453 if (type == TYPE_NEQUAL)
4454 n1 = !n1;
4455 }
4456 }
4457
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004458 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4459 {
4460 if (rettv->v_type != var2.v_type
4461 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4462 {
4463 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004464 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004466 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004467 clear_tv(rettv);
4468 clear_tv(&var2);
4469 return FAIL;
4470 }
4471 else
4472 {
4473 /* Compare two Funcrefs for being equal or unequal. */
4474 if (rettv->vval.v_string == NULL
4475 || var2.vval.v_string == NULL)
4476 n1 = FALSE;
4477 else
4478 n1 = STRCMP(rettv->vval.v_string,
4479 var2.vval.v_string) == 0;
4480 if (type == TYPE_NEQUAL)
4481 n1 = !n1;
4482 }
4483 }
4484
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004485#ifdef FEAT_FLOAT
4486 /*
4487 * If one of the two variables is a float, compare as a float.
4488 * When using "=~" or "!~", always compare as string.
4489 */
4490 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4491 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4492 {
4493 float_T f1, f2;
4494
4495 if (rettv->v_type == VAR_FLOAT)
4496 f1 = rettv->vval.v_float;
4497 else
4498 f1 = get_tv_number(rettv);
4499 if (var2.v_type == VAR_FLOAT)
4500 f2 = var2.vval.v_float;
4501 else
4502 f2 = get_tv_number(&var2);
4503 n1 = FALSE;
4504 switch (type)
4505 {
4506 case TYPE_EQUAL: n1 = (f1 == f2); break;
4507 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4508 case TYPE_GREATER: n1 = (f1 > f2); break;
4509 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4510 case TYPE_SMALLER: n1 = (f1 < f2); break;
4511 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4512 case TYPE_UNKNOWN:
4513 case TYPE_MATCH:
4514 case TYPE_NOMATCH: break; /* avoid gcc warning */
4515 }
4516 }
4517#endif
4518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 /*
4520 * If one of the two variables is a number, compare as a number.
4521 * When using "=~" or "!~", always compare as string.
4522 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004523 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004526 n1 = get_tv_number(rettv);
4527 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 switch (type)
4529 {
4530 case TYPE_EQUAL: n1 = (n1 == n2); break;
4531 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4532 case TYPE_GREATER: n1 = (n1 > n2); break;
4533 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4534 case TYPE_SMALLER: n1 = (n1 < n2); break;
4535 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4536 case TYPE_UNKNOWN:
4537 case TYPE_MATCH:
4538 case TYPE_NOMATCH: break; /* avoid gcc warning */
4539 }
4540 }
4541 else
4542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004543 s1 = get_tv_string_buf(rettv, buf1);
4544 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4546 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4547 else
4548 i = 0;
4549 n1 = FALSE;
4550 switch (type)
4551 {
4552 case TYPE_EQUAL: n1 = (i == 0); break;
4553 case TYPE_NEQUAL: n1 = (i != 0); break;
4554 case TYPE_GREATER: n1 = (i > 0); break;
4555 case TYPE_GEQUAL: n1 = (i >= 0); break;
4556 case TYPE_SMALLER: n1 = (i < 0); break;
4557 case TYPE_SEQUAL: n1 = (i <= 0); break;
4558
4559 case TYPE_MATCH:
4560 case TYPE_NOMATCH:
4561 /* avoid 'l' flag in 'cpoptions' */
4562 save_cpo = p_cpo;
4563 p_cpo = (char_u *)"";
4564 regmatch.regprog = vim_regcomp(s2,
4565 RE_MAGIC + RE_STRING);
4566 regmatch.rm_ic = ic;
4567 if (regmatch.regprog != NULL)
4568 {
4569 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004570 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 if (type == TYPE_NOMATCH)
4572 n1 = !n1;
4573 }
4574 p_cpo = save_cpo;
4575 break;
4576
4577 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4578 }
4579 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004580 clear_tv(rettv);
4581 clear_tv(&var2);
4582 rettv->v_type = VAR_NUMBER;
4583 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
4585 }
4586
4587 return OK;
4588}
4589
4590/*
4591 * Handle fourth level expression:
4592 * + number addition
4593 * - number subtraction
4594 * . string concatenation
4595 *
4596 * "arg" must point to the first non-white of the expression.
4597 * "arg" is advanced to the next non-white after the recognized expression.
4598 *
4599 * Return OK or FAIL.
4600 */
4601 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004602eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 int evaluate;
4606{
Bram Moolenaar33570922005-01-25 22:26:29 +00004607 typval_T var2;
4608 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 int op;
4610 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004611#ifdef FEAT_FLOAT
4612 float_T f1 = 0, f2 = 0;
4613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 char_u *s1, *s2;
4615 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4616 char_u *p;
4617
4618 /*
4619 * Get the first variable.
4620 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004621 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 return FAIL;
4623
4624 /*
4625 * Repeat computing, until no '+', '-' or '.' is following.
4626 */
4627 for (;;)
4628 {
4629 op = **arg;
4630 if (op != '+' && op != '-' && op != '.')
4631 break;
4632
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004633 if ((op != '+' || rettv->v_type != VAR_LIST)
4634#ifdef FEAT_FLOAT
4635 && (op == '.' || rettv->v_type != VAR_FLOAT)
4636#endif
4637 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004638 {
4639 /* For "list + ...", an illegal use of the first operand as
4640 * a number cannot be determined before evaluating the 2nd
4641 * operand: if this is also a list, all is ok.
4642 * For "something . ...", "something - ..." or "non-list + ...",
4643 * we know that the first operand needs to be a string or number
4644 * without evaluating the 2nd operand. So check before to avoid
4645 * side effects after an error. */
4646 if (evaluate && get_tv_string_chk(rettv) == NULL)
4647 {
4648 clear_tv(rettv);
4649 return FAIL;
4650 }
4651 }
4652
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 /*
4654 * Get the second variable.
4655 */
4656 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004657 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004659 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 return FAIL;
4661 }
4662
4663 if (evaluate)
4664 {
4665 /*
4666 * Compute the result.
4667 */
4668 if (op == '.')
4669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004670 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4671 s2 = get_tv_string_buf_chk(&var2, buf2);
4672 if (s2 == NULL) /* type error ? */
4673 {
4674 clear_tv(rettv);
4675 clear_tv(&var2);
4676 return FAIL;
4677 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004678 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004679 clear_tv(rettv);
4680 rettv->v_type = VAR_STRING;
4681 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004683 else if (op == '+' && rettv->v_type == VAR_LIST
4684 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004685 {
4686 /* concatenate Lists */
4687 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4688 &var3) == FAIL)
4689 {
4690 clear_tv(rettv);
4691 clear_tv(&var2);
4692 return FAIL;
4693 }
4694 clear_tv(rettv);
4695 *rettv = var3;
4696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 else
4698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004699 int error = FALSE;
4700
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004701#ifdef FEAT_FLOAT
4702 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004703 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004704 f1 = rettv->vval.v_float;
4705 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004706 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004707 else
4708#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004709 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004710 n1 = get_tv_number_chk(rettv, &error);
4711 if (error)
4712 {
4713 /* This can only happen for "list + non-list". For
4714 * "non-list + ..." or "something - ...", we returned
4715 * before evaluating the 2nd operand. */
4716 clear_tv(rettv);
4717 return FAIL;
4718 }
4719#ifdef FEAT_FLOAT
4720 if (var2.v_type == VAR_FLOAT)
4721 f1 = n1;
4722#endif
4723 }
4724#ifdef FEAT_FLOAT
4725 if (var2.v_type == VAR_FLOAT)
4726 {
4727 f2 = var2.vval.v_float;
4728 n2 = 0;
4729 }
4730 else
4731#endif
4732 {
4733 n2 = get_tv_number_chk(&var2, &error);
4734 if (error)
4735 {
4736 clear_tv(rettv);
4737 clear_tv(&var2);
4738 return FAIL;
4739 }
4740#ifdef FEAT_FLOAT
4741 if (rettv->v_type == VAR_FLOAT)
4742 f2 = n2;
4743#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004744 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004745 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746
4747#ifdef FEAT_FLOAT
4748 /* If there is a float on either side the result is a float. */
4749 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4750 {
4751 if (op == '+')
4752 f1 = f1 + f2;
4753 else
4754 f1 = f1 - f2;
4755 rettv->v_type = VAR_FLOAT;
4756 rettv->vval.v_float = f1;
4757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004759#endif
4760 {
4761 if (op == '+')
4762 n1 = n1 + n2;
4763 else
4764 n1 = n1 - n2;
4765 rettv->v_type = VAR_NUMBER;
4766 rettv->vval.v_number = n1;
4767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771 }
4772 return OK;
4773}
4774
4775/*
4776 * Handle fifth level expression:
4777 * * number multiplication
4778 * / number division
4779 * % number modulo
4780 *
4781 * "arg" must point to the first non-white of the expression.
4782 * "arg" is advanced to the next non-white after the recognized expression.
4783 *
4784 * Return OK or FAIL.
4785 */
4786 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004787eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004791 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
Bram Moolenaar33570922005-01-25 22:26:29 +00004793 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 int op;
4795 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796#ifdef FEAT_FLOAT
4797 int use_float = FALSE;
4798 float_T f1 = 0, f2;
4799#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
4802 /*
4803 * Get the first variable.
4804 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004805 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return FAIL;
4807
4808 /*
4809 * Repeat computing, until no '*', '/' or '%' is following.
4810 */
4811 for (;;)
4812 {
4813 op = **arg;
4814 if (op != '*' && op != '/' && op != '%')
4815 break;
4816
4817 if (evaluate)
4818 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004819#ifdef FEAT_FLOAT
4820 if (rettv->v_type == VAR_FLOAT)
4821 {
4822 f1 = rettv->vval.v_float;
4823 use_float = TRUE;
4824 n1 = 0;
4825 }
4826 else
4827#endif
4828 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830 if (error)
4831 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 }
4833 else
4834 n1 = 0;
4835
4836 /*
4837 * Get the second variable.
4838 */
4839 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004840 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 return FAIL;
4842
4843 if (evaluate)
4844 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845#ifdef FEAT_FLOAT
4846 if (var2.v_type == VAR_FLOAT)
4847 {
4848 if (!use_float)
4849 {
4850 f1 = n1;
4851 use_float = TRUE;
4852 }
4853 f2 = var2.vval.v_float;
4854 n2 = 0;
4855 }
4856 else
4857#endif
4858 {
4859 n2 = get_tv_number_chk(&var2, &error);
4860 clear_tv(&var2);
4861 if (error)
4862 return FAIL;
4863#ifdef FEAT_FLOAT
4864 if (use_float)
4865 f2 = n2;
4866#endif
4867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
4869 /*
4870 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004871 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873#ifdef FEAT_FLOAT
4874 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876 if (op == '*')
4877 f1 = f1 * f2;
4878 else if (op == '/')
4879 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004880# ifdef VMS
4881 /* VMS crashes on divide by zero, work around it */
4882 if (f2 == 0.0)
4883 {
4884 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004885 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004886 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004887 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004888 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004889 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004890 }
4891 else
4892 f1 = f1 / f2;
4893# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894 /* We rely on the floating point library to handle divide
4895 * by zero to result in "inf" and not a crash. */
4896 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004897# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004900 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004901 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004902 return FAIL;
4903 }
4904 rettv->v_type = VAR_FLOAT;
4905 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910 if (op == '*')
4911 n1 = n1 * n2;
4912 else if (op == '/')
4913 {
4914 if (n2 == 0) /* give an error message? */
4915 {
4916 if (n1 == 0)
4917 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4918 else if (n1 < 0)
4919 n1 = -0x7fffffffL;
4920 else
4921 n1 = 0x7fffffffL;
4922 }
4923 else
4924 n1 = n1 / n2;
4925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927 {
4928 if (n2 == 0) /* give an error message? */
4929 n1 = 0;
4930 else
4931 n1 = n1 % n2;
4932 }
4933 rettv->v_type = VAR_NUMBER;
4934 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937 }
4938
4939 return OK;
4940}
4941
4942/*
4943 * Handle sixth level expression:
4944 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004945 * "string" string constant
4946 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 * &option-name option value
4948 * @r register contents
4949 * identifier variable value
4950 * function() function call
4951 * $VAR environment variable
4952 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004953 * [expr, expr] List
4954 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 *
4956 * Also handle:
4957 * ! in front logical NOT
4958 * - in front unary minus
4959 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 * trailing [] subscript in String or List
4961 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 *
4963 * "arg" must point to the first non-white of the expression.
4964 * "arg" is advanced to the next non-white after the recognized expression.
4965 *
4966 * Return OK or FAIL.
4967 */
4968 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004969eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004973 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 long n;
4976 int len;
4977 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 char_u *start_leader, *end_leader;
4979 int ret = OK;
4980 char_u *alias;
4981
4982 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004983 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004984 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004986 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987
4988 /*
4989 * Skip '!' and '-' characters. They are handled later.
4990 */
4991 start_leader = *arg;
4992 while (**arg == '!' || **arg == '-' || **arg == '+')
4993 *arg = skipwhite(*arg + 1);
4994 end_leader = *arg;
4995
4996 switch (**arg)
4997 {
4998 /*
4999 * Number constant.
5000 */
5001 case '0':
5002 case '1':
5003 case '2':
5004 case '3':
5005 case '4':
5006 case '5':
5007 case '6':
5008 case '7':
5009 case '8':
5010 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 {
5012#ifdef FEAT_FLOAT
5013 char_u *p = skipdigits(*arg + 1);
5014 int get_float = FALSE;
5015
5016 /* We accept a float when the format matches
5017 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005018 * strict to avoid backwards compatibility problems.
5019 * Don't look for a float after the "." operator, so that
5020 * ":let vers = 1.2.3" doesn't fail. */
5021 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005023 get_float = TRUE;
5024 p = skipdigits(p + 2);
5025 if (*p == 'e' || *p == 'E')
5026 {
5027 ++p;
5028 if (*p == '-' || *p == '+')
5029 ++p;
5030 if (!vim_isdigit(*p))
5031 get_float = FALSE;
5032 else
5033 p = skipdigits(p + 1);
5034 }
5035 if (ASCII_ISALPHA(*p) || *p == '.')
5036 get_float = FALSE;
5037 }
5038 if (get_float)
5039 {
5040 float_T f;
5041
5042 *arg += string2float(*arg, &f);
5043 if (evaluate)
5044 {
5045 rettv->v_type = VAR_FLOAT;
5046 rettv->vval.v_float = f;
5047 }
5048 }
5049 else
5050#endif
5051 {
5052 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5053 *arg += len;
5054 if (evaluate)
5055 {
5056 rettv->v_type = VAR_NUMBER;
5057 rettv->vval.v_number = n;
5058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
5063 /*
5064 * String constant: "string".
5065 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005066 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 break;
5068
5069 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005072 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005073 break;
5074
5075 /*
5076 * List: [expr, expr]
5077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 break;
5080
5081 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 * Dictionary: {key: val, key: val}
5083 */
5084 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5085 break;
5086
5087 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005088 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005090 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 break;
5092
5093 /*
5094 * Environment variable: $VAR.
5095 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005096 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098
5099 /*
5100 * Register contents: @r.
5101 */
5102 case '@': ++*arg;
5103 if (evaluate)
5104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005105 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005106 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 if (**arg != NUL)
5109 ++*arg;
5110 break;
5111
5112 /*
5113 * nested expression: (expression).
5114 */
5115 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005116 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (**arg == ')')
5118 ++*arg;
5119 else if (ret == OK)
5120 {
5121 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005122 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 ret = FAIL;
5124 }
5125 break;
5126
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 break;
5129 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130
5131 if (ret == NOTDONE)
5132 {
5133 /*
5134 * Must be a variable or function name.
5135 * Can also be a curly-braces kind of name: {expr}.
5136 */
5137 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005138 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 if (alias != NULL)
5140 s = alias;
5141
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005142 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 ret = FAIL;
5144 else
5145 {
5146 if (**arg == '(') /* recursive! */
5147 {
5148 /* If "s" is the name of a variable of type VAR_FUNC
5149 * use its contents. */
5150 s = deref_func_name(s, &len);
5151
5152 /* Invoke the function. */
5153 ret = get_func_tv(s, len, rettv, arg,
5154 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005155 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005156
5157 /* If evaluate is FALSE rettv->v_type was not set in
5158 * get_func_tv, but it's needed in handle_subscript() to parse
5159 * what follows. So set it here. */
5160 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5161 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005162 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005163 rettv->v_type = VAR_FUNC;
5164 }
5165
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 /* Stop the expression evaluation when immediately
5167 * aborting on error, or when an interrupt occurred or
5168 * an exception was thrown but not caught. */
5169 if (aborting())
5170 {
5171 if (ret == OK)
5172 clear_tv(rettv);
5173 ret = FAIL;
5174 }
5175 }
5176 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005177 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005178 else
5179 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005181 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 }
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 *arg = skipwhite(*arg);
5185
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005186 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5187 * expr(expr). */
5188 if (ret == OK)
5189 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190
5191 /*
5192 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5193 */
5194 if (ret == OK && evaluate && end_leader > start_leader)
5195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005196 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005197 int val = 0;
5198#ifdef FEAT_FLOAT
5199 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005200
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005201 if (rettv->v_type == VAR_FLOAT)
5202 f = rettv->vval.v_float;
5203 else
5204#endif
5205 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 clear_tv(rettv);
5209 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211 else
5212 {
5213 while (end_leader > start_leader)
5214 {
5215 --end_leader;
5216 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005217 {
5218#ifdef FEAT_FLOAT
5219 if (rettv->v_type == VAR_FLOAT)
5220 f = !f;
5221 else
5222#endif
5223 val = !val;
5224 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005225 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005226 {
5227#ifdef FEAT_FLOAT
5228 if (rettv->v_type == VAR_FLOAT)
5229 f = -f;
5230 else
5231#endif
5232 val = -val;
5233 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005234 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005235#ifdef FEAT_FLOAT
5236 if (rettv->v_type == VAR_FLOAT)
5237 {
5238 clear_tv(rettv);
5239 rettv->vval.v_float = f;
5240 }
5241 else
5242#endif
5243 {
5244 clear_tv(rettv);
5245 rettv->v_type = VAR_NUMBER;
5246 rettv->vval.v_number = val;
5247 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250
5251 return ret;
5252}
5253
5254/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005255 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5256 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5258 */
5259 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005260eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005262 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005264 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265{
5266 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005267 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 long len = -1;
5270 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005274 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005276 if (verbose)
5277 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 return FAIL;
5279 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005280#ifdef FEAT_FLOAT
5281 else if (rettv->v_type == VAR_FLOAT)
5282 {
5283 if (verbose)
5284 EMSG(_(e_float_as_string));
5285 return FAIL;
5286 }
5287#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 /*
5292 * dict.name
5293 */
5294 key = *arg + 1;
5295 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5296 ;
5297 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005299 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 }
5301 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 /*
5304 * something[idx]
5305 *
5306 * Get the (first) variable from inside the [].
5307 */
5308 *arg = skipwhite(*arg + 1);
5309 if (**arg == ':')
5310 empty1 = TRUE;
5311 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5312 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5314 {
5315 /* not a number or string */
5316 clear_tv(&var1);
5317 return FAIL;
5318 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005319
5320 /*
5321 * Get the second variable from inside the [:].
5322 */
5323 if (**arg == ':')
5324 {
5325 range = TRUE;
5326 *arg = skipwhite(*arg + 1);
5327 if (**arg == ']')
5328 empty2 = TRUE;
5329 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 if (!empty1)
5332 clear_tv(&var1);
5333 return FAIL;
5334 }
5335 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5336 {
5337 /* not a number or string */
5338 if (!empty1)
5339 clear_tv(&var1);
5340 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 return FAIL;
5342 }
5343 }
5344
5345 /* Check for the ']'. */
5346 if (**arg != ']')
5347 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005348 if (verbose)
5349 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005350 clear_tv(&var1);
5351 if (range)
5352 clear_tv(&var2);
5353 return FAIL;
5354 }
5355 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 }
5357
5358 if (evaluate)
5359 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005360 n1 = 0;
5361 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005363 n1 = get_tv_number(&var1);
5364 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 }
5366 if (range)
5367 {
5368 if (empty2)
5369 n2 = -1;
5370 else
5371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 n2 = get_tv_number(&var2);
5373 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 }
5375 }
5376
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005377 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 {
5379 case VAR_NUMBER:
5380 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005381 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 len = (long)STRLEN(s);
5383 if (range)
5384 {
5385 /* The resulting variable is a substring. If the indexes
5386 * are out of range the result is empty. */
5387 if (n1 < 0)
5388 {
5389 n1 = len + n1;
5390 if (n1 < 0)
5391 n1 = 0;
5392 }
5393 if (n2 < 0)
5394 n2 = len + n2;
5395 else if (n2 >= len)
5396 n2 = len;
5397 if (n1 >= len || n2 < 0 || n1 > n2)
5398 s = NULL;
5399 else
5400 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5401 }
5402 else
5403 {
5404 /* The resulting variable is a string of a single
5405 * character. If the index is too big or negative the
5406 * result is empty. */
5407 if (n1 >= len || n1 < 0)
5408 s = NULL;
5409 else
5410 s = vim_strnsave(s + n1, 1);
5411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005412 clear_tv(rettv);
5413 rettv->v_type = VAR_STRING;
5414 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 break;
5416
5417 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419 if (n1 < 0)
5420 n1 = len + n1;
5421 if (!empty1 && (n1 < 0 || n1 >= len))
5422 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005423 /* For a range we allow invalid values and return an empty
5424 * list. A list index out of range is an error. */
5425 if (!range)
5426 {
5427 if (verbose)
5428 EMSGN(_(e_listidx), n1);
5429 return FAIL;
5430 }
5431 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 }
5433 if (range)
5434 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005435 list_T *l;
5436 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437
5438 if (n2 < 0)
5439 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005440 else if (n2 >= len)
5441 n2 = len - 1;
5442 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005443 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 l = list_alloc();
5445 if (l == NULL)
5446 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 n1 <= n2; ++n1)
5449 {
5450 if (list_append_tv(l, &item->li_tv) == FAIL)
5451 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005452 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 return FAIL;
5454 }
5455 item = item->li_next;
5456 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 clear_tv(rettv);
5458 rettv->v_type = VAR_LIST;
5459 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005460 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 }
5462 else
5463 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005464 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 clear_tv(rettv);
5466 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 }
5468 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469
5470 case VAR_DICT:
5471 if (range)
5472 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005473 if (verbose)
5474 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475 if (len == -1)
5476 clear_tv(&var1);
5477 return FAIL;
5478 }
5479 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005480 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481
5482 if (len == -1)
5483 {
5484 key = get_tv_string(&var1);
5485 if (*key == NUL)
5486 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005487 if (verbose)
5488 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 clear_tv(&var1);
5490 return FAIL;
5491 }
5492 }
5493
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005494 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005495
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005496 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005497 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 if (len == -1)
5499 clear_tv(&var1);
5500 if (item == NULL)
5501 return FAIL;
5502
5503 copy_tv(&item->di_tv, &var1);
5504 clear_tv(rettv);
5505 *rettv = var1;
5506 }
5507 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 }
5509 }
5510
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511 return OK;
5512}
5513
5514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 * Get an option value.
5516 * "arg" points to the '&' or '+' before the option name.
5517 * "arg" is advanced to character after the option name.
5518 * Return OK or FAIL.
5519 */
5520 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005523 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 int evaluate;
5525{
5526 char_u *option_end;
5527 long numval;
5528 char_u *stringval;
5529 int opt_type;
5530 int c;
5531 int working = (**arg == '+'); /* has("+option") */
5532 int ret = OK;
5533 int opt_flags;
5534
5535 /*
5536 * Isolate the option name and find its value.
5537 */
5538 option_end = find_option_end(arg, &opt_flags);
5539 if (option_end == NULL)
5540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 EMSG2(_("E112: Option name missing: %s"), *arg);
5543 return FAIL;
5544 }
5545
5546 if (!evaluate)
5547 {
5548 *arg = option_end;
5549 return OK;
5550 }
5551
5552 c = *option_end;
5553 *option_end = NUL;
5554 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
5557 if (opt_type == -3) /* invalid name */
5558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 EMSG2(_("E113: Unknown option: %s"), *arg);
5561 ret = FAIL;
5562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 {
5565 if (opt_type == -2) /* hidden string option */
5566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 rettv->v_type = VAR_STRING;
5568 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570 else if (opt_type == -1) /* hidden number option */
5571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 rettv->v_type = VAR_NUMBER;
5573 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
5575 else if (opt_type == 1) /* number option */
5576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 rettv->v_type = VAR_NUMBER;
5578 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
5580 else /* string option */
5581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005582 rettv->v_type = VAR_STRING;
5583 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
5585 }
5586 else if (working && (opt_type == -2 || opt_type == -1))
5587 ret = FAIL;
5588
5589 *option_end = c; /* put back for error messages */
5590 *arg = option_end;
5591
5592 return ret;
5593}
5594
5595/*
5596 * Allocate a variable for a string constant.
5597 * Return OK or FAIL.
5598 */
5599 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005600get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 int evaluate;
5604{
5605 char_u *p;
5606 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 int extra = 0;
5608
5609 /*
5610 * Find the end of the string, skipping backslashed characters.
5611 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005612 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 {
5614 if (*p == '\\' && p[1] != NUL)
5615 {
5616 ++p;
5617 /* A "\<x>" form occupies at least 4 characters, and produces up
5618 * to 6 characters: reserve space for 2 extra */
5619 if (*p == '<')
5620 extra += 2;
5621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 }
5623
5624 if (*p != '"')
5625 {
5626 EMSG2(_("E114: Missing quote: %s"), *arg);
5627 return FAIL;
5628 }
5629
5630 /* If only parsing, set *arg and return here */
5631 if (!evaluate)
5632 {
5633 *arg = p + 1;
5634 return OK;
5635 }
5636
5637 /*
5638 * Copy the string into allocated memory, handling backslashed
5639 * characters.
5640 */
5641 name = alloc((unsigned)(p - *arg + extra));
5642 if (name == NULL)
5643 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 rettv->v_type = VAR_STRING;
5645 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
5649 if (*p == '\\')
5650 {
5651 switch (*++p)
5652 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005653 case 'b': *name++ = BS; ++p; break;
5654 case 'e': *name++ = ESC; ++p; break;
5655 case 'f': *name++ = FF; ++p; break;
5656 case 'n': *name++ = NL; ++p; break;
5657 case 'r': *name++ = CAR; ++p; break;
5658 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
5660 case 'X': /* hex: "\x1", "\x12" */
5661 case 'x':
5662 case 'u': /* Unicode: "\u0023" */
5663 case 'U':
5664 if (vim_isxdigit(p[1]))
5665 {
5666 int n, nr;
5667 int c = toupper(*p);
5668
5669 if (c == 'X')
5670 n = 2;
5671 else
5672 n = 4;
5673 nr = 0;
5674 while (--n >= 0 && vim_isxdigit(p[1]))
5675 {
5676 ++p;
5677 nr = (nr << 4) + hex2nr(*p);
5678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680#ifdef FEAT_MBYTE
5681 /* For "\u" store the number according to
5682 * 'encoding'. */
5683 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 else
5686#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005687 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 break;
5690
5691 /* octal: "\1", "\12", "\123" */
5692 case '0':
5693 case '1':
5694 case '2':
5695 case '3':
5696 case '4':
5697 case '5':
5698 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 case '7': *name = *p++ - '0';
5700 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 *name = (*name << 3) + *p++ - '0';
5703 if (*p >= '0' && *p <= '7')
5704 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 break;
5708
5709 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 if (extra != 0)
5712 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 break;
5715 }
5716 /* FALLTHROUGH */
5717
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 break;
5720 }
5721 }
5722 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 *arg = p + 1;
5728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 return OK;
5730}
5731
5732/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 * Return OK or FAIL.
5735 */
5736 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005737get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 int evaluate;
5741{
5742 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005744 int reduce = 0;
5745
5746 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005748 */
5749 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5750 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 break;
5755 ++reduce;
5756 ++p;
5757 }
5758 }
5759
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005763 return FAIL;
5764 }
5765
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005767 if (!evaluate)
5768 {
5769 *arg = p + 1;
5770 return OK;
5771 }
5772
5773 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 */
5776 str = alloc((unsigned)((p - *arg) - reduce));
5777 if (str == NULL)
5778 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 rettv->v_type = VAR_STRING;
5780 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005787 break;
5788 ++p;
5789 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005791 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793 *arg = p + 1;
5794
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 return OK;
5796}
5797
5798/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 * Allocate a variable for a List and fill it from "*arg".
5800 * Return OK or FAIL.
5801 */
5802 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005803get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005805 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 int evaluate;
5807{
Bram Moolenaar33570922005-01-25 22:26:29 +00005808 list_T *l = NULL;
5809 typval_T tv;
5810 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811
5812 if (evaluate)
5813 {
5814 l = list_alloc();
5815 if (l == NULL)
5816 return FAIL;
5817 }
5818
5819 *arg = skipwhite(*arg + 1);
5820 while (**arg != ']' && **arg != NUL)
5821 {
5822 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5823 goto failret;
5824 if (evaluate)
5825 {
5826 item = listitem_alloc();
5827 if (item != NULL)
5828 {
5829 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005830 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831 list_append(l, item);
5832 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005833 else
5834 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 }
5836
5837 if (**arg == ']')
5838 break;
5839 if (**arg != ',')
5840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 goto failret;
5843 }
5844 *arg = skipwhite(*arg + 1);
5845 }
5846
5847 if (**arg != ']')
5848 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850failret:
5851 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005852 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 return FAIL;
5854 }
5855
5856 *arg = skipwhite(*arg + 1);
5857 if (evaluate)
5858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005859 rettv->v_type = VAR_LIST;
5860 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861 ++l->lv_refcount;
5862 }
5863
5864 return OK;
5865}
5866
5867/*
5868 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005869 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005871 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872list_alloc()
5873{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005874 list_T *l;
5875
5876 l = (list_T *)alloc_clear(sizeof(list_T));
5877 if (l != NULL)
5878 {
5879 /* Prepend the list to the list of lists for garbage collection. */
5880 if (first_list != NULL)
5881 first_list->lv_used_prev = l;
5882 l->lv_used_prev = NULL;
5883 l->lv_used_next = first_list;
5884 first_list = l;
5885 }
5886 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887}
5888
5889/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005890 * Allocate an empty list for a return value.
5891 * Returns OK or FAIL.
5892 */
5893 static int
5894rettv_list_alloc(rettv)
5895 typval_T *rettv;
5896{
5897 list_T *l = list_alloc();
5898
5899 if (l == NULL)
5900 return FAIL;
5901
5902 rettv->vval.v_list = l;
5903 rettv->v_type = VAR_LIST;
5904 ++l->lv_refcount;
5905 return OK;
5906}
5907
5908/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909 * Unreference a list: decrement the reference count and free it when it
5910 * becomes zero.
5911 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005912 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005916 if (l != NULL && --l->lv_refcount <= 0)
5917 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918}
5919
5920/*
5921 * Free a list, including all items it points to.
5922 * Ignores the reference count.
5923 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005924 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005925list_free(l, recurse)
5926 list_T *l;
5927 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928{
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005931 /* Remove the list from the list of lists for garbage collection. */
5932 if (l->lv_used_prev == NULL)
5933 first_list = l->lv_used_next;
5934 else
5935 l->lv_used_prev->lv_used_next = l->lv_used_next;
5936 if (l->lv_used_next != NULL)
5937 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5938
Bram Moolenaard9fba312005-06-26 22:34:35 +00005939 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005941 /* Remove the item before deleting it. */
5942 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005943 if (recurse || (item->li_tv.v_type != VAR_LIST
5944 && item->li_tv.v_type != VAR_DICT))
5945 clear_tv(&item->li_tv);
5946 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 }
5948 vim_free(l);
5949}
5950
5951/*
5952 * Allocate a list item.
5953 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005954 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955listitem_alloc()
5956{
Bram Moolenaar33570922005-01-25 22:26:29 +00005957 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958}
5959
5960/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005961 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005963 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005965 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005967 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 vim_free(item);
5969}
5970
5971/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005972 * Remove a list item from a List and free it. Also clears the value.
5973 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005974 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005975listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005976 list_T *l;
5977 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005978{
5979 list_remove(l, item, item);
5980 listitem_free(item);
5981}
5982
5983/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 * Get the number of items in a list.
5985 */
5986 static long
5987list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 if (l == NULL)
5991 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005992 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993}
5994
5995/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005996 * Return TRUE when two lists have exactly the same values.
5997 */
5998 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005999list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 list_T *l1;
6001 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006003 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006004{
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006006
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006007 if (l1 == NULL || l2 == NULL)
6008 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006009 if (l1 == l2)
6010 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006011 if (list_len(l1) != list_len(l2))
6012 return FALSE;
6013
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014 for (item1 = l1->lv_first, item2 = l2->lv_first;
6015 item1 != NULL && item2 != NULL;
6016 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006017 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018 return FALSE;
6019 return item1 == NULL && item2 == NULL;
6020}
6021
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006022#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6023 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006024/*
6025 * Return the dictitem that an entry in a hashtable points to.
6026 */
6027 dictitem_T *
6028dict_lookup(hi)
6029 hashitem_T *hi;
6030{
6031 return HI2DI(hi);
6032}
6033#endif
6034
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006035/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006036 * Return TRUE when two dictionaries have exactly the same key/values.
6037 */
6038 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006039dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006040 dict_T *d1;
6041 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006042 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006043 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006044{
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 hashitem_T *hi;
6046 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006047 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006048
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006049 if (d1 == NULL || d2 == NULL)
6050 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006051 if (d1 == d2)
6052 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006053 if (dict_len(d1) != dict_len(d2))
6054 return FALSE;
6055
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006056 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006058 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006059 if (!HASHITEM_EMPTY(hi))
6060 {
6061 item2 = dict_find(d2, hi->hi_key, -1);
6062 if (item2 == NULL)
6063 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006064 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006065 return FALSE;
6066 --todo;
6067 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006068 }
6069 return TRUE;
6070}
6071
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006072static int tv_equal_recurse_limit;
6073
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006074/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006075 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006076 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006077 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006078 */
6079 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006080tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 typval_T *tv1;
6082 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006083 int ic; /* ignore case */
6084 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006085{
6086 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006087 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006088 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006089 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006091 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006092 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006093
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006094 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 * recursiveness to a limit. We guess they are equal then.
6096 * A fixed limit has the problem of still taking an awful long time.
6097 * Reduce the limit every time running into it. That should work fine for
6098 * deeply linked structures that are not recursively linked and catch
6099 * recursiveness quickly. */
6100 if (!recursive)
6101 tv_equal_recurse_limit = 1000;
6102 if (recursive_cnt >= tv_equal_recurse_limit)
6103 {
6104 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006105 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006106 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006107
6108 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006110 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111 ++recursive_cnt;
6112 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6113 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006114 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006115
6116 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 ++recursive_cnt;
6118 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6119 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006120 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006121
6122 case VAR_FUNC:
6123 return (tv1->vval.v_string != NULL
6124 && tv2->vval.v_string != NULL
6125 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6126
6127 case VAR_NUMBER:
6128 return tv1->vval.v_number == tv2->vval.v_number;
6129
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006130#ifdef FEAT_FLOAT
6131 case VAR_FLOAT:
6132 return tv1->vval.v_float == tv2->vval.v_float;
6133#endif
6134
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006135 case VAR_STRING:
6136 s1 = get_tv_string_buf(tv1, buf1);
6137 s2 = get_tv_string_buf(tv2, buf2);
6138 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006139 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006140
6141 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006142 return TRUE;
6143}
6144
6145/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146 * Locate item with index "n" in list "l" and return it.
6147 * A negative index is counted from the end; -1 is the last item.
6148 * Returns NULL when "n" is out of range.
6149 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006150 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 long n;
6154{
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006156 long idx;
6157
6158 if (l == NULL)
6159 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006160
6161 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006162 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006163 n = l->lv_len + n;
6164
6165 /* Check for index out of range. */
6166 if (n < 0 || n >= l->lv_len)
6167 return NULL;
6168
6169 /* When there is a cached index may start search from there. */
6170 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006171 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006172 if (n < l->lv_idx / 2)
6173 {
6174 /* closest to the start of the list */
6175 item = l->lv_first;
6176 idx = 0;
6177 }
6178 else if (n > (l->lv_idx + l->lv_len) / 2)
6179 {
6180 /* closest to the end of the list */
6181 item = l->lv_last;
6182 idx = l->lv_len - 1;
6183 }
6184 else
6185 {
6186 /* closest to the cached index */
6187 item = l->lv_idx_item;
6188 idx = l->lv_idx;
6189 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006190 }
6191 else
6192 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006193 if (n < l->lv_len / 2)
6194 {
6195 /* closest to the start of the list */
6196 item = l->lv_first;
6197 idx = 0;
6198 }
6199 else
6200 {
6201 /* closest to the end of the list */
6202 item = l->lv_last;
6203 idx = l->lv_len - 1;
6204 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006205 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006206
6207 while (n > idx)
6208 {
6209 /* search forward */
6210 item = item->li_next;
6211 ++idx;
6212 }
6213 while (n < idx)
6214 {
6215 /* search backward */
6216 item = item->li_prev;
6217 --idx;
6218 }
6219
6220 /* cache the used index */
6221 l->lv_idx = idx;
6222 l->lv_idx_item = item;
6223
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224 return item;
6225}
6226
6227/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006228 * Get list item "l[idx]" as a number.
6229 */
6230 static long
6231list_find_nr(l, idx, errorp)
6232 list_T *l;
6233 long idx;
6234 int *errorp; /* set to TRUE when something wrong */
6235{
6236 listitem_T *li;
6237
6238 li = list_find(l, idx);
6239 if (li == NULL)
6240 {
6241 if (errorp != NULL)
6242 *errorp = TRUE;
6243 return -1L;
6244 }
6245 return get_tv_number_chk(&li->li_tv, errorp);
6246}
6247
6248/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006249 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6250 */
6251 char_u *
6252list_find_str(l, idx)
6253 list_T *l;
6254 long idx;
6255{
6256 listitem_T *li;
6257
6258 li = list_find(l, idx - 1);
6259 if (li == NULL)
6260 {
6261 EMSGN(_(e_listidx), idx);
6262 return NULL;
6263 }
6264 return get_tv_string(&li->li_tv);
6265}
6266
6267/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006268 * Locate "item" list "l" and return its index.
6269 * Returns -1 when "item" is not in the list.
6270 */
6271 static long
6272list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006273 list_T *l;
6274 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006275{
6276 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006277 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006278
6279 if (l == NULL)
6280 return -1;
6281 idx = 0;
6282 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6283 ++idx;
6284 if (li == NULL)
6285 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006286 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006287}
6288
6289/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290 * Append item "item" to the end of list "l".
6291 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006292 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006294 list_T *l;
6295 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296{
6297 if (l->lv_last == NULL)
6298 {
6299 /* empty list */
6300 l->lv_first = item;
6301 l->lv_last = item;
6302 item->li_prev = NULL;
6303 }
6304 else
6305 {
6306 l->lv_last->li_next = item;
6307 item->li_prev = l->lv_last;
6308 l->lv_last = item;
6309 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006310 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311 item->li_next = NULL;
6312}
6313
6314/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006315 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006316 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006318 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 list_T *l;
6321 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006323 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324
Bram Moolenaar05159a02005-02-26 23:04:13 +00006325 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006327 copy_tv(tv, &li->li_tv);
6328 list_append(l, li);
6329 return OK;
6330}
6331
6332/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006333 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006334 * Return FAIL when out of memory.
6335 */
6336 int
6337list_append_dict(list, dict)
6338 list_T *list;
6339 dict_T *dict;
6340{
6341 listitem_T *li = listitem_alloc();
6342
6343 if (li == NULL)
6344 return FAIL;
6345 li->li_tv.v_type = VAR_DICT;
6346 li->li_tv.v_lock = 0;
6347 li->li_tv.vval.v_dict = dict;
6348 list_append(list, li);
6349 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006350 return OK;
6351}
6352
6353/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006354 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006355 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006356 * Returns FAIL when out of memory.
6357 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006358 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006359list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006360 list_T *l;
6361 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006362 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006363{
6364 listitem_T *li = listitem_alloc();
6365
6366 if (li == NULL)
6367 return FAIL;
6368 list_append(l, li);
6369 li->li_tv.v_type = VAR_STRING;
6370 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006371 if (str == NULL)
6372 li->li_tv.vval.v_string = NULL;
6373 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006374 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006375 return FAIL;
6376 return OK;
6377}
6378
6379/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006380 * Append "n" to list "l".
6381 * Returns FAIL when out of memory.
6382 */
6383 static int
6384list_append_number(l, n)
6385 list_T *l;
6386 varnumber_T n;
6387{
6388 listitem_T *li;
6389
6390 li = listitem_alloc();
6391 if (li == NULL)
6392 return FAIL;
6393 li->li_tv.v_type = VAR_NUMBER;
6394 li->li_tv.v_lock = 0;
6395 li->li_tv.vval.v_number = n;
6396 list_append(l, li);
6397 return OK;
6398}
6399
6400/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402 * If "item" is NULL append at the end.
6403 * Return FAIL when out of memory.
6404 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006405 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006406list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 list_T *l;
6408 typval_T *tv;
6409 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006410{
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006412
6413 if (ni == NULL)
6414 return FAIL;
6415 copy_tv(tv, &ni->li_tv);
6416 if (item == NULL)
6417 /* Append new item at end of list. */
6418 list_append(l, ni);
6419 else
6420 {
6421 /* Insert new item before existing item. */
6422 ni->li_prev = item->li_prev;
6423 ni->li_next = item;
6424 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006425 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006427 ++l->lv_idx;
6428 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006430 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006432 l->lv_idx_item = NULL;
6433 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006435 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436 }
6437 return OK;
6438}
6439
6440/*
6441 * Extend "l1" with "l2".
6442 * If "bef" is NULL append at the end, otherwise insert before this item.
6443 * Returns FAIL when out of memory.
6444 */
6445 static int
6446list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006447 list_T *l1;
6448 list_T *l2;
6449 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006450{
Bram Moolenaar33570922005-01-25 22:26:29 +00006451 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006452 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006454 /* We also quit the loop when we have inserted the original item count of
6455 * the list, avoid a hang when we extend a list with itself. */
6456 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6458 return FAIL;
6459 return OK;
6460}
6461
6462/*
6463 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6464 * Return FAIL when out of memory.
6465 */
6466 static int
6467list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 list_T *l1;
6469 list_T *l2;
6470 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006471{
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006473
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006474 if (l1 == NULL || l2 == NULL)
6475 return FAIL;
6476
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006477 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006478 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479 if (l == NULL)
6480 return FAIL;
6481 tv->v_type = VAR_LIST;
6482 tv->vval.v_list = l;
6483
6484 /* append all items from the second list */
6485 return list_extend(l, l2, NULL);
6486}
6487
6488/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006489 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006491 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492 * Returns NULL when out of memory.
6493 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006495list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006496 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006498 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499{
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 list_T *copy;
6501 listitem_T *item;
6502 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503
6504 if (orig == NULL)
6505 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506
6507 copy = list_alloc();
6508 if (copy != NULL)
6509 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006510 if (copyID != 0)
6511 {
6512 /* Do this before adding the items, because one of the items may
6513 * refer back to this list. */
6514 orig->lv_copyID = copyID;
6515 orig->lv_copylist = copy;
6516 }
6517 for (item = orig->lv_first; item != NULL && !got_int;
6518 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519 {
6520 ni = listitem_alloc();
6521 if (ni == NULL)
6522 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006523 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006524 {
6525 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6526 {
6527 vim_free(ni);
6528 break;
6529 }
6530 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006532 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533 list_append(copy, ni);
6534 }
6535 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006536 if (item != NULL)
6537 {
6538 list_unref(copy);
6539 copy = NULL;
6540 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006541 }
6542
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006543 return copy;
6544}
6545
6546/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006548 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006549 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006550 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006551list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006552 list_T *l;
6553 listitem_T *item;
6554 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555{
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558 /* notify watchers */
6559 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006561 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562 list_fix_watch(l, ip);
6563 if (ip == item2)
6564 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566
6567 if (item2->li_next == NULL)
6568 l->lv_last = item->li_prev;
6569 else
6570 item2->li_next->li_prev = item->li_prev;
6571 if (item->li_prev == NULL)
6572 l->lv_first = item2->li_next;
6573 else
6574 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006575 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576}
6577
6578/*
6579 * Return an allocated string with the string representation of a list.
6580 * May return NULL.
6581 */
6582 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006583list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006585 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586{
6587 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588
6589 if (tv->vval.v_list == NULL)
6590 return NULL;
6591 ga_init2(&ga, (int)sizeof(char), 80);
6592 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006593 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 {
6595 vim_free(ga.ga_data);
6596 return NULL;
6597 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 ga_append(&ga, ']');
6599 ga_append(&ga, NUL);
6600 return (char_u *)ga.ga_data;
6601}
6602
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006603typedef struct join_S {
6604 char_u *s;
6605 char_u *tofree;
6606} join_T;
6607
6608 static int
6609list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6610 garray_T *gap; /* to store the result in */
6611 list_T *l;
6612 char_u *sep;
6613 int echo_style;
6614 int copyID;
6615 garray_T *join_gap; /* to keep each list item string */
6616{
6617 int i;
6618 join_T *p;
6619 int len;
6620 int sumlen = 0;
6621 int first = TRUE;
6622 char_u *tofree;
6623 char_u numbuf[NUMBUFLEN];
6624 listitem_T *item;
6625 char_u *s;
6626
6627 /* Stringify each item in the list. */
6628 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6629 {
6630 if (echo_style)
6631 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6632 else
6633 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6634 if (s == NULL)
6635 return FAIL;
6636
6637 len = (int)STRLEN(s);
6638 sumlen += len;
6639
6640 ga_grow(join_gap, 1);
6641 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6642 if (tofree != NULL || s != numbuf)
6643 {
6644 p->s = s;
6645 p->tofree = tofree;
6646 }
6647 else
6648 {
6649 p->s = vim_strnsave(s, len);
6650 p->tofree = p->s;
6651 }
6652
6653 line_breakcheck();
6654 }
6655
6656 /* Allocate result buffer with its total size, avoid re-allocation and
6657 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6658 if (join_gap->ga_len >= 2)
6659 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6660 if (ga_grow(gap, sumlen + 2) == FAIL)
6661 return FAIL;
6662
6663 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6664 {
6665 if (first)
6666 first = FALSE;
6667 else
6668 ga_concat(gap, sep);
6669 p = ((join_T *)join_gap->ga_data) + i;
6670
6671 if (p->s != NULL)
6672 ga_concat(gap, p->s);
6673 line_breakcheck();
6674 }
6675
6676 return OK;
6677}
6678
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006680 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006681 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006682 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006683 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006684 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006685list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006686 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006687 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006688 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006689 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006690 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006691{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006692 garray_T join_ga;
6693 int retval;
6694 join_T *p;
6695 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006696
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006697 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6698 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6699
6700 /* Dispose each item in join_ga. */
6701 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006702 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006703 p = (join_T *)join_ga.ga_data;
6704 for (i = 0; i < join_ga.ga_len; ++i)
6705 {
6706 vim_free(p->tofree);
6707 ++p;
6708 }
6709 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006710 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006711
6712 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006713}
6714
6715/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006716 * Garbage collection for lists and dictionaries.
6717 *
6718 * We use reference counts to be able to free most items right away when they
6719 * are no longer used. But for composite items it's possible that it becomes
6720 * unused while the reference count is > 0: When there is a recursive
6721 * reference. Example:
6722 * :let l = [1, 2, 3]
6723 * :let d = {9: l}
6724 * :let l[1] = d
6725 *
6726 * Since this is quite unusual we handle this with garbage collection: every
6727 * once in a while find out which lists and dicts are not referenced from any
6728 * variable.
6729 *
6730 * Here is a good reference text about garbage collection (refers to Python
6731 * but it applies to all reference-counting mechanisms):
6732 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006733 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006734
6735/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 * Do garbage collection for lists and dicts.
6737 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006738 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006739 int
6740garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006741{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006742 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743 buf_T *buf;
6744 win_T *wp;
6745 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006746 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006747 int did_free;
6748 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006749#ifdef FEAT_WINDOWS
6750 tabpage_T *tp;
6751#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006752
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006753 /* Only do this once. */
6754 want_garbage_collect = FALSE;
6755 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006756 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006757
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006758 /* We advance by two because we add one for items referenced through
6759 * previous_funccal. */
6760 current_copyID += COPYID_INC;
6761 copyID = current_copyID;
6762
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006763 /*
6764 * 1. Go through all accessible variables and mark all lists and dicts
6765 * with copyID.
6766 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006767
6768 /* Don't free variables in the previous_funccal list unless they are only
6769 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006770 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006771 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6772 {
6773 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6774 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6775 }
6776
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006777 /* script-local variables */
6778 for (i = 1; i <= ga_scripts.ga_len; ++i)
6779 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6780
6781 /* buffer-local variables */
6782 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006783 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784
6785 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006786 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006787 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006788#ifdef FEAT_AUTOCMD
6789 if (aucmd_win != NULL)
6790 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6791#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006792
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006793#ifdef FEAT_WINDOWS
6794 /* tabpage-local variables */
6795 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006796 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006797#endif
6798
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006799 /* global variables */
6800 set_ref_in_ht(&globvarht, copyID);
6801
6802 /* function-local variables */
6803 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6804 {
6805 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6806 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6807 }
6808
Bram Moolenaard812df62008-11-09 12:46:09 +00006809 /* v: vars */
6810 set_ref_in_ht(&vimvarht, copyID);
6811
Bram Moolenaar1dced572012-04-05 16:54:08 +02006812#ifdef FEAT_LUA
6813 set_ref_in_lua(copyID);
6814#endif
6815
Bram Moolenaardb913952012-06-29 12:54:53 +02006816#ifdef FEAT_PYTHON
6817 set_ref_in_python(copyID);
6818#endif
6819
6820#ifdef FEAT_PYTHON3
6821 set_ref_in_python3(copyID);
6822#endif
6823
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006824 /*
6825 * 2. Free lists and dictionaries that are not referenced.
6826 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006827 did_free = free_unref_items(copyID);
6828
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006829 /*
6830 * 3. Check if any funccal can be freed now.
6831 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006832 for (pfc = &previous_funccal; *pfc != NULL; )
6833 {
6834 if (can_free_funccal(*pfc, copyID))
6835 {
6836 fc = *pfc;
6837 *pfc = fc->caller;
6838 free_funccal(fc, TRUE);
6839 did_free = TRUE;
6840 did_free_funccal = TRUE;
6841 }
6842 else
6843 pfc = &(*pfc)->caller;
6844 }
6845 if (did_free_funccal)
6846 /* When a funccal was freed some more items might be garbage
6847 * collected, so run again. */
6848 (void)garbage_collect();
6849
6850 return did_free;
6851}
6852
6853/*
6854 * Free lists and dictionaries that are no longer referenced.
6855 */
6856 static int
6857free_unref_items(copyID)
6858 int copyID;
6859{
6860 dict_T *dd;
6861 list_T *ll;
6862 int did_free = FALSE;
6863
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866 */
6867 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006868 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006870 /* Free the Dictionary and ordinary items it contains, but don't
6871 * recurse into Lists and Dictionaries, they will be in the list
6872 * of dicts or list of lists. */
6873 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874 did_free = TRUE;
6875
6876 /* restart, next dict may also have been freed */
6877 dd = first_dict;
6878 }
6879 else
6880 dd = dd->dv_used_next;
6881
6882 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883 * Go through the list of lists and free items without the copyID.
6884 * But don't free a list that has a watcher (used in a for loop), these
6885 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 */
6887 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006888 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6889 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006891 /* Free the List and ordinary items it contains, but don't recurse
6892 * into Lists and Dictionaries, they will be in the list of dicts
6893 * or list of lists. */
6894 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 did_free = TRUE;
6896
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006897 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 ll = first_list;
6899 }
6900 else
6901 ll = ll->lv_used_next;
6902
6903 return did_free;
6904}
6905
6906/*
6907 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6908 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006909 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910set_ref_in_ht(ht, copyID)
6911 hashtab_T *ht;
6912 int copyID;
6913{
6914 int todo;
6915 hashitem_T *hi;
6916
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006917 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006918 for (hi = ht->ht_array; todo > 0; ++hi)
6919 if (!HASHITEM_EMPTY(hi))
6920 {
6921 --todo;
6922 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6923 }
6924}
6925
6926/*
6927 * Mark all lists and dicts referenced through list "l" with "copyID".
6928 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006929 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930set_ref_in_list(l, copyID)
6931 list_T *l;
6932 int copyID;
6933{
6934 listitem_T *li;
6935
6936 for (li = l->lv_first; li != NULL; li = li->li_next)
6937 set_ref_in_item(&li->li_tv, copyID);
6938}
6939
6940/*
6941 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6942 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006943 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944set_ref_in_item(tv, copyID)
6945 typval_T *tv;
6946 int copyID;
6947{
6948 dict_T *dd;
6949 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006950
6951 switch (tv->v_type)
6952 {
6953 case VAR_DICT:
6954 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006955 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006956 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957 /* Didn't see this dict yet. */
6958 dd->dv_copyID = copyID;
6959 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006960 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006961 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006962
6963 case VAR_LIST:
6964 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006965 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006966 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 /* Didn't see this list yet. */
6968 ll->lv_copyID = copyID;
6969 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006970 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006971 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006972 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006974}
6975
6976/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006977 * Allocate an empty header for a dictionary.
6978 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006979 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006980dict_alloc()
6981{
Bram Moolenaar33570922005-01-25 22:26:29 +00006982 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006983
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006985 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006986 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006987 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 if (first_dict != NULL)
6989 first_dict->dv_used_prev = d;
6990 d->dv_used_next = first_dict;
6991 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006992 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993
Bram Moolenaar33570922005-01-25 22:26:29 +00006994 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006995 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006996 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006997 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006998 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006999 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007000 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007001}
7002
7003/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007004 * Allocate an empty dict for a return value.
7005 * Returns OK or FAIL.
7006 */
7007 static int
7008rettv_dict_alloc(rettv)
7009 typval_T *rettv;
7010{
7011 dict_T *d = dict_alloc();
7012
7013 if (d == NULL)
7014 return FAIL;
7015
7016 rettv->vval.v_dict = d;
7017 rettv->v_type = VAR_DICT;
7018 ++d->dv_refcount;
7019 return OK;
7020}
7021
7022
7023/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007024 * Unreference a Dictionary: decrement the reference count and free it when it
7025 * becomes zero.
7026 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007027 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007028dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007029 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007030{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007031 if (d != NULL && --d->dv_refcount <= 0)
7032 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033}
7034
7035/*
7036 * Free a Dictionary, including all items it contains.
7037 * Ignores the reference count.
7038 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007039 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007040dict_free(d, recurse)
7041 dict_T *d;
7042 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007043{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007044 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007045 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007046 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007047
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 /* Remove the dict from the list of dicts for garbage collection. */
7049 if (d->dv_used_prev == NULL)
7050 first_dict = d->dv_used_next;
7051 else
7052 d->dv_used_prev->dv_used_next = d->dv_used_next;
7053 if (d->dv_used_next != NULL)
7054 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7055
7056 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007057 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007058 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007059 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007060 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007061 if (!HASHITEM_EMPTY(hi))
7062 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007063 /* Remove the item before deleting it, just in case there is
7064 * something recursive causing trouble. */
7065 di = HI2DI(hi);
7066 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007067 if (recurse || (di->di_tv.v_type != VAR_LIST
7068 && di->di_tv.v_type != VAR_DICT))
7069 clear_tv(&di->di_tv);
7070 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007071 --todo;
7072 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007073 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007074 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007075 vim_free(d);
7076}
7077
7078/*
7079 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007080 * The "key" is copied to the new item.
7081 * Note that the value of the item "di_tv" still needs to be initialized!
7082 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007083 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007084 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007085dictitem_alloc(key)
7086 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007087{
Bram Moolenaar33570922005-01-25 22:26:29 +00007088 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007089
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007090 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007091 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007092 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007093 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007094 di->di_flags = 0;
7095 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007096 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007097}
7098
7099/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007100 * Make a copy of a Dictionary item.
7101 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007102 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007105{
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007107
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007108 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7109 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007110 if (di != NULL)
7111 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007113 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007114 copy_tv(&org->di_tv, &di->di_tv);
7115 }
7116 return di;
7117}
7118
7119/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120 * Remove item "item" from Dictionary "dict" and free it.
7121 */
7122 static void
7123dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 dict_T *dict;
7125 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007126{
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007128
Bram Moolenaar33570922005-01-25 22:26:29 +00007129 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007130 if (HASHITEM_EMPTY(hi))
7131 EMSG2(_(e_intern2), "dictitem_remove()");
7132 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007134 dictitem_free(item);
7135}
7136
7137/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007138 * Free a dict item. Also clears the value.
7139 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007140 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007141dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007142 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007143{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007144 clear_tv(&item->di_tv);
7145 vim_free(item);
7146}
7147
7148/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007149 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7150 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007151 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152 * Returns NULL when out of memory.
7153 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007155dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007156 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007158 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007159{
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 dict_T *copy;
7161 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007162 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007164
7165 if (orig == NULL)
7166 return NULL;
7167
7168 copy = dict_alloc();
7169 if (copy != NULL)
7170 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007171 if (copyID != 0)
7172 {
7173 orig->dv_copyID = copyID;
7174 orig->dv_copydict = copy;
7175 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007176 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007177 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007178 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007179 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007181 --todo;
7182
7183 di = dictitem_alloc(hi->hi_key);
7184 if (di == NULL)
7185 break;
7186 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007187 {
7188 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7189 copyID) == FAIL)
7190 {
7191 vim_free(di);
7192 break;
7193 }
7194 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 else
7196 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7197 if (dict_add(copy, di) == FAIL)
7198 {
7199 dictitem_free(di);
7200 break;
7201 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007204
Bram Moolenaare9a41262005-01-15 22:18:47 +00007205 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007206 if (todo > 0)
7207 {
7208 dict_unref(copy);
7209 copy = NULL;
7210 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211 }
7212
7213 return copy;
7214}
7215
7216/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007218 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007220 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007222 dict_T *d;
7223 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224{
Bram Moolenaar33570922005-01-25 22:26:29 +00007225 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007226}
7227
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007229 * Add a number or string entry to dictionary "d".
7230 * When "str" is NULL use number "nr", otherwise use "str".
7231 * Returns FAIL when out of memory and when key already exists.
7232 */
7233 int
7234dict_add_nr_str(d, key, nr, str)
7235 dict_T *d;
7236 char *key;
7237 long nr;
7238 char_u *str;
7239{
7240 dictitem_T *item;
7241
7242 item = dictitem_alloc((char_u *)key);
7243 if (item == NULL)
7244 return FAIL;
7245 item->di_tv.v_lock = 0;
7246 if (str == NULL)
7247 {
7248 item->di_tv.v_type = VAR_NUMBER;
7249 item->di_tv.vval.v_number = nr;
7250 }
7251 else
7252 {
7253 item->di_tv.v_type = VAR_STRING;
7254 item->di_tv.vval.v_string = vim_strsave(str);
7255 }
7256 if (dict_add(d, item) == FAIL)
7257 {
7258 dictitem_free(item);
7259 return FAIL;
7260 }
7261 return OK;
7262}
7263
7264/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007265 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007266 * Returns FAIL when out of memory and when key already exists.
7267 */
7268 int
7269dict_add_list(d, key, list)
7270 dict_T *d;
7271 char *key;
7272 list_T *list;
7273{
7274 dictitem_T *item;
7275
7276 item = dictitem_alloc((char_u *)key);
7277 if (item == NULL)
7278 return FAIL;
7279 item->di_tv.v_lock = 0;
7280 item->di_tv.v_type = VAR_LIST;
7281 item->di_tv.vval.v_list = list;
7282 if (dict_add(d, item) == FAIL)
7283 {
7284 dictitem_free(item);
7285 return FAIL;
7286 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007287 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007288 return OK;
7289}
7290
7291/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007292 * Get the number of items in a Dictionary.
7293 */
7294 static long
7295dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298 if (d == NULL)
7299 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007300 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301}
7302
7303/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 * Find item "key[len]" in Dictionary "d".
7305 * If "len" is negative use strlen(key).
7306 * Returns NULL when not found.
7307 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007308 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 char_u *key;
7312 int len;
7313{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314#define AKEYLEN 200
7315 char_u buf[AKEYLEN];
7316 char_u *akey;
7317 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007318 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007319
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 if (len < 0)
7321 akey = key;
7322 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007323 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 tofree = akey = vim_strnsave(key, len);
7325 if (akey == NULL)
7326 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007327 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 else
7329 {
7330 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007331 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007332 akey = buf;
7333 }
7334
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336 vim_free(tofree);
7337 if (HASHITEM_EMPTY(hi))
7338 return NULL;
7339 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340}
7341
7342/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007343 * Get a string item from a dictionary.
7344 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007345 * Returns NULL if the entry doesn't exist or out of memory.
7346 */
7347 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007348get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007349 dict_T *d;
7350 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007351 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007352{
7353 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007354 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007355
7356 di = dict_find(d, key, -1);
7357 if (di == NULL)
7358 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007359 s = get_tv_string(&di->di_tv);
7360 if (save && s != NULL)
7361 s = vim_strsave(s);
7362 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007363}
7364
7365/*
7366 * Get a number item from a dictionary.
7367 * Returns 0 if the entry doesn't exist or out of memory.
7368 */
7369 long
7370get_dict_number(d, key)
7371 dict_T *d;
7372 char_u *key;
7373{
7374 dictitem_T *di;
7375
7376 di = dict_find(d, key, -1);
7377 if (di == NULL)
7378 return 0;
7379 return get_tv_number(&di->di_tv);
7380}
7381
7382/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007383 * Return an allocated string with the string representation of a Dictionary.
7384 * May return NULL.
7385 */
7386 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007387dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007389 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390{
7391 garray_T ga;
7392 int first = TRUE;
7393 char_u *tofree;
7394 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007397 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007399
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401 return NULL;
7402 ga_init2(&ga, (int)sizeof(char), 80);
7403 ga_append(&ga, '{');
7404
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007405 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007406 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007408 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007410 --todo;
7411
7412 if (first)
7413 first = FALSE;
7414 else
7415 ga_concat(&ga, (char_u *)", ");
7416
7417 tofree = string_quote(hi->hi_key, FALSE);
7418 if (tofree != NULL)
7419 {
7420 ga_concat(&ga, tofree);
7421 vim_free(tofree);
7422 }
7423 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007424 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425 if (s != NULL)
7426 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007428 if (s == NULL)
7429 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007432 if (todo > 0)
7433 {
7434 vim_free(ga.ga_data);
7435 return NULL;
7436 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437
7438 ga_append(&ga, '}');
7439 ga_append(&ga, NUL);
7440 return (char_u *)ga.ga_data;
7441}
7442
7443/*
7444 * Allocate a variable for a Dictionary and fill it from "*arg".
7445 * Return OK or FAIL. Returns NOTDONE for {expr}.
7446 */
7447 static int
7448get_dict_tv(arg, rettv, evaluate)
7449 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 int evaluate;
7452{
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 dict_T *d = NULL;
7454 typval_T tvkey;
7455 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007456 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007457 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007459 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460
7461 /*
7462 * First check if it's not a curly-braces thing: {expr}.
7463 * Must do this without evaluating, otherwise a function may be called
7464 * twice. Unfortunately this means we need to call eval1() twice for the
7465 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007466 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 if (*start != '}')
7469 {
7470 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7471 return FAIL;
7472 if (*start == '}')
7473 return NOTDONE;
7474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475
7476 if (evaluate)
7477 {
7478 d = dict_alloc();
7479 if (d == NULL)
7480 return FAIL;
7481 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007482 tvkey.v_type = VAR_UNKNOWN;
7483 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484
7485 *arg = skipwhite(*arg + 1);
7486 while (**arg != '}' && **arg != NUL)
7487 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007488 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 goto failret;
7490 if (**arg != ':')
7491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007492 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007493 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 goto failret;
7495 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007496 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007498 key = get_tv_string_buf_chk(&tvkey, buf);
7499 if (key == NULL || *key == NUL)
7500 {
7501 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7502 if (key != NULL)
7503 EMSG(_(e_emptykey));
7504 clear_tv(&tvkey);
7505 goto failret;
7506 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007507 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508
7509 *arg = skipwhite(*arg + 1);
7510 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7511 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007512 if (evaluate)
7513 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 goto failret;
7515 }
7516 if (evaluate)
7517 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519 if (item != NULL)
7520 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007521 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007522 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 clear_tv(&tv);
7524 goto failret;
7525 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007526 item = dictitem_alloc(key);
7527 clear_tv(&tvkey);
7528 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007531 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 if (dict_add(d, item) == FAIL)
7533 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 }
7535 }
7536
7537 if (**arg == '}')
7538 break;
7539 if (**arg != ',')
7540 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007541 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542 goto failret;
7543 }
7544 *arg = skipwhite(*arg + 1);
7545 }
7546
7547 if (**arg != '}')
7548 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007549 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550failret:
7551 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007552 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007553 return FAIL;
7554 }
7555
7556 *arg = skipwhite(*arg + 1);
7557 if (evaluate)
7558 {
7559 rettv->v_type = VAR_DICT;
7560 rettv->vval.v_dict = d;
7561 ++d->dv_refcount;
7562 }
7563
7564 return OK;
7565}
7566
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007568 * Return a string with the string representation of a variable.
7569 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007570 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007571 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007572 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007573 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007574 */
7575 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007576echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007578 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007579 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007580 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007581{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007582 static int recurse = 0;
7583 char_u *r = NULL;
7584
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007586 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007587 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007588 *tofree = NULL;
7589 return NULL;
7590 }
7591 ++recurse;
7592
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007593 switch (tv->v_type)
7594 {
7595 case VAR_FUNC:
7596 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007597 r = tv->vval.v_string;
7598 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007599
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007600 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007601 if (tv->vval.v_list == NULL)
7602 {
7603 *tofree = NULL;
7604 r = NULL;
7605 }
7606 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7607 {
7608 *tofree = NULL;
7609 r = (char_u *)"[...]";
7610 }
7611 else
7612 {
7613 tv->vval.v_list->lv_copyID = copyID;
7614 *tofree = list2string(tv, copyID);
7615 r = *tofree;
7616 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007618
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620 if (tv->vval.v_dict == NULL)
7621 {
7622 *tofree = NULL;
7623 r = NULL;
7624 }
7625 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7626 {
7627 *tofree = NULL;
7628 r = (char_u *)"{...}";
7629 }
7630 else
7631 {
7632 tv->vval.v_dict->dv_copyID = copyID;
7633 *tofree = dict2string(tv, copyID);
7634 r = *tofree;
7635 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007636 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007638 case VAR_STRING:
7639 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007640 *tofree = NULL;
7641 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007642 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007643
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007644#ifdef FEAT_FLOAT
7645 case VAR_FLOAT:
7646 *tofree = NULL;
7647 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7648 r = numbuf;
7649 break;
7650#endif
7651
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007653 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007654 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007655 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007656
7657 --recurse;
7658 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007659}
7660
7661/*
7662 * Return a string with the string representation of a variable.
7663 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7664 * "numbuf" is used for a number.
7665 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007666 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007667 */
7668 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007669tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007671 char_u **tofree;
7672 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007673 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674{
7675 switch (tv->v_type)
7676 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007677 case VAR_FUNC:
7678 *tofree = string_quote(tv->vval.v_string, TRUE);
7679 return *tofree;
7680 case VAR_STRING:
7681 *tofree = string_quote(tv->vval.v_string, FALSE);
7682 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007683#ifdef FEAT_FLOAT
7684 case VAR_FLOAT:
7685 *tofree = NULL;
7686 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7687 return numbuf;
7688#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007689 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007692 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007693 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007694 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007695 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007696 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007697}
7698
7699/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 * Return string "str" in ' quotes, doubling ' characters.
7701 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007703 */
7704 static char_u *
7705string_quote(str, function)
7706 char_u *str;
7707 int function;
7708{
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007710 char_u *p, *r, *s;
7711
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 len = (function ? 13 : 3);
7713 if (str != NULL)
7714 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007715 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 for (p = str; *p != NUL; mb_ptr_adv(p))
7717 if (*p == '\'')
7718 ++len;
7719 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007720 s = r = alloc(len);
7721 if (r != NULL)
7722 {
7723 if (function)
7724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007726 r += 10;
7727 }
7728 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007730 if (str != NULL)
7731 for (p = str; *p != NUL; )
7732 {
7733 if (*p == '\'')
7734 *r++ = '\'';
7735 MB_COPY_CHAR(p, r);
7736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007738 if (function)
7739 *r++ = ')';
7740 *r++ = NUL;
7741 }
7742 return s;
7743}
7744
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007745#ifdef FEAT_FLOAT
7746/*
7747 * Convert the string "text" to a floating point number.
7748 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7749 * this always uses a decimal point.
7750 * Returns the length of the text that was consumed.
7751 */
7752 static int
7753string2float(text, value)
7754 char_u *text;
7755 float_T *value; /* result stored here */
7756{
7757 char *s = (char *)text;
7758 float_T f;
7759
7760 f = strtod(s, &s);
7761 *value = f;
7762 return (int)((char_u *)s - text);
7763}
7764#endif
7765
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 * Get the value of an environment variable.
7768 * "arg" is pointing to the '$'. It is advanced to after the name.
7769 * If the environment variable was not set, silently assume it is empty.
7770 * Always return OK.
7771 */
7772 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007773get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 int evaluate;
7777{
7778 char_u *string = NULL;
7779 int len;
7780 int cc;
7781 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007782 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783
7784 ++*arg;
7785 name = *arg;
7786 len = get_env_len(arg);
7787 if (evaluate)
7788 {
7789 if (len != 0)
7790 {
7791 cc = name[len];
7792 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007793 /* first try vim_getenv(), fast for normal environment vars */
7794 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007796 {
7797 if (!mustfree)
7798 string = vim_strsave(string);
7799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 else
7801 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007802 if (mustfree)
7803 vim_free(string);
7804
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 /* next try expanding things like $VIM and ${HOME} */
7806 string = expand_env_save(name - 1);
7807 if (string != NULL && *string == '$')
7808 {
7809 vim_free(string);
7810 string = NULL;
7811 }
7812 }
7813 name[len] = cc;
7814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007815 rettv->v_type = VAR_STRING;
7816 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 }
7818
7819 return OK;
7820}
7821
7822/*
7823 * Array with names and number of arguments of all internal functions
7824 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7825 */
7826static struct fst
7827{
7828 char *f_name; /* function name */
7829 char f_min_argc; /* minimal number of arguments */
7830 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007831 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007832 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833} functions[] =
7834{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007835#ifdef FEAT_FLOAT
7836 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007837 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007839 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007840 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {"append", 2, 2, f_append},
7842 {"argc", 0, 0, f_argc},
7843 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007844 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007845#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007846 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007847 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007848 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007851 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {"bufexists", 1, 1, f_bufexists},
7853 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7854 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7855 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7856 {"buflisted", 1, 1, f_buflisted},
7857 {"bufloaded", 1, 1, f_bufloaded},
7858 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007859 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {"bufwinnr", 1, 1, f_bufwinnr},
7861 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007862 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007863 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007864#ifdef FEAT_FLOAT
7865 {"ceil", 1, 1, f_ceil},
7866#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007867 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007868 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007870 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007872#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007873 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007874 {"complete_add", 1, 1, f_complete_add},
7875 {"complete_check", 0, 0, f_complete_check},
7876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007878 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007879#ifdef FEAT_FLOAT
7880 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007881 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007882#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007883 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007885 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007886 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"delete", 1, 1, f_delete},
7888 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007889 {"diff_filler", 1, 1, f_diff_filler},
7890 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007891 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"eventhandler", 0, 0, f_eventhandler},
7895 {"executable", 1, 1, f_executable},
7896 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007897#ifdef FEAT_FLOAT
7898 {"exp", 1, 1, f_exp},
7899#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007900 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007901 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007902 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7904 {"filereadable", 1, 1, f_filereadable},
7905 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007907 {"finddir", 1, 3, f_finddir},
7908 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007909#ifdef FEAT_FLOAT
7910 {"float2nr", 1, 1, f_float2nr},
7911 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007912 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007913#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007914 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"fnamemodify", 2, 2, f_fnamemodify},
7916 {"foldclosed", 1, 1, f_foldclosed},
7917 {"foldclosedend", 1, 1, f_foldclosedend},
7918 {"foldlevel", 1, 1, f_foldlevel},
7919 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007920 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007922 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007923 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007924 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007925 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007926 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"getchar", 0, 1, f_getchar},
7928 {"getcharmod", 0, 0, f_getcharmod},
7929 {"getcmdline", 0, 0, f_getcmdline},
7930 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007931 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007933 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007934 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {"getfsize", 1, 1, f_getfsize},
7936 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007937 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007938 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007939 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007940 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007941 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007942 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007943 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007944 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007946 {"gettabvar", 2, 3, f_gettabvar},
7947 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"getwinposx", 0, 0, f_getwinposx},
7949 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007950 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007951 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007952 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007954 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007955 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007956 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7958 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7959 {"histadd", 2, 2, f_histadd},
7960 {"histdel", 1, 2, f_histdel},
7961 {"histget", 1, 2, f_histget},
7962 {"histnr", 1, 1, f_histnr},
7963 {"hlID", 1, 1, f_hlID},
7964 {"hlexists", 1, 1, f_hlexists},
7965 {"hostname", 0, 0, f_hostname},
7966 {"iconv", 3, 3, f_iconv},
7967 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007968 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007969 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007971 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 {"inputrestore", 0, 0, f_inputrestore},
7973 {"inputsave", 0, 0, f_inputsave},
7974 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007975 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007976 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007978 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007979 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007980 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007981 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007983 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {"libcall", 3, 3, f_libcall},
7985 {"libcallnr", 3, 3, f_libcallnr},
7986 {"line", 1, 1, f_line},
7987 {"line2byte", 1, 1, f_line2byte},
7988 {"lispindent", 1, 1, f_lispindent},
7989 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007990#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007991 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007992 {"log10", 1, 1, f_log10},
7993#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007994#ifdef FEAT_LUA
7995 {"luaeval", 1, 2, f_luaeval},
7996#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007997 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007998 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007999 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008000 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008001 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008002 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008003 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008004 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008005 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008006 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008007 {"max", 1, 1, f_max},
8008 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008009#ifdef vim_mkdir
8010 {"mkdir", 1, 3, f_mkdir},
8011#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008012 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008013#ifdef FEAT_MZSCHEME
8014 {"mzeval", 1, 1, f_mzeval},
8015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008017 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008018 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008019 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008020#ifdef FEAT_FLOAT
8021 {"pow", 2, 2, f_pow},
8022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008024 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008025 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008026#ifdef FEAT_PYTHON3
8027 {"py3eval", 1, 1, f_py3eval},
8028#endif
8029#ifdef FEAT_PYTHON
8030 {"pyeval", 1, 1, f_pyeval},
8031#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008032 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008033 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008034 {"reltime", 0, 2, f_reltime},
8035 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {"remote_expr", 2, 3, f_remote_expr},
8037 {"remote_foreground", 1, 1, f_remote_foreground},
8038 {"remote_peek", 1, 2, f_remote_peek},
8039 {"remote_read", 1, 1, f_remote_read},
8040 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008041 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008043 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008045 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008046#ifdef FEAT_FLOAT
8047 {"round", 1, 1, f_round},
8048#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008049 {"screenattr", 2, 2, f_screenattr},
8050 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008051 {"screencol", 0, 0, f_screencol},
8052 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008053 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008054 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008055 {"searchpair", 3, 7, f_searchpair},
8056 {"searchpairpos", 3, 7, f_searchpairpos},
8057 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"server2client", 2, 2, f_server2client},
8059 {"serverlist", 0, 0, f_serverlist},
8060 {"setbufvar", 3, 3, f_setbufvar},
8061 {"setcmdpos", 1, 1, f_setcmdpos},
8062 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008063 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008064 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008065 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008066 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008068 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008069 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008071#ifdef FEAT_CRYPT
8072 {"sha256", 1, 1, f_sha256},
8073#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008074 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008075 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008077#ifdef FEAT_FLOAT
8078 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008079 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008080#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008081 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008082 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008083 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008084 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008085 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008086#ifdef FEAT_FLOAT
8087 {"sqrt", 1, 1, f_sqrt},
8088 {"str2float", 1, 1, f_str2float},
8089#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008090 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008091 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008092 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093#ifdef HAVE_STRFTIME
8094 {"strftime", 1, 2, f_strftime},
8095#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008096 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008097 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"strlen", 1, 1, f_strlen},
8099 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008100 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008102 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"submatch", 1, 1, f_submatch},
8104 {"substitute", 4, 4, f_substitute},
8105 {"synID", 3, 3, f_synID},
8106 {"synIDattr", 2, 3, f_synIDattr},
8107 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008108 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008109 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008110 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008111 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008112 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008113 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008114 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008115 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008116#ifdef FEAT_FLOAT
8117 {"tan", 1, 1, f_tan},
8118 {"tanh", 1, 1, f_tanh},
8119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008121 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"tolower", 1, 1, f_tolower},
8123 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008124 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008125#ifdef FEAT_FLOAT
8126 {"trunc", 1, 1, f_trunc},
8127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008129 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008130 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008131 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"virtcol", 1, 1, f_virtcol},
8133 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008134 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"winbufnr", 1, 1, f_winbufnr},
8136 {"wincol", 0, 0, f_wincol},
8137 {"winheight", 1, 1, f_winheight},
8138 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008139 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008141 {"winrestview", 1, 1, f_winrestview},
8142 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008144 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008145 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146};
8147
8148#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8149
8150/*
8151 * Function given to ExpandGeneric() to obtain the list of internal
8152 * or user defined function names.
8153 */
8154 char_u *
8155get_function_name(xp, idx)
8156 expand_T *xp;
8157 int idx;
8158{
8159 static int intidx = -1;
8160 char_u *name;
8161
8162 if (idx == 0)
8163 intidx = -1;
8164 if (intidx < 0)
8165 {
8166 name = get_user_func_name(xp, idx);
8167 if (name != NULL)
8168 return name;
8169 }
8170 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8171 {
8172 STRCPY(IObuff, functions[intidx].f_name);
8173 STRCAT(IObuff, "(");
8174 if (functions[intidx].f_max_argc == 0)
8175 STRCAT(IObuff, ")");
8176 return IObuff;
8177 }
8178
8179 return NULL;
8180}
8181
8182/*
8183 * Function given to ExpandGeneric() to obtain the list of internal or
8184 * user defined variable or function names.
8185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 char_u *
8187get_expr_name(xp, idx)
8188 expand_T *xp;
8189 int idx;
8190{
8191 static int intidx = -1;
8192 char_u *name;
8193
8194 if (idx == 0)
8195 intidx = -1;
8196 if (intidx < 0)
8197 {
8198 name = get_function_name(xp, idx);
8199 if (name != NULL)
8200 return name;
8201 }
8202 return get_user_var_name(xp, ++intidx);
8203}
8204
8205#endif /* FEAT_CMDL_COMPL */
8206
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008207#if defined(EBCDIC) || defined(PROTO)
8208/*
8209 * Compare struct fst by function name.
8210 */
8211 static int
8212compare_func_name(s1, s2)
8213 const void *s1;
8214 const void *s2;
8215{
8216 struct fst *p1 = (struct fst *)s1;
8217 struct fst *p2 = (struct fst *)s2;
8218
8219 return STRCMP(p1->f_name, p2->f_name);
8220}
8221
8222/*
8223 * Sort the function table by function name.
8224 * The sorting of the table above is ASCII dependant.
8225 * On machines using EBCDIC we have to sort it.
8226 */
8227 static void
8228sortFunctions()
8229{
8230 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8231
8232 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8233}
8234#endif
8235
8236
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237/*
8238 * Find internal function in table above.
8239 * Return index, or -1 if not found
8240 */
8241 static int
8242find_internal_func(name)
8243 char_u *name; /* name of the function */
8244{
8245 int first = 0;
8246 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8247 int cmp;
8248 int x;
8249
8250 /*
8251 * Find the function name in the table. Binary search.
8252 */
8253 while (first <= last)
8254 {
8255 x = first + ((unsigned)(last - first) >> 1);
8256 cmp = STRCMP(name, functions[x].f_name);
8257 if (cmp < 0)
8258 last = x - 1;
8259 else if (cmp > 0)
8260 first = x + 1;
8261 else
8262 return x;
8263 }
8264 return -1;
8265}
8266
8267/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008268 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8269 * name it contains, otherwise return "name".
8270 */
8271 static char_u *
8272deref_func_name(name, lenp)
8273 char_u *name;
8274 int *lenp;
8275{
Bram Moolenaar33570922005-01-25 22:26:29 +00008276 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008277 int cc;
8278
8279 cc = name[*lenp];
8280 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008281 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008282 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008283 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008284 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008285 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008286 {
8287 *lenp = 0;
8288 return (char_u *)""; /* just in case */
8289 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008290 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008291 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008292 }
8293
8294 return name;
8295}
8296
8297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 * Allocate a variable for the result of a function.
8299 * Return OK or FAIL.
8300 */
8301 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008302get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8303 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 char_u *name; /* name of the function */
8305 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 char_u **arg; /* argument, pointing to the '(' */
8308 linenr_T firstline; /* first line of range */
8309 linenr_T lastline; /* last line of range */
8310 int *doesrange; /* return: function handled range */
8311 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008312 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313{
8314 char_u *argp;
8315 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008316 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 int argcount = 0; /* number of arguments found */
8318
8319 /*
8320 * Get the arguments.
8321 */
8322 argp = *arg;
8323 while (argcount < MAX_FUNC_ARGS)
8324 {
8325 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8326 if (*argp == ')' || *argp == ',' || *argp == NUL)
8327 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8329 {
8330 ret = FAIL;
8331 break;
8332 }
8333 ++argcount;
8334 if (*argp != ',')
8335 break;
8336 }
8337 if (*argp == ')')
8338 ++argp;
8339 else
8340 ret = FAIL;
8341
8342 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008344 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008346 {
8347 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008348 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008350 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352
8353 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008354 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355
8356 *arg = skipwhite(argp);
8357 return ret;
8358}
8359
8360
8361/*
8362 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008363 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008364 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 */
8366 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008367call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008368 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008369 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008371 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008373 typval_T *argvars; /* vars for arguments, must have "argcount"
8374 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 linenr_T firstline; /* first line of range */
8376 linenr_T lastline; /* last line of range */
8377 int *doesrange; /* return: function handled range */
8378 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008379 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380{
8381 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382#define ERROR_UNKNOWN 0
8383#define ERROR_TOOMANY 1
8384#define ERROR_TOOFEW 2
8385#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008386#define ERROR_DICT 4
8387#define ERROR_NONE 5
8388#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 int error = ERROR_NONE;
8390 int i;
8391 int llen;
8392 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393#define FLEN_FIXED 40
8394 char_u fname_buf[FLEN_FIXED + 1];
8395 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008396 char_u *name;
8397
8398 /* Make a copy of the name, if it comes from a funcref variable it could
8399 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008400 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008401 if (name == NULL)
8402 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403
8404 /*
8405 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8406 * Change <SNR>123_name() to K_SNR 123_name().
8407 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 llen = eval_fname_script(name);
8410 if (llen > 0)
8411 {
8412 fname_buf[0] = K_SPECIAL;
8413 fname_buf[1] = KS_EXTRA;
8414 fname_buf[2] = (int)KE_SNR;
8415 i = 3;
8416 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8417 {
8418 if (current_SID <= 0)
8419 error = ERROR_SCRIPT;
8420 else
8421 {
8422 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8423 i = (int)STRLEN(fname_buf);
8424 }
8425 }
8426 if (i + STRLEN(name + llen) < FLEN_FIXED)
8427 {
8428 STRCPY(fname_buf + i, name + llen);
8429 fname = fname_buf;
8430 }
8431 else
8432 {
8433 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8434 if (fname == NULL)
8435 error = ERROR_OTHER;
8436 else
8437 {
8438 mch_memmove(fname, fname_buf, (size_t)i);
8439 STRCPY(fname + i, name + llen);
8440 }
8441 }
8442 }
8443 else
8444 fname = name;
8445
8446 *doesrange = FALSE;
8447
8448
8449 /* execute the function if no errors detected and executing */
8450 if (evaluate && error == ERROR_NONE)
8451 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008452 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8453 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 error = ERROR_UNKNOWN;
8455
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008456 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {
8458 /*
8459 * User defined function.
8460 */
8461 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008462
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008464 /* Trigger FuncUndefined event, may load the function. */
8465 if (fp == NULL
8466 && apply_autocmds(EVENT_FUNCUNDEFINED,
8467 fname, fname, TRUE, NULL)
8468 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008470 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 fp = find_func(fname);
8472 }
8473#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008474 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008475 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008476 {
8477 /* loaded a package, search for the function again */
8478 fp = find_func(fname);
8479 }
8480
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 if (fp != NULL)
8482 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008483 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008485 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008487 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008489 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008490 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 else
8492 {
8493 /*
8494 * Call the user function.
8495 * Save and restore search patterns, script variables and
8496 * redo buffer.
8497 */
8498 save_search_patterns();
8499 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008500 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008501 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008502 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008503 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8504 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8505 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008506 /* Function was unreferenced while being used, free it
8507 * now. */
8508 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 restoreRedobuff();
8510 restore_search_patterns();
8511 error = ERROR_NONE;
8512 }
8513 }
8514 }
8515 else
8516 {
8517 /*
8518 * Find the function name in the table, call its implementation.
8519 */
8520 i = find_internal_func(fname);
8521 if (i >= 0)
8522 {
8523 if (argcount < functions[i].f_min_argc)
8524 error = ERROR_TOOFEW;
8525 else if (argcount > functions[i].f_max_argc)
8526 error = ERROR_TOOMANY;
8527 else
8528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008530 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 error = ERROR_NONE;
8532 }
8533 }
8534 }
8535 /*
8536 * The function call (or "FuncUndefined" autocommand sequence) might
8537 * have been aborted by an error, an interrupt, or an explicitly thrown
8538 * exception that has not been caught so far. This situation can be
8539 * tested for by calling aborting(). For an error in an internal
8540 * function or for the "E132" error in call_user_func(), however, the
8541 * throw point at which the "force_abort" flag (temporarily reset by
8542 * emsg()) is normally updated has not been reached yet. We need to
8543 * update that flag first to make aborting() reliable.
8544 */
8545 update_force_abort();
8546 }
8547 if (error == ERROR_NONE)
8548 ret = OK;
8549
8550 /*
8551 * Report an error unless the argument evaluation or function call has been
8552 * cancelled due to an aborting error, an interrupt, or an exception.
8553 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008554 if (!aborting())
8555 {
8556 switch (error)
8557 {
8558 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008559 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008560 break;
8561 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008562 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008563 break;
8564 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008565 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008566 name);
8567 break;
8568 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008569 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008570 name);
8571 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008572 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008573 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008574 name);
8575 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008576 }
8577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 if (fname != name && fname != fname_buf)
8580 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008581 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582
8583 return ret;
8584}
8585
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008586/*
8587 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008588 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008589 */
8590 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008591emsg_funcname(ermsg, name)
8592 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008593 char_u *name;
8594{
8595 char_u *p;
8596
8597 if (*name == K_SPECIAL)
8598 p = concat_str((char_u *)"<SNR>", name + 3);
8599 else
8600 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008601 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008602 if (p != name)
8603 vim_free(p);
8604}
8605
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008606/*
8607 * Return TRUE for a non-zero Number and a non-empty String.
8608 */
8609 static int
8610non_zero_arg(argvars)
8611 typval_T *argvars;
8612{
8613 return ((argvars[0].v_type == VAR_NUMBER
8614 && argvars[0].vval.v_number != 0)
8615 || (argvars[0].v_type == VAR_STRING
8616 && argvars[0].vval.v_string != NULL
8617 && *argvars[0].vval.v_string != NUL));
8618}
8619
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620/*********************************************
8621 * Implementation of the built-in functions
8622 */
8623
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008624#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008625static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8626
8627/*
8628 * Get the float value of "argvars[0]" into "f".
8629 * Returns FAIL when the argument is not a Number or Float.
8630 */
8631 static int
8632get_float_arg(argvars, f)
8633 typval_T *argvars;
8634 float_T *f;
8635{
8636 if (argvars[0].v_type == VAR_FLOAT)
8637 {
8638 *f = argvars[0].vval.v_float;
8639 return OK;
8640 }
8641 if (argvars[0].v_type == VAR_NUMBER)
8642 {
8643 *f = (float_T)argvars[0].vval.v_number;
8644 return OK;
8645 }
8646 EMSG(_("E808: Number or Float required"));
8647 return FAIL;
8648}
8649
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008650/*
8651 * "abs(expr)" function
8652 */
8653 static void
8654f_abs(argvars, rettv)
8655 typval_T *argvars;
8656 typval_T *rettv;
8657{
8658 if (argvars[0].v_type == VAR_FLOAT)
8659 {
8660 rettv->v_type = VAR_FLOAT;
8661 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8662 }
8663 else
8664 {
8665 varnumber_T n;
8666 int error = FALSE;
8667
8668 n = get_tv_number_chk(&argvars[0], &error);
8669 if (error)
8670 rettv->vval.v_number = -1;
8671 else if (n > 0)
8672 rettv->vval.v_number = n;
8673 else
8674 rettv->vval.v_number = -n;
8675 }
8676}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008677
8678/*
8679 * "acos()" function
8680 */
8681 static void
8682f_acos(argvars, rettv)
8683 typval_T *argvars;
8684 typval_T *rettv;
8685{
8686 float_T f;
8687
8688 rettv->v_type = VAR_FLOAT;
8689 if (get_float_arg(argvars, &f) == OK)
8690 rettv->vval.v_float = acos(f);
8691 else
8692 rettv->vval.v_float = 0.0;
8693}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008694#endif
8695
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008697 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 */
8699 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008700f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008701 typval_T *argvars;
8702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703{
Bram Moolenaar33570922005-01-25 22:26:29 +00008704 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008706 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008707 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008709 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008710 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008711 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008713 }
8714 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008715 EMSG(_(e_listreq));
8716}
8717
8718/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008719 * "and(expr, expr)" function
8720 */
8721 static void
8722f_and(argvars, rettv)
8723 typval_T *argvars;
8724 typval_T *rettv;
8725{
8726 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8727 & get_tv_number_chk(&argvars[1], NULL);
8728}
8729
8730/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008731 * "append(lnum, string/list)" function
8732 */
8733 static void
8734f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008735 typval_T *argvars;
8736 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008737{
8738 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008740 list_T *l = NULL;
8741 listitem_T *li = NULL;
8742 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008743 long added = 0;
8744
Bram Moolenaar0d660222005-01-07 21:51:51 +00008745 lnum = get_tv_lnum(argvars);
8746 if (lnum >= 0
8747 && lnum <= curbuf->b_ml.ml_line_count
8748 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008749 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008750 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008751 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008752 l = argvars[1].vval.v_list;
8753 if (l == NULL)
8754 return;
8755 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008756 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008757 for (;;)
8758 {
8759 if (l == NULL)
8760 tv = &argvars[1]; /* append a string */
8761 else if (li == NULL)
8762 break; /* end of list */
8763 else
8764 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008765 line = get_tv_string_chk(tv);
8766 if (line == NULL) /* type error */
8767 {
8768 rettv->vval.v_number = 1; /* Failed */
8769 break;
8770 }
8771 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008772 ++added;
8773 if (l == NULL)
8774 break;
8775 li = li->li_next;
8776 }
8777
8778 appended_lines_mark(lnum, added);
8779 if (curwin->w_cursor.lnum > lnum)
8780 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008782 else
8783 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784}
8785
8786/*
8787 * "argc()" function
8788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008790f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008791 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008794 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795}
8796
8797/*
8798 * "argidx()" function
8799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008801f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008802 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008805 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806}
8807
8808/*
8809 * "argv(nr)" function
8810 */
8811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008812f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008813 typval_T *argvars;
8814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815{
8816 int idx;
8817
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008818 if (argvars[0].v_type != VAR_UNKNOWN)
8819 {
8820 idx = get_tv_number_chk(&argvars[0], NULL);
8821 if (idx >= 0 && idx < ARGCOUNT)
8822 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8823 else
8824 rettv->vval.v_string = NULL;
8825 rettv->v_type = VAR_STRING;
8826 }
8827 else if (rettv_list_alloc(rettv) == OK)
8828 for (idx = 0; idx < ARGCOUNT; ++idx)
8829 list_append_string(rettv->vval.v_list,
8830 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831}
8832
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008833#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008834/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008835 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008836 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008837 static void
8838f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008839 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008840 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008841{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008842 float_T f;
8843
8844 rettv->v_type = VAR_FLOAT;
8845 if (get_float_arg(argvars, &f) == OK)
8846 rettv->vval.v_float = asin(f);
8847 else
8848 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008849}
8850
8851/*
8852 * "atan()" function
8853 */
8854 static void
8855f_atan(argvars, rettv)
8856 typval_T *argvars;
8857 typval_T *rettv;
8858{
8859 float_T f;
8860
8861 rettv->v_type = VAR_FLOAT;
8862 if (get_float_arg(argvars, &f) == OK)
8863 rettv->vval.v_float = atan(f);
8864 else
8865 rettv->vval.v_float = 0.0;
8866}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008867
8868/*
8869 * "atan2()" function
8870 */
8871 static void
8872f_atan2(argvars, rettv)
8873 typval_T *argvars;
8874 typval_T *rettv;
8875{
8876 float_T fx, fy;
8877
8878 rettv->v_type = VAR_FLOAT;
8879 if (get_float_arg(argvars, &fx) == OK
8880 && get_float_arg(&argvars[1], &fy) == OK)
8881 rettv->vval.v_float = atan2(fx, fy);
8882 else
8883 rettv->vval.v_float = 0.0;
8884}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008885#endif
8886
Bram Moolenaar071d4272004-06-13 20:20:40 +00008887/*
8888 * "browse(save, title, initdir, default)" function
8889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008891f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008892 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008894{
8895#ifdef FEAT_BROWSE
8896 int save;
8897 char_u *title;
8898 char_u *initdir;
8899 char_u *defname;
8900 char_u buf[NUMBUFLEN];
8901 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008902 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008904 save = get_tv_number_chk(&argvars[0], &error);
8905 title = get_tv_string_chk(&argvars[1]);
8906 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8907 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008909 if (error || title == NULL || initdir == NULL || defname == NULL)
8910 rettv->vval.v_string = NULL;
8911 else
8912 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008913 do_browse(save ? BROWSE_SAVE : 0,
8914 title, defname, NULL, initdir, NULL, curbuf);
8915#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008916 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008917#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008918 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008919}
8920
8921/*
8922 * "browsedir(title, initdir)" function
8923 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008925f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008926 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008927 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008928{
8929#ifdef FEAT_BROWSE
8930 char_u *title;
8931 char_u *initdir;
8932 char_u buf[NUMBUFLEN];
8933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008934 title = get_tv_string_chk(&argvars[0]);
8935 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008936
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008937 if (title == NULL || initdir == NULL)
8938 rettv->vval.v_string = NULL;
8939 else
8940 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008941 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008943 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008945 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946}
8947
Bram Moolenaar33570922005-01-25 22:26:29 +00008948static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008949
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950/*
8951 * Find a buffer by number or exact name.
8952 */
8953 static buf_T *
8954find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008955 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956{
8957 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008959 if (avar->v_type == VAR_NUMBER)
8960 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008961 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008963 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008964 if (buf == NULL)
8965 {
8966 /* No full path name match, try a match with a URL or a "nofile"
8967 * buffer, these don't use the full path. */
8968 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8969 if (buf->b_fname != NULL
8970 && (path_with_url(buf->b_fname)
8971#ifdef FEAT_QUICKFIX
8972 || bt_nofile(buf)
8973#endif
8974 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008975 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008976 break;
8977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008978 }
8979 return buf;
8980}
8981
8982/*
8983 * "bufexists(expr)" function
8984 */
8985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008986f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008987 typval_T *argvars;
8988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008990 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991}
8992
8993/*
8994 * "buflisted(expr)" function
8995 */
8996 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008997f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008998 typval_T *argvars;
8999 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000{
9001 buf_T *buf;
9002
9003 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009004 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005}
9006
9007/*
9008 * "bufloaded(expr)" function
9009 */
9010 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009011f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009012 typval_T *argvars;
9013 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014{
9015 buf_T *buf;
9016
9017 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009018 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019}
9020
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009021static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009022
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023/*
9024 * Get buffer by number or pattern.
9025 */
9026 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009027get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009028 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009029 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009031 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032 int save_magic;
9033 char_u *save_cpo;
9034 buf_T *buf;
9035
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009036 if (tv->v_type == VAR_NUMBER)
9037 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009038 if (tv->v_type != VAR_STRING)
9039 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 if (name == NULL || *name == NUL)
9041 return curbuf;
9042 if (name[0] == '$' && name[1] == NUL)
9043 return lastbuf;
9044
9045 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9046 save_magic = p_magic;
9047 p_magic = TRUE;
9048 save_cpo = p_cpo;
9049 p_cpo = (char_u *)"";
9050
9051 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009052 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009053
9054 p_magic = save_magic;
9055 p_cpo = save_cpo;
9056
9057 /* If not found, try expanding the name, like done for bufexists(). */
9058 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009059 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060
9061 return buf;
9062}
9063
9064/*
9065 * "bufname(expr)" function
9066 */
9067 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009069 typval_T *argvars;
9070 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071{
9072 buf_T *buf;
9073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009074 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009076 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009079 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 --emsg_off;
9083}
9084
9085/*
9086 * "bufnr(expr)" function
9087 */
9088 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009090 typval_T *argvars;
9091 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092{
9093 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009094 int error = FALSE;
9095 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009097 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009098 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009099 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009100 --emsg_off;
9101
9102 /* If the buffer isn't found and the second argument is not zero create a
9103 * new buffer. */
9104 if (buf == NULL
9105 && argvars[1].v_type != VAR_UNKNOWN
9106 && get_tv_number_chk(&argvars[1], &error) != 0
9107 && !error
9108 && (name = get_tv_string_chk(&argvars[0])) != NULL
9109 && !error)
9110 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9111
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009113 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009115 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009116}
9117
9118/*
9119 * "bufwinnr(nr)" function
9120 */
9121 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122f_bufwinnr(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#ifdef FEAT_WINDOWS
9127 win_T *wp;
9128 int winnr = 0;
9129#endif
9130 buf_T *buf;
9131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009132 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009134 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135#ifdef FEAT_WINDOWS
9136 for (wp = firstwin; wp; wp = wp->w_next)
9137 {
9138 ++winnr;
9139 if (wp->w_buffer == buf)
9140 break;
9141 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009142 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145#endif
9146 --emsg_off;
9147}
9148
9149/*
9150 * "byte2line(byte)" function
9151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009154 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009155 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156{
9157#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009158 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159#else
9160 long boff = 0;
9161
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009162 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009164 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 (linenr_T)0, &boff);
9168#endif
9169}
9170
9171/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009172 * "byteidx()" function
9173 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009174 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009176 typval_T *argvars;
9177 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009178{
9179#ifdef FEAT_MBYTE
9180 char_u *t;
9181#endif
9182 char_u *str;
9183 long idx;
9184
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009185 str = get_tv_string_chk(&argvars[0]);
9186 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009187 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009188 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009189 return;
9190
9191#ifdef FEAT_MBYTE
9192 t = str;
9193 for ( ; idx > 0; idx--)
9194 {
9195 if (*t == NUL) /* EOL reached */
9196 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009197 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009198 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009199 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009200#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009201 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009202 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009203#endif
9204}
9205
Bram Moolenaardb913952012-06-29 12:54:53 +02009206 int
9207func_call(name, args, selfdict, rettv)
9208 char_u *name;
9209 typval_T *args;
9210 dict_T *selfdict;
9211 typval_T *rettv;
9212{
9213 listitem_T *item;
9214 typval_T argv[MAX_FUNC_ARGS + 1];
9215 int argc = 0;
9216 int dummy;
9217 int r = 0;
9218
9219 for (item = args->vval.v_list->lv_first; item != NULL;
9220 item = item->li_next)
9221 {
9222 if (argc == MAX_FUNC_ARGS)
9223 {
9224 EMSG(_("E699: Too many arguments"));
9225 break;
9226 }
9227 /* Make a copy of each argument. This is needed to be able to set
9228 * v_lock to VAR_FIXED in the copy without changing the original list.
9229 */
9230 copy_tv(&item->li_tv, &argv[argc++]);
9231 }
9232
9233 if (item == NULL)
9234 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9235 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9236 &dummy, TRUE, selfdict);
9237
9238 /* Free the arguments. */
9239 while (argc > 0)
9240 clear_tv(&argv[--argc]);
9241
9242 return r;
9243}
9244
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009245/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009246 * "call(func, arglist)" function
9247 */
9248 static void
9249f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009250 typval_T *argvars;
9251 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009252{
9253 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009254 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009255
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009256 if (argvars[1].v_type != VAR_LIST)
9257 {
9258 EMSG(_(e_listreq));
9259 return;
9260 }
9261 if (argvars[1].vval.v_list == NULL)
9262 return;
9263
9264 if (argvars[0].v_type == VAR_FUNC)
9265 func = argvars[0].vval.v_string;
9266 else
9267 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009268 if (*func == NUL)
9269 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009270
Bram Moolenaare9a41262005-01-15 22:18:47 +00009271 if (argvars[2].v_type != VAR_UNKNOWN)
9272 {
9273 if (argvars[2].v_type != VAR_DICT)
9274 {
9275 EMSG(_(e_dictreq));
9276 return;
9277 }
9278 selfdict = argvars[2].vval.v_dict;
9279 }
9280
Bram Moolenaardb913952012-06-29 12:54:53 +02009281 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009282}
9283
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009284#ifdef FEAT_FLOAT
9285/*
9286 * "ceil({float})" function
9287 */
9288 static void
9289f_ceil(argvars, rettv)
9290 typval_T *argvars;
9291 typval_T *rettv;
9292{
9293 float_T f;
9294
9295 rettv->v_type = VAR_FLOAT;
9296 if (get_float_arg(argvars, &f) == OK)
9297 rettv->vval.v_float = ceil(f);
9298 else
9299 rettv->vval.v_float = 0.0;
9300}
9301#endif
9302
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009303/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009304 * "changenr()" function
9305 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009306 static void
9307f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009308 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009309 typval_T *rettv;
9310{
9311 rettv->vval.v_number = curbuf->b_u_seq_cur;
9312}
9313
9314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 * "char2nr(string)" function
9316 */
9317 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009318f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009319 typval_T *argvars;
9320 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009321{
9322#ifdef FEAT_MBYTE
9323 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009324 {
9325 int utf8 = 0;
9326
9327 if (argvars[1].v_type != VAR_UNKNOWN)
9328 utf8 = get_tv_number_chk(&argvars[1], NULL);
9329
9330 if (utf8)
9331 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9332 else
9333 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009335 else
9336#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009337 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009338}
9339
9340/*
9341 * "cindent(lnum)" function
9342 */
9343 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009344f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009345 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009346 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347{
9348#ifdef FEAT_CINDENT
9349 pos_T pos;
9350 linenr_T lnum;
9351
9352 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9355 {
9356 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009357 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009358 curwin->w_cursor = pos;
9359 }
9360 else
9361#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363}
9364
9365/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009366 * "clearmatches()" function
9367 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009368 static void
9369f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009370 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009371 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009372{
9373#ifdef FEAT_SEARCH_EXTRA
9374 clear_matches(curwin);
9375#endif
9376}
9377
9378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009379 * "col(string)" function
9380 */
9381 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009382f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009383 typval_T *argvars;
9384 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385{
9386 colnr_T col = 0;
9387 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009388 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009390 fp = var2fpos(&argvars[0], FALSE, &fnum);
9391 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009392 {
9393 if (fp->col == MAXCOL)
9394 {
9395 /* '> can be MAXCOL, get the length of the line then */
9396 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009397 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 else
9399 col = MAXCOL;
9400 }
9401 else
9402 {
9403 col = fp->col + 1;
9404#ifdef FEAT_VIRTUALEDIT
9405 /* col(".") when the cursor is on the NUL at the end of the line
9406 * because of "coladd" can be seen as an extra column. */
9407 if (virtual_active() && fp == &curwin->w_cursor)
9408 {
9409 char_u *p = ml_get_cursor();
9410
9411 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9412 curwin->w_virtcol - curwin->w_cursor.coladd))
9413 {
9414# ifdef FEAT_MBYTE
9415 int l;
9416
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009417 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 col += l;
9419# else
9420 if (*p != NUL && p[1] == NUL)
9421 ++col;
9422# endif
9423 }
9424 }
9425#endif
9426 }
9427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009428 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429}
9430
Bram Moolenaar572cb562005-08-05 21:35:02 +00009431#if defined(FEAT_INS_EXPAND)
9432/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009433 * "complete()" function
9434 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009435 static void
9436f_complete(argvars, rettv)
9437 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009438 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009439{
9440 int startcol;
9441
9442 if ((State & INSERT) == 0)
9443 {
9444 EMSG(_("E785: complete() can only be used in Insert mode"));
9445 return;
9446 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009447
9448 /* Check for undo allowed here, because if something was already inserted
9449 * the line was already saved for undo and this check isn't done. */
9450 if (!undo_allowed())
9451 return;
9452
Bram Moolenaarade00832006-03-10 21:46:58 +00009453 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9454 {
9455 EMSG(_(e_invarg));
9456 return;
9457 }
9458
9459 startcol = get_tv_number_chk(&argvars[0], NULL);
9460 if (startcol <= 0)
9461 return;
9462
9463 set_completion(startcol - 1, argvars[1].vval.v_list);
9464}
9465
9466/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009467 * "complete_add()" function
9468 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009469 static void
9470f_complete_add(argvars, rettv)
9471 typval_T *argvars;
9472 typval_T *rettv;
9473{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009474 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009475}
9476
9477/*
9478 * "complete_check()" function
9479 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009480 static void
9481f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009482 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009483 typval_T *rettv;
9484{
9485 int saved = RedrawingDisabled;
9486
9487 RedrawingDisabled = 0;
9488 ins_compl_check_keys(0);
9489 rettv->vval.v_number = compl_interrupted;
9490 RedrawingDisabled = saved;
9491}
9492#endif
9493
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494/*
9495 * "confirm(message, buttons[, default [, type]])" function
9496 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009498f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009499 typval_T *argvars UNUSED;
9500 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501{
9502#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9503 char_u *message;
9504 char_u *buttons = NULL;
9505 char_u buf[NUMBUFLEN];
9506 char_u buf2[NUMBUFLEN];
9507 int def = 1;
9508 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009509 char_u *typestr;
9510 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009512 message = get_tv_string_chk(&argvars[0]);
9513 if (message == NULL)
9514 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009515 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009517 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9518 if (buttons == NULL)
9519 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009520 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009522 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009523 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009525 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9526 if (typestr == NULL)
9527 error = TRUE;
9528 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009530 switch (TOUPPER_ASC(*typestr))
9531 {
9532 case 'E': type = VIM_ERROR; break;
9533 case 'Q': type = VIM_QUESTION; break;
9534 case 'I': type = VIM_INFO; break;
9535 case 'W': type = VIM_WARNING; break;
9536 case 'G': type = VIM_GENERIC; break;
9537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 }
9539 }
9540 }
9541 }
9542
9543 if (buttons == NULL || *buttons == NUL)
9544 buttons = (char_u *)_("&Ok");
9545
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009546 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009547 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009548 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549#endif
9550}
9551
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009552/*
9553 * "copy()" function
9554 */
9555 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009557 typval_T *argvars;
9558 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009559{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009560 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009561}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009563#ifdef FEAT_FLOAT
9564/*
9565 * "cos()" function
9566 */
9567 static void
9568f_cos(argvars, rettv)
9569 typval_T *argvars;
9570 typval_T *rettv;
9571{
9572 float_T f;
9573
9574 rettv->v_type = VAR_FLOAT;
9575 if (get_float_arg(argvars, &f) == OK)
9576 rettv->vval.v_float = cos(f);
9577 else
9578 rettv->vval.v_float = 0.0;
9579}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009580
9581/*
9582 * "cosh()" function
9583 */
9584 static void
9585f_cosh(argvars, rettv)
9586 typval_T *argvars;
9587 typval_T *rettv;
9588{
9589 float_T f;
9590
9591 rettv->v_type = VAR_FLOAT;
9592 if (get_float_arg(argvars, &f) == OK)
9593 rettv->vval.v_float = cosh(f);
9594 else
9595 rettv->vval.v_float = 0.0;
9596}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009597#endif
9598
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009600 * "count()" function
9601 */
9602 static void
9603f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009604 typval_T *argvars;
9605 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009606{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009607 long n = 0;
9608 int ic = FALSE;
9609
Bram Moolenaare9a41262005-01-15 22:18:47 +00009610 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009611 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009612 listitem_T *li;
9613 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009614 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009615
Bram Moolenaare9a41262005-01-15 22:18:47 +00009616 if ((l = argvars[0].vval.v_list) != NULL)
9617 {
9618 li = l->lv_first;
9619 if (argvars[2].v_type != VAR_UNKNOWN)
9620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009621 int error = FALSE;
9622
9623 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009624 if (argvars[3].v_type != VAR_UNKNOWN)
9625 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009626 idx = get_tv_number_chk(&argvars[3], &error);
9627 if (!error)
9628 {
9629 li = list_find(l, idx);
9630 if (li == NULL)
9631 EMSGN(_(e_listidx), idx);
9632 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009633 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634 if (error)
9635 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009636 }
9637
9638 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009639 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009640 ++n;
9641 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009642 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009643 else if (argvars[0].v_type == VAR_DICT)
9644 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009645 int todo;
9646 dict_T *d;
9647 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009648
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009649 if ((d = argvars[0].vval.v_dict) != NULL)
9650 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009651 int error = FALSE;
9652
Bram Moolenaare9a41262005-01-15 22:18:47 +00009653 if (argvars[2].v_type != VAR_UNKNOWN)
9654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009655 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009656 if (argvars[3].v_type != VAR_UNKNOWN)
9657 EMSG(_(e_invarg));
9658 }
9659
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009660 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009661 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009662 {
9663 if (!HASHITEM_EMPTY(hi))
9664 {
9665 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009666 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009667 ++n;
9668 }
9669 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009670 }
9671 }
9672 else
9673 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009674 rettv->vval.v_number = n;
9675}
9676
9677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9679 *
9680 * Checks the existence of a cscope connection.
9681 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009684 typval_T *argvars UNUSED;
9685 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686{
9687#ifdef FEAT_CSCOPE
9688 int num = 0;
9689 char_u *dbpath = NULL;
9690 char_u *prepend = NULL;
9691 char_u buf[NUMBUFLEN];
9692
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009693 if (argvars[0].v_type != VAR_UNKNOWN
9694 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 num = (int)get_tv_number(&argvars[0]);
9697 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009698 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 }
9701
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009702 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703#endif
9704}
9705
9706/*
9707 * "cursor(lnum, col)" function
9708 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009709 * Moves the cursor to the specified line and column.
9710 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009714 typval_T *argvars;
9715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716{
9717 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009718#ifdef FEAT_VIRTUALEDIT
9719 long coladd = 0;
9720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009722 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009723 if (argvars[1].v_type == VAR_UNKNOWN)
9724 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009725 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009726
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009727 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009728 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009729 line = pos.lnum;
9730 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009731#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009732 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009733#endif
9734 }
9735 else
9736 {
9737 line = get_tv_lnum(argvars);
9738 col = get_tv_number_chk(&argvars[1], NULL);
9739#ifdef FEAT_VIRTUALEDIT
9740 if (argvars[2].v_type != VAR_UNKNOWN)
9741 coladd = get_tv_number_chk(&argvars[2], NULL);
9742#endif
9743 }
9744 if (line < 0 || col < 0
9745#ifdef FEAT_VIRTUALEDIT
9746 || coladd < 0
9747#endif
9748 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009749 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 if (line > 0)
9751 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752 if (col > 0)
9753 curwin->w_cursor.col = col - 1;
9754#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009755 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756#endif
9757
9758 /* Make sure the cursor is in a valid position. */
9759 check_cursor();
9760#ifdef FEAT_MBYTE
9761 /* Correct cursor for multi-byte character. */
9762 if (has_mbyte)
9763 mb_adjust_cursor();
9764#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009765
9766 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009767 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768}
9769
9770/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009771 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772 */
9773 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009775 typval_T *argvars;
9776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009778 int noref = 0;
9779
9780 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009781 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009782 if (noref < 0 || noref > 1)
9783 EMSG(_(e_invarg));
9784 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009785 {
9786 current_copyID += COPYID_INC;
9787 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789}
9790
9791/*
9792 * "delete()" function
9793 */
9794 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009796 typval_T *argvars;
9797 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798{
9799 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009800 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009801 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009802 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803}
9804
9805/*
9806 * "did_filetype()" function
9807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009810 typval_T *argvars UNUSED;
9811 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812{
9813#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009814 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815#endif
9816}
9817
9818/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009819 * "diff_filler()" function
9820 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009823 typval_T *argvars UNUSED;
9824 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009825{
9826#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009828#endif
9829}
9830
9831/*
9832 * "diff_hlID()" function
9833 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009834 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009836 typval_T *argvars UNUSED;
9837 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009838{
9839#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009840 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009841 static linenr_T prev_lnum = 0;
9842 static int changedtick = 0;
9843 static int fnum = 0;
9844 static int change_start = 0;
9845 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009846 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009847 int filler_lines;
9848 int col;
9849
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009850 if (lnum < 0) /* ignore type error in {lnum} arg */
9851 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009852 if (lnum != prev_lnum
9853 || changedtick != curbuf->b_changedtick
9854 || fnum != curbuf->b_fnum)
9855 {
9856 /* New line, buffer, change: need to get the values. */
9857 filler_lines = diff_check(curwin, lnum);
9858 if (filler_lines < 0)
9859 {
9860 if (filler_lines == -1)
9861 {
9862 change_start = MAXCOL;
9863 change_end = -1;
9864 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9865 hlID = HLF_ADD; /* added line */
9866 else
9867 hlID = HLF_CHD; /* changed line */
9868 }
9869 else
9870 hlID = HLF_ADD; /* added line */
9871 }
9872 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009873 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009874 prev_lnum = lnum;
9875 changedtick = curbuf->b_changedtick;
9876 fnum = curbuf->b_fnum;
9877 }
9878
9879 if (hlID == HLF_CHD || hlID == HLF_TXD)
9880 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009881 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009882 if (col >= change_start && col <= change_end)
9883 hlID = HLF_TXD; /* changed text */
9884 else
9885 hlID = HLF_CHD; /* changed line */
9886 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009887 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009888#endif
9889}
9890
9891/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009892 * "empty({expr})" function
9893 */
9894 static void
9895f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009896 typval_T *argvars;
9897 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009898{
9899 int n;
9900
9901 switch (argvars[0].v_type)
9902 {
9903 case VAR_STRING:
9904 case VAR_FUNC:
9905 n = argvars[0].vval.v_string == NULL
9906 || *argvars[0].vval.v_string == NUL;
9907 break;
9908 case VAR_NUMBER:
9909 n = argvars[0].vval.v_number == 0;
9910 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009911#ifdef FEAT_FLOAT
9912 case VAR_FLOAT:
9913 n = argvars[0].vval.v_float == 0.0;
9914 break;
9915#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009916 case VAR_LIST:
9917 n = argvars[0].vval.v_list == NULL
9918 || argvars[0].vval.v_list->lv_first == NULL;
9919 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920 case VAR_DICT:
9921 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009922 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009923 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009924 default:
9925 EMSG2(_(e_intern2), "f_empty()");
9926 n = 0;
9927 }
9928
9929 rettv->vval.v_number = n;
9930}
9931
9932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 * "escape({string}, {chars})" function
9934 */
9935 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009936f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009937 typval_T *argvars;
9938 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939{
9940 char_u buf[NUMBUFLEN];
9941
Bram Moolenaar758711c2005-02-02 23:11:38 +00009942 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9943 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945}
9946
9947/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009948 * "eval()" function
9949 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009950 static void
9951f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009952 typval_T *argvars;
9953 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009954{
9955 char_u *s;
9956
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009957 s = get_tv_string_chk(&argvars[0]);
9958 if (s != NULL)
9959 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009960
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009961 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9962 {
9963 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009964 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009965 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009966 else if (*s != NUL)
9967 EMSG(_(e_trailing));
9968}
9969
9970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 * "eventhandler()" function
9972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009974f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009975 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009976 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009978 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979}
9980
9981/*
9982 * "executable()" function
9983 */
9984 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009985f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009986 typval_T *argvars;
9987 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009989 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990}
9991
9992/*
9993 * "exists()" function
9994 */
9995 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009996f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009997 typval_T *argvars;
9998 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999{
10000 char_u *p;
10001 char_u *name;
10002 int n = FALSE;
10003 int len = 0;
10004
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010005 no_autoload = TRUE;
10006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010007 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 if (*p == '$') /* environment variable */
10009 {
10010 /* first try "normal" environment variables (fast) */
10011 if (mch_getenv(p + 1) != NULL)
10012 n = TRUE;
10013 else
10014 {
10015 /* try expanding things like $VIM and ${HOME} */
10016 p = expand_env_save(p);
10017 if (p != NULL && *p != '$')
10018 n = TRUE;
10019 vim_free(p);
10020 }
10021 }
10022 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010024 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010025 if (*skipwhite(p) != NUL)
10026 n = FALSE; /* trailing garbage */
10027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028 else if (*p == '*') /* internal or user defined function */
10029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010030 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 }
10032 else if (*p == ':')
10033 {
10034 n = cmd_exists(p + 1);
10035 }
10036 else if (*p == '#')
10037 {
10038#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010039 if (p[1] == '#')
10040 n = autocmd_supported(p + 2);
10041 else
10042 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043#endif
10044 }
10045 else /* internal variable */
10046 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010047 char_u *tofree;
10048 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010049
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010050 /* get_name_len() takes care of expanding curly braces */
10051 name = p;
10052 len = get_name_len(&p, &tofree, TRUE, FALSE);
10053 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010054 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010055 if (tofree != NULL)
10056 name = tofree;
10057 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10058 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010059 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010060 /* handle d.key, l[idx], f(expr) */
10061 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10062 if (n)
10063 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010064 }
10065 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010066 if (*p != NUL)
10067 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010069 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010070 }
10071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010072 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010073
10074 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010075}
10076
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010077#ifdef FEAT_FLOAT
10078/*
10079 * "exp()" function
10080 */
10081 static void
10082f_exp(argvars, rettv)
10083 typval_T *argvars;
10084 typval_T *rettv;
10085{
10086 float_T f;
10087
10088 rettv->v_type = VAR_FLOAT;
10089 if (get_float_arg(argvars, &f) == OK)
10090 rettv->vval.v_float = exp(f);
10091 else
10092 rettv->vval.v_float = 0.0;
10093}
10094#endif
10095
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096/*
10097 * "expand()" function
10098 */
10099 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010100f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010101 typval_T *argvars;
10102 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103{
10104 char_u *s;
10105 int len;
10106 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010107 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010109 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010110 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010112 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010113 if (argvars[1].v_type != VAR_UNKNOWN
10114 && argvars[2].v_type != VAR_UNKNOWN
10115 && get_tv_number_chk(&argvars[2], &error)
10116 && !error)
10117 {
10118 rettv->v_type = VAR_LIST;
10119 rettv->vval.v_list = NULL;
10120 }
10121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010122 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010123 if (*s == '%' || *s == '#' || *s == '<')
10124 {
10125 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010126 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010127 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010128 if (rettv->v_type == VAR_LIST)
10129 {
10130 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10131 list_append_string(rettv->vval.v_list, result, -1);
10132 else
10133 vim_free(result);
10134 }
10135 else
10136 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137 }
10138 else
10139 {
10140 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010141 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010142 if (argvars[1].v_type != VAR_UNKNOWN
10143 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010144 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010145 if (!error)
10146 {
10147 ExpandInit(&xpc);
10148 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010149 if (p_wic)
10150 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010151 if (rettv->v_type == VAR_STRING)
10152 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10153 options, WILD_ALL);
10154 else if (rettv_list_alloc(rettv) != FAIL)
10155 {
10156 int i;
10157
10158 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10159 for (i = 0; i < xpc.xp_numfiles; i++)
10160 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10161 ExpandCleanup(&xpc);
10162 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010163 }
10164 else
10165 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 }
10167}
10168
10169/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010170 * Go over all entries in "d2" and add them to "d1".
10171 * When "action" is "error" then a duplicate key is an error.
10172 * When "action" is "force" then a duplicate key is overwritten.
10173 * Otherwise duplicate keys are ignored ("action" is "keep").
10174 */
10175 void
10176dict_extend(d1, d2, action)
10177 dict_T *d1;
10178 dict_T *d2;
10179 char_u *action;
10180{
10181 dictitem_T *di1;
10182 hashitem_T *hi2;
10183 int todo;
10184
10185 todo = (int)d2->dv_hashtab.ht_used;
10186 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10187 {
10188 if (!HASHITEM_EMPTY(hi2))
10189 {
10190 --todo;
10191 di1 = dict_find(d1, hi2->hi_key, -1);
10192 if (d1->dv_scope != 0)
10193 {
10194 /* Disallow replacing a builtin function in l: and g:.
10195 * Check the key to be valid when adding to any
10196 * scope. */
10197 if (d1->dv_scope == VAR_DEF_SCOPE
10198 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10199 && var_check_func_name(hi2->hi_key,
10200 di1 == NULL))
10201 break;
10202 if (!valid_varname(hi2->hi_key))
10203 break;
10204 }
10205 if (di1 == NULL)
10206 {
10207 di1 = dictitem_copy(HI2DI(hi2));
10208 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10209 dictitem_free(di1);
10210 }
10211 else if (*action == 'e')
10212 {
10213 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10214 break;
10215 }
10216 else if (*action == 'f' && HI2DI(hi2) != di1)
10217 {
10218 clear_tv(&di1->di_tv);
10219 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10220 }
10221 }
10222 }
10223}
10224
10225/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010226 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010227 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010228 */
10229 static void
10230f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010231 typval_T *argvars;
10232 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010233{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010234 char *arg_errmsg = N_("extend() argument");
10235
Bram Moolenaare9a41262005-01-15 22:18:47 +000010236 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010237 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010238 list_T *l1, *l2;
10239 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010240 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010241 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010242
Bram Moolenaare9a41262005-01-15 22:18:47 +000010243 l1 = argvars[0].vval.v_list;
10244 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010245 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010246 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010247 {
10248 if (argvars[2].v_type != VAR_UNKNOWN)
10249 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010250 before = get_tv_number_chk(&argvars[2], &error);
10251 if (error)
10252 return; /* type error; errmsg already given */
10253
Bram Moolenaar758711c2005-02-02 23:11:38 +000010254 if (before == l1->lv_len)
10255 item = NULL;
10256 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010257 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010258 item = list_find(l1, before);
10259 if (item == NULL)
10260 {
10261 EMSGN(_(e_listidx), before);
10262 return;
10263 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010264 }
10265 }
10266 else
10267 item = NULL;
10268 list_extend(l1, l2, item);
10269
Bram Moolenaare9a41262005-01-15 22:18:47 +000010270 copy_tv(&argvars[0], rettv);
10271 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010272 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010273 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10274 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010275 dict_T *d1, *d2;
10276 char_u *action;
10277 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010278
10279 d1 = argvars[0].vval.v_dict;
10280 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010281 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010282 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010283 {
10284 /* Check the third argument. */
10285 if (argvars[2].v_type != VAR_UNKNOWN)
10286 {
10287 static char *(av[]) = {"keep", "force", "error"};
10288
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010289 action = get_tv_string_chk(&argvars[2]);
10290 if (action == NULL)
10291 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010292 for (i = 0; i < 3; ++i)
10293 if (STRCMP(action, av[i]) == 0)
10294 break;
10295 if (i == 3)
10296 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010297 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010298 return;
10299 }
10300 }
10301 else
10302 action = (char_u *)"force";
10303
Bram Moolenaara9922d62013-05-30 13:01:18 +020010304 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010305
Bram Moolenaare9a41262005-01-15 22:18:47 +000010306 copy_tv(&argvars[0], rettv);
10307 }
10308 }
10309 else
10310 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010311}
10312
10313/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010314 * "feedkeys()" function
10315 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010316 static void
10317f_feedkeys(argvars, rettv)
10318 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010319 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010320{
10321 int remap = TRUE;
10322 char_u *keys, *flags;
10323 char_u nbuf[NUMBUFLEN];
10324 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010325 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010326
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010327 /* This is not allowed in the sandbox. If the commands would still be
10328 * executed in the sandbox it would be OK, but it probably happens later,
10329 * when "sandbox" is no longer set. */
10330 if (check_secure())
10331 return;
10332
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010333 keys = get_tv_string(&argvars[0]);
10334 if (*keys != NUL)
10335 {
10336 if (argvars[1].v_type != VAR_UNKNOWN)
10337 {
10338 flags = get_tv_string_buf(&argvars[1], nbuf);
10339 for ( ; *flags != NUL; ++flags)
10340 {
10341 switch (*flags)
10342 {
10343 case 'n': remap = FALSE; break;
10344 case 'm': remap = TRUE; break;
10345 case 't': typed = TRUE; break;
10346 }
10347 }
10348 }
10349
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010350 /* Need to escape K_SPECIAL and CSI before putting the string in the
10351 * typeahead buffer. */
10352 keys_esc = vim_strsave_escape_csi(keys);
10353 if (keys_esc != NULL)
10354 {
10355 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010356 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010357 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010358 if (vgetc_busy)
10359 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010360 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010361 }
10362}
10363
10364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010365 * "filereadable()" function
10366 */
10367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010368f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010369 typval_T *argvars;
10370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010371{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010372 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 char_u *p;
10374 int n;
10375
Bram Moolenaarc236c162008-07-13 17:41:49 +000010376#ifndef O_NONBLOCK
10377# define O_NONBLOCK 0
10378#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010379 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010380 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10381 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 {
10383 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010384 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385 }
10386 else
10387 n = FALSE;
10388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010389 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390}
10391
10392/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010393 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 * rights to write into.
10395 */
10396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010397f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010398 typval_T *argvars;
10399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010401 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402}
10403
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010404static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010405
10406 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010407findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010408 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010409 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010410 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010411{
10412#ifdef FEAT_SEARCHPATH
10413 char_u *fname;
10414 char_u *fresult = NULL;
10415 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10416 char_u *p;
10417 char_u pathbuf[NUMBUFLEN];
10418 int count = 1;
10419 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010420 int error = FALSE;
10421#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010422
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010423 rettv->vval.v_string = NULL;
10424 rettv->v_type = VAR_STRING;
10425
10426#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010427 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010428
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010429 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010431 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10432 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010433 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010434 else
10435 {
10436 if (*p != NUL)
10437 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010440 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010441 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010442 }
10443
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010444 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10445 error = TRUE;
10446
10447 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010448 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010449 do
10450 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010451 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010452 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010453 fresult = find_file_in_path_option(first ? fname : NULL,
10454 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010455 0, first, path,
10456 find_what,
10457 curbuf->b_ffname,
10458 find_what == FINDFILE_DIR
10459 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010460 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010461
10462 if (fresult != NULL && rettv->v_type == VAR_LIST)
10463 list_append_string(rettv->vval.v_list, fresult, -1);
10464
10465 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010467
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010468 if (rettv->v_type == VAR_STRING)
10469 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010470#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010471}
10472
Bram Moolenaar33570922005-01-25 22:26:29 +000010473static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10474static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010475
10476/*
10477 * Implementation of map() and filter().
10478 */
10479 static void
10480filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010481 typval_T *argvars;
10482 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010483 int map;
10484{
10485 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010486 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010487 listitem_T *li, *nli;
10488 list_T *l = NULL;
10489 dictitem_T *di;
10490 hashtab_T *ht;
10491 hashitem_T *hi;
10492 dict_T *d = NULL;
10493 typval_T save_val;
10494 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010495 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010496 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010497 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10498 char *arg_errmsg = (map ? N_("map() argument")
10499 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010500 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010501 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010502
Bram Moolenaare9a41262005-01-15 22:18:47 +000010503 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010504 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010505 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010506 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010507 return;
10508 }
10509 else if (argvars[0].v_type == VAR_DICT)
10510 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010511 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010512 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010513 return;
10514 }
10515 else
10516 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010517 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010518 return;
10519 }
10520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010521 expr = get_tv_string_buf_chk(&argvars[1], buf);
10522 /* On type errors, the preceding call has already displayed an error
10523 * message. Avoid a misleading error message for an empty string that
10524 * was not passed as argument. */
10525 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010527 prepare_vimvar(VV_VAL, &save_val);
10528 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010529
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010530 /* We reset "did_emsg" to be able to detect whether an error
10531 * occurred during evaluation of the expression. */
10532 save_did_emsg = did_emsg;
10533 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010534
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010535 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010538 vimvars[VV_KEY].vv_type = VAR_STRING;
10539
10540 ht = &d->dv_hashtab;
10541 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010542 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010543 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010544 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 if (!HASHITEM_EMPTY(hi))
10546 {
10547 --todo;
10548 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010549 if (tv_check_lock(di->di_tv.v_lock,
10550 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010551 break;
10552 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010553 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010554 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010555 break;
10556 if (!map && rem)
10557 dictitem_remove(d, di);
10558 clear_tv(&vimvars[VV_KEY].vv_tv);
10559 }
10560 }
10561 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010562 }
10563 else
10564 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010565 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010567 for (li = l->lv_first; li != NULL; li = nli)
10568 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010569 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010570 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010572 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010573 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010574 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010575 break;
10576 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010577 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010578 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010579 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010580 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010581
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010582 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010584
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010585 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010586 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010587
10588 copy_tv(&argvars[0], rettv);
10589}
10590
10591 static int
10592filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010593 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010594 char_u *expr;
10595 int map;
10596 int *remp;
10597{
Bram Moolenaar33570922005-01-25 22:26:29 +000010598 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010599 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010600 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010601
Bram Moolenaar33570922005-01-25 22:26:29 +000010602 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 s = expr;
10604 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010605 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010606 if (*s != NUL) /* check for trailing chars after expr */
10607 {
10608 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010609 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010610 }
10611 if (map)
10612 {
10613 /* map(): replace the list item value */
10614 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010615 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010616 *tv = rettv;
10617 }
10618 else
10619 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010620 int error = FALSE;
10621
Bram Moolenaare9a41262005-01-15 22:18:47 +000010622 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010623 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010624 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010625 /* On type error, nothing has been removed; return FAIL to stop the
10626 * loop. The error message was given by get_tv_number_chk(). */
10627 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010628 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010629 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010630 retval = OK;
10631theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010632 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010633 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010634}
10635
10636/*
10637 * "filter()" function
10638 */
10639 static void
10640f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010641 typval_T *argvars;
10642 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010643{
10644 filter_map(argvars, rettv, FALSE);
10645}
10646
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010647/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010648 * "finddir({fname}[, {path}[, {count}]])" function
10649 */
10650 static void
10651f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010652 typval_T *argvars;
10653 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010654{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010655 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010656}
10657
10658/*
10659 * "findfile({fname}[, {path}[, {count}]])" function
10660 */
10661 static void
10662f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010663 typval_T *argvars;
10664 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010665{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010666 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010667}
10668
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010669#ifdef FEAT_FLOAT
10670/*
10671 * "float2nr({float})" function
10672 */
10673 static void
10674f_float2nr(argvars, rettv)
10675 typval_T *argvars;
10676 typval_T *rettv;
10677{
10678 float_T f;
10679
10680 if (get_float_arg(argvars, &f) == OK)
10681 {
10682 if (f < -0x7fffffff)
10683 rettv->vval.v_number = -0x7fffffff;
10684 else if (f > 0x7fffffff)
10685 rettv->vval.v_number = 0x7fffffff;
10686 else
10687 rettv->vval.v_number = (varnumber_T)f;
10688 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010689}
10690
10691/*
10692 * "floor({float})" function
10693 */
10694 static void
10695f_floor(argvars, rettv)
10696 typval_T *argvars;
10697 typval_T *rettv;
10698{
10699 float_T f;
10700
10701 rettv->v_type = VAR_FLOAT;
10702 if (get_float_arg(argvars, &f) == OK)
10703 rettv->vval.v_float = floor(f);
10704 else
10705 rettv->vval.v_float = 0.0;
10706}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010707
10708/*
10709 * "fmod()" function
10710 */
10711 static void
10712f_fmod(argvars, rettv)
10713 typval_T *argvars;
10714 typval_T *rettv;
10715{
10716 float_T fx, fy;
10717
10718 rettv->v_type = VAR_FLOAT;
10719 if (get_float_arg(argvars, &fx) == OK
10720 && get_float_arg(&argvars[1], &fy) == OK)
10721 rettv->vval.v_float = fmod(fx, fy);
10722 else
10723 rettv->vval.v_float = 0.0;
10724}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010725#endif
10726
Bram Moolenaar0d660222005-01-07 21:51:51 +000010727/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010728 * "fnameescape({string})" function
10729 */
10730 static void
10731f_fnameescape(argvars, rettv)
10732 typval_T *argvars;
10733 typval_T *rettv;
10734{
10735 rettv->vval.v_string = vim_strsave_fnameescape(
10736 get_tv_string(&argvars[0]), FALSE);
10737 rettv->v_type = VAR_STRING;
10738}
10739
10740/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741 * "fnamemodify({fname}, {mods})" function
10742 */
10743 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010744f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010745 typval_T *argvars;
10746 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747{
10748 char_u *fname;
10749 char_u *mods;
10750 int usedlen = 0;
10751 int len;
10752 char_u *fbuf = NULL;
10753 char_u buf[NUMBUFLEN];
10754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010755 fname = get_tv_string_chk(&argvars[0]);
10756 mods = get_tv_string_buf_chk(&argvars[1], buf);
10757 if (fname == NULL || mods == NULL)
10758 fname = NULL;
10759 else
10760 {
10761 len = (int)STRLEN(fname);
10762 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010765 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010766 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010767 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010769 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010770 vim_free(fbuf);
10771}
10772
Bram Moolenaar33570922005-01-25 22:26:29 +000010773static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774
10775/*
10776 * "foldclosed()" function
10777 */
10778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010779foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010780 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010781 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010782 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783{
10784#ifdef FEAT_FOLDING
10785 linenr_T lnum;
10786 linenr_T first, last;
10787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10790 {
10791 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10792 {
10793 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010794 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 return;
10798 }
10799 }
10800#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010801 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010802}
10803
10804/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010805 * "foldclosed()" function
10806 */
10807 static void
10808f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010809 typval_T *argvars;
10810 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010811{
10812 foldclosed_both(argvars, rettv, FALSE);
10813}
10814
10815/*
10816 * "foldclosedend()" function
10817 */
10818 static void
10819f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010820 typval_T *argvars;
10821 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010822{
10823 foldclosed_both(argvars, rettv, TRUE);
10824}
10825
10826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827 * "foldlevel()" function
10828 */
10829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010830f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010831 typval_T *argvars UNUSED;
10832 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010833{
10834#ifdef FEAT_FOLDING
10835 linenr_T lnum;
10836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010837 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010839 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841}
10842
10843/*
10844 * "foldtext()" function
10845 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010847f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010848 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010849 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850{
10851#ifdef FEAT_FOLDING
10852 linenr_T lnum;
10853 char_u *s;
10854 char_u *r;
10855 int len;
10856 char *txt;
10857#endif
10858
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010859 rettv->v_type = VAR_STRING;
10860 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010861#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010862 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10863 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10864 <= curbuf->b_ml.ml_line_count
10865 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866 {
10867 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010868 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10869 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870 {
10871 if (!linewhite(lnum))
10872 break;
10873 ++lnum;
10874 }
10875
10876 /* Find interesting text in this line. */
10877 s = skipwhite(ml_get(lnum));
10878 /* skip C comment-start */
10879 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010880 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010882 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010883 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010884 {
10885 s = skipwhite(ml_get(lnum + 1));
10886 if (*s == '*')
10887 s = skipwhite(s + 1);
10888 }
10889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 txt = _("+-%s%3ld lines: ");
10891 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010892 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 + 20 /* for %3ld */
10894 + STRLEN(s))); /* concatenated */
10895 if (r != NULL)
10896 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010897 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10898 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10899 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 len = (int)STRLEN(r);
10901 STRCAT(r, s);
10902 /* remove 'foldmarker' and 'commentstring' */
10903 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010904 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 }
10906 }
10907#endif
10908}
10909
10910/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010911 * "foldtextresult(lnum)" function
10912 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010914f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010915 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010916 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010917{
10918#ifdef FEAT_FOLDING
10919 linenr_T lnum;
10920 char_u *text;
10921 char_u buf[51];
10922 foldinfo_T foldinfo;
10923 int fold_count;
10924#endif
10925
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010926 rettv->v_type = VAR_STRING;
10927 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010928#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010929 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010930 /* treat illegal types and illegal string values for {lnum} the same */
10931 if (lnum < 0)
10932 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010933 fold_count = foldedCount(curwin, lnum, &foldinfo);
10934 if (fold_count > 0)
10935 {
10936 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10937 &foldinfo, buf);
10938 if (text == buf)
10939 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010940 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010941 }
10942#endif
10943}
10944
10945/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010946 * "foreground()" function
10947 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010950 typval_T *argvars UNUSED;
10951 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010953#ifdef FEAT_GUI
10954 if (gui.in_use)
10955 gui_mch_set_foreground();
10956#else
10957# ifdef WIN32
10958 win32_set_foreground();
10959# endif
10960#endif
10961}
10962
10963/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010964 * "function()" function
10965 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010966 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010967f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010968 typval_T *argvars;
10969 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010970{
10971 char_u *s;
10972
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010973 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010974 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010975 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010976 /* Don't check an autoload name for existence here. */
10977 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010978 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010979 else
10980 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010981 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010982 {
10983 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010984 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010985
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010986 /* Expand s: and <SID> into <SNR>nr_, so that the function can
10987 * also be called from another script. Using trans_function_name()
10988 * would also work, but some plugins depend on the name being
10989 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010990 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020010991 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010992 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010993 if (rettv->vval.v_string != NULL)
10994 {
10995 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010996 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010997 }
10998 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020010999 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011000 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011001 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011002 }
11003}
11004
11005/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011006 * "garbagecollect()" function
11007 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011008 static void
11009f_garbagecollect(argvars, rettv)
11010 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011011 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011012{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011013 /* This is postponed until we are back at the toplevel, because we may be
11014 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11015 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011016
11017 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11018 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011019}
11020
11021/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011022 * "get()" function
11023 */
11024 static void
11025f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011026 typval_T *argvars;
11027 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011028{
Bram Moolenaar33570922005-01-25 22:26:29 +000011029 listitem_T *li;
11030 list_T *l;
11031 dictitem_T *di;
11032 dict_T *d;
11033 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011034
Bram Moolenaare9a41262005-01-15 22:18:47 +000011035 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011036 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011037 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011038 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011039 int error = FALSE;
11040
11041 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11042 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011043 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011044 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011045 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011046 else if (argvars[0].v_type == VAR_DICT)
11047 {
11048 if ((d = argvars[0].vval.v_dict) != NULL)
11049 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011050 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011051 if (di != NULL)
11052 tv = &di->di_tv;
11053 }
11054 }
11055 else
11056 EMSG2(_(e_listdictarg), "get()");
11057
11058 if (tv == NULL)
11059 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011060 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011061 copy_tv(&argvars[2], rettv);
11062 }
11063 else
11064 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011065}
11066
Bram Moolenaar342337a2005-07-21 21:11:17 +000011067static 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 +000011068
11069/*
11070 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011071 * Return a range (from start to end) of lines in rettv from the specified
11072 * buffer.
11073 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011074 */
11075 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011076get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011077 buf_T *buf;
11078 linenr_T start;
11079 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011080 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011081 typval_T *rettv;
11082{
11083 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011084
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011085 if (retlist && rettv_list_alloc(rettv) == FAIL)
11086 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011087
11088 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11089 return;
11090
11091 if (!retlist)
11092 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011093 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11094 p = ml_get_buf(buf, start, FALSE);
11095 else
11096 p = (char_u *)"";
11097
11098 rettv->v_type = VAR_STRING;
11099 rettv->vval.v_string = vim_strsave(p);
11100 }
11101 else
11102 {
11103 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011104 return;
11105
11106 if (start < 1)
11107 start = 1;
11108 if (end > buf->b_ml.ml_line_count)
11109 end = buf->b_ml.ml_line_count;
11110 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011111 if (list_append_string(rettv->vval.v_list,
11112 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011113 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011114 }
11115}
11116
11117/*
11118 * "getbufline()" function
11119 */
11120 static void
11121f_getbufline(argvars, rettv)
11122 typval_T *argvars;
11123 typval_T *rettv;
11124{
11125 linenr_T lnum;
11126 linenr_T end;
11127 buf_T *buf;
11128
11129 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11130 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011131 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011132 --emsg_off;
11133
Bram Moolenaar661b1822005-07-28 22:36:45 +000011134 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011135 if (argvars[2].v_type == VAR_UNKNOWN)
11136 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011137 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011138 end = get_tv_lnum_buf(&argvars[2], buf);
11139
Bram Moolenaar342337a2005-07-21 21:11:17 +000011140 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011141}
11142
Bram Moolenaar0d660222005-01-07 21:51:51 +000011143/*
11144 * "getbufvar()" function
11145 */
11146 static void
11147f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011148 typval_T *argvars;
11149 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011150{
11151 buf_T *buf;
11152 buf_T *save_curbuf;
11153 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011154 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011155 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011156
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011157 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11158 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011159 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011160 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011161
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011162 rettv->v_type = VAR_STRING;
11163 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011164
11165 if (buf != NULL && varname != NULL)
11166 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011167 /* set curbuf to be our buf, temporarily */
11168 save_curbuf = curbuf;
11169 curbuf = buf;
11170
Bram Moolenaar0d660222005-01-07 21:51:51 +000011171 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011172 {
11173 if (get_option_tv(&varname, rettv, TRUE) == OK)
11174 done = TRUE;
11175 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011176 else if (STRCMP(varname, "changedtick") == 0)
11177 {
11178 rettv->v_type = VAR_NUMBER;
11179 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011180 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011181 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011182 else
11183 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011184 /* Look up the variable. */
11185 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11186 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11187 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011188 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011189 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011190 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011191 done = TRUE;
11192 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011193 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011194
11195 /* restore previous notion of curbuf */
11196 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011197 }
11198
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011199 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11200 /* use the default value */
11201 copy_tv(&argvars[2], rettv);
11202
Bram Moolenaar0d660222005-01-07 21:51:51 +000011203 --emsg_off;
11204}
11205
11206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207 * "getchar()" function
11208 */
11209 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011210f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011211 typval_T *argvars;
11212 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011213{
11214 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011215 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011217 /* Position the cursor. Needed after a message that ends in a space. */
11218 windgoto(msg_row, msg_col);
11219
Bram Moolenaar071d4272004-06-13 20:20:40 +000011220 ++no_mapping;
11221 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011222 for (;;)
11223 {
11224 if (argvars[0].v_type == VAR_UNKNOWN)
11225 /* getchar(): blocking wait. */
11226 n = safe_vgetc();
11227 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11228 /* getchar(1): only check if char avail */
11229 n = vpeekc();
11230 else if (error || vpeekc() == NUL)
11231 /* illegal argument or getchar(0) and no char avail: return zero */
11232 n = 0;
11233 else
11234 /* getchar(0) and char avail: return char */
11235 n = safe_vgetc();
11236 if (n == K_IGNORE)
11237 continue;
11238 break;
11239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011240 --no_mapping;
11241 --allow_keys;
11242
Bram Moolenaar219b8702006-11-01 14:32:36 +000011243 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11244 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11245 vimvars[VV_MOUSE_COL].vv_nr = 0;
11246
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011247 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 if (IS_SPECIAL(n) || mod_mask != 0)
11249 {
11250 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11251 int i = 0;
11252
11253 /* Turn a special key into three bytes, plus modifier. */
11254 if (mod_mask != 0)
11255 {
11256 temp[i++] = K_SPECIAL;
11257 temp[i++] = KS_MODIFIER;
11258 temp[i++] = mod_mask;
11259 }
11260 if (IS_SPECIAL(n))
11261 {
11262 temp[i++] = K_SPECIAL;
11263 temp[i++] = K_SECOND(n);
11264 temp[i++] = K_THIRD(n);
11265 }
11266#ifdef FEAT_MBYTE
11267 else if (has_mbyte)
11268 i += (*mb_char2bytes)(n, temp + i);
11269#endif
11270 else
11271 temp[i++] = n;
11272 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011273 rettv->v_type = VAR_STRING;
11274 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011275
11276#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011277 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011278 {
11279 int row = mouse_row;
11280 int col = mouse_col;
11281 win_T *win;
11282 linenr_T lnum;
11283# ifdef FEAT_WINDOWS
11284 win_T *wp;
11285# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011286 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011287
11288 if (row >= 0 && col >= 0)
11289 {
11290 /* Find the window at the mouse coordinates and compute the
11291 * text position. */
11292 win = mouse_find_win(&row, &col);
11293 (void)mouse_comp_pos(win, &row, &col, &lnum);
11294# ifdef FEAT_WINDOWS
11295 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011296 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011297# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011298 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011299 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11300 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11301 }
11302 }
11303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011304 }
11305}
11306
11307/*
11308 * "getcharmod()" function
11309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011310 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011311f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011312 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011313 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011314{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011315 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011316}
11317
11318/*
11319 * "getcmdline()" function
11320 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011322f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011323 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011324 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011326 rettv->v_type = VAR_STRING;
11327 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011328}
11329
11330/*
11331 * "getcmdpos()" function
11332 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011335 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011336 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011338 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339}
11340
11341/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011342 * "getcmdtype()" function
11343 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011344 static void
11345f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011346 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011347 typval_T *rettv;
11348{
11349 rettv->v_type = VAR_STRING;
11350 rettv->vval.v_string = alloc(2);
11351 if (rettv->vval.v_string != NULL)
11352 {
11353 rettv->vval.v_string[0] = get_cmdline_type();
11354 rettv->vval.v_string[1] = NUL;
11355 }
11356}
11357
11358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359 * "getcwd()" function
11360 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011361 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011362f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011363 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011364 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011365{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011366 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011368 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011369 rettv->vval.v_string = NULL;
11370 cwd = alloc(MAXPATHL);
11371 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011373 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11374 {
11375 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011377 if (rettv->vval.v_string != NULL)
11378 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011380 }
11381 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011382 }
11383}
11384
11385/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011386 * "getfontname()" function
11387 */
11388 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011390 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011391 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011392{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011393 rettv->v_type = VAR_STRING;
11394 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011395#ifdef FEAT_GUI
11396 if (gui.in_use)
11397 {
11398 GuiFont font;
11399 char_u *name = NULL;
11400
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011401 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011402 {
11403 /* Get the "Normal" font. Either the name saved by
11404 * hl_set_font_name() or from the font ID. */
11405 font = gui.norm_font;
11406 name = hl_get_font_name();
11407 }
11408 else
11409 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011410 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011411 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11412 return;
11413 font = gui_mch_get_font(name, FALSE);
11414 if (font == NOFONT)
11415 return; /* Invalid font name, return empty string. */
11416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011417 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011418 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011419 gui_mch_free_font(font);
11420 }
11421#endif
11422}
11423
11424/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011425 * "getfperm({fname})" function
11426 */
11427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011428f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011429 typval_T *argvars;
11430 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011431{
11432 char_u *fname;
11433 struct stat st;
11434 char_u *perm = NULL;
11435 char_u flags[] = "rwx";
11436 int i;
11437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011438 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011439
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011440 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011441 if (mch_stat((char *)fname, &st) >= 0)
11442 {
11443 perm = vim_strsave((char_u *)"---------");
11444 if (perm != NULL)
11445 {
11446 for (i = 0; i < 9; i++)
11447 {
11448 if (st.st_mode & (1 << (8 - i)))
11449 perm[i] = flags[i % 3];
11450 }
11451 }
11452 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011453 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011454}
11455
11456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457 * "getfsize({fname})" function
11458 */
11459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011460f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011461 typval_T *argvars;
11462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011463{
11464 char_u *fname;
11465 struct stat st;
11466
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011467 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470
11471 if (mch_stat((char *)fname, &st) >= 0)
11472 {
11473 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011474 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011476 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011478
11479 /* non-perfect check for overflow */
11480 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11481 rettv->vval.v_number = -2;
11482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 }
11484 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486}
11487
11488/*
11489 * "getftime({fname})" function
11490 */
11491 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011492f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011493 typval_T *argvars;
11494 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495{
11496 char_u *fname;
11497 struct stat st;
11498
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011499 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011500
11501 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011504 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505}
11506
11507/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011508 * "getftype({fname})" function
11509 */
11510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011512 typval_T *argvars;
11513 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011514{
11515 char_u *fname;
11516 struct stat st;
11517 char_u *type = NULL;
11518 char *t;
11519
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011520 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011522 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011523 if (mch_lstat((char *)fname, &st) >= 0)
11524 {
11525#ifdef S_ISREG
11526 if (S_ISREG(st.st_mode))
11527 t = "file";
11528 else if (S_ISDIR(st.st_mode))
11529 t = "dir";
11530# ifdef S_ISLNK
11531 else if (S_ISLNK(st.st_mode))
11532 t = "link";
11533# endif
11534# ifdef S_ISBLK
11535 else if (S_ISBLK(st.st_mode))
11536 t = "bdev";
11537# endif
11538# ifdef S_ISCHR
11539 else if (S_ISCHR(st.st_mode))
11540 t = "cdev";
11541# endif
11542# ifdef S_ISFIFO
11543 else if (S_ISFIFO(st.st_mode))
11544 t = "fifo";
11545# endif
11546# ifdef S_ISSOCK
11547 else if (S_ISSOCK(st.st_mode))
11548 t = "fifo";
11549# endif
11550 else
11551 t = "other";
11552#else
11553# ifdef S_IFMT
11554 switch (st.st_mode & S_IFMT)
11555 {
11556 case S_IFREG: t = "file"; break;
11557 case S_IFDIR: t = "dir"; break;
11558# ifdef S_IFLNK
11559 case S_IFLNK: t = "link"; break;
11560# endif
11561# ifdef S_IFBLK
11562 case S_IFBLK: t = "bdev"; break;
11563# endif
11564# ifdef S_IFCHR
11565 case S_IFCHR: t = "cdev"; break;
11566# endif
11567# ifdef S_IFIFO
11568 case S_IFIFO: t = "fifo"; break;
11569# endif
11570# ifdef S_IFSOCK
11571 case S_IFSOCK: t = "socket"; break;
11572# endif
11573 default: t = "other";
11574 }
11575# else
11576 if (mch_isdir(fname))
11577 t = "dir";
11578 else
11579 t = "file";
11580# endif
11581#endif
11582 type = vim_strsave((char_u *)t);
11583 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011584 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011585}
11586
11587/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011588 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011589 */
11590 static void
11591f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011592 typval_T *argvars;
11593 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011594{
11595 linenr_T lnum;
11596 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011597 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011598
11599 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011600 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011601 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011602 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011603 retlist = FALSE;
11604 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011605 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011606 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011607 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011608 retlist = TRUE;
11609 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011610
Bram Moolenaar342337a2005-07-21 21:11:17 +000011611 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011612}
11613
11614/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011615 * "getmatches()" function
11616 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011617 static void
11618f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011619 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011620 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011621{
11622#ifdef FEAT_SEARCH_EXTRA
11623 dict_T *dict;
11624 matchitem_T *cur = curwin->w_match_head;
11625
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011626 if (rettv_list_alloc(rettv) == OK)
11627 {
11628 while (cur != NULL)
11629 {
11630 dict = dict_alloc();
11631 if (dict == NULL)
11632 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011633 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11634 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11635 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11636 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11637 list_append_dict(rettv->vval.v_list, dict);
11638 cur = cur->next;
11639 }
11640 }
11641#endif
11642}
11643
11644/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011645 * "getpid()" function
11646 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011647 static void
11648f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011649 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011650 typval_T *rettv;
11651{
11652 rettv->vval.v_number = mch_get_pid();
11653}
11654
11655/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011656 * "getpos(string)" function
11657 */
11658 static void
11659f_getpos(argvars, rettv)
11660 typval_T *argvars;
11661 typval_T *rettv;
11662{
11663 pos_T *fp;
11664 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011665 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011666
11667 if (rettv_list_alloc(rettv) == OK)
11668 {
11669 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011670 fp = var2fpos(&argvars[0], TRUE, &fnum);
11671 if (fnum != -1)
11672 list_append_number(l, (varnumber_T)fnum);
11673 else
11674 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011675 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11676 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011677 list_append_number(l, (fp != NULL)
11678 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011679 : (varnumber_T)0);
11680 list_append_number(l,
11681#ifdef FEAT_VIRTUALEDIT
11682 (fp != NULL) ? (varnumber_T)fp->coladd :
11683#endif
11684 (varnumber_T)0);
11685 }
11686 else
11687 rettv->vval.v_number = FALSE;
11688}
11689
11690/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011691 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011692 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011693 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011694f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011695 typval_T *argvars UNUSED;
11696 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011697{
11698#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011699 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011700#endif
11701
Bram Moolenaar2641f772005-03-25 21:58:17 +000011702#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011703 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011704 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011705 wp = NULL;
11706 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11707 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011708 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011709 if (wp == NULL)
11710 return;
11711 }
11712
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011713 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011714 }
11715#endif
11716}
11717
11718/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 * "getreg()" function
11720 */
11721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011722f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011723 typval_T *argvars;
11724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725{
11726 char_u *strregname;
11727 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011728 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011729 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011731 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011733 strregname = get_tv_string_chk(&argvars[0]);
11734 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011735 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011736 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011739 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011740 regname = (strregname == NULL ? '"' : *strregname);
11741 if (regname == 0)
11742 regname = '"';
11743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011744 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011745 rettv->vval.v_string = error ? NULL :
11746 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747}
11748
11749/*
11750 * "getregtype()" function
11751 */
11752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011754 typval_T *argvars;
11755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756{
11757 char_u *strregname;
11758 int regname;
11759 char_u buf[NUMBUFLEN + 2];
11760 long reglen = 0;
11761
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011762 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011763 {
11764 strregname = get_tv_string_chk(&argvars[0]);
11765 if (strregname == NULL) /* type error; errmsg already given */
11766 {
11767 rettv->v_type = VAR_STRING;
11768 rettv->vval.v_string = NULL;
11769 return;
11770 }
11771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 else
11773 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011774 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775
11776 regname = (strregname == NULL ? '"' : *strregname);
11777 if (regname == 0)
11778 regname = '"';
11779
11780 buf[0] = NUL;
11781 buf[1] = NUL;
11782 switch (get_reg_type(regname, &reglen))
11783 {
11784 case MLINE: buf[0] = 'V'; break;
11785 case MCHAR: buf[0] = 'v'; break;
11786#ifdef FEAT_VISUAL
11787 case MBLOCK:
11788 buf[0] = Ctrl_V;
11789 sprintf((char *)buf + 1, "%ld", reglen + 1);
11790 break;
11791#endif
11792 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011793 rettv->v_type = VAR_STRING;
11794 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795}
11796
11797/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011798 * "gettabvar()" function
11799 */
11800 static void
11801f_gettabvar(argvars, rettv)
11802 typval_T *argvars;
11803 typval_T *rettv;
11804{
11805 tabpage_T *tp;
11806 dictitem_T *v;
11807 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011808 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011809
11810 rettv->v_type = VAR_STRING;
11811 rettv->vval.v_string = NULL;
11812
11813 varname = get_tv_string_chk(&argvars[1]);
11814 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11815 if (tp != NULL && varname != NULL)
11816 {
11817 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011818 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011819 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011820 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011821 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011822 done = TRUE;
11823 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011824 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011825
11826 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011827 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011828}
11829
11830/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011831 * "gettabwinvar()" function
11832 */
11833 static void
11834f_gettabwinvar(argvars, rettv)
11835 typval_T *argvars;
11836 typval_T *rettv;
11837{
11838 getwinvar(argvars, rettv, 1);
11839}
11840
11841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842 * "getwinposx()" function
11843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011846 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011848{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011849 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850#ifdef FEAT_GUI
11851 if (gui.in_use)
11852 {
11853 int x, y;
11854
11855 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011856 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857 }
11858#endif
11859}
11860
11861/*
11862 * "getwinposy()" function
11863 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011866 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011867 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011869 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870#ifdef FEAT_GUI
11871 if (gui.in_use)
11872 {
11873 int x, y;
11874
11875 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011876 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 }
11878#endif
11879}
11880
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011881/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011882 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011883 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011884 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011885find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011886 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011887 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011888{
11889#ifdef FEAT_WINDOWS
11890 win_T *wp;
11891#endif
11892 int nr;
11893
11894 nr = get_tv_number_chk(vp, NULL);
11895
11896#ifdef FEAT_WINDOWS
11897 if (nr < 0)
11898 return NULL;
11899 if (nr == 0)
11900 return curwin;
11901
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011902 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11903 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011904 if (--nr <= 0)
11905 break;
11906 return wp;
11907#else
11908 if (nr == 0 || nr == 1)
11909 return curwin;
11910 return NULL;
11911#endif
11912}
11913
Bram Moolenaar071d4272004-06-13 20:20:40 +000011914/*
11915 * "getwinvar()" function
11916 */
11917 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011918f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011919 typval_T *argvars;
11920 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011922 getwinvar(argvars, rettv, 0);
11923}
11924
11925/*
11926 * getwinvar() and gettabwinvar()
11927 */
11928 static void
11929getwinvar(argvars, rettv, off)
11930 typval_T *argvars;
11931 typval_T *rettv;
11932 int off; /* 1 for gettabwinvar() */
11933{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934 win_T *win, *oldcurwin;
11935 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011936 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011937 tabpage_T *tp = NULL;
11938 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011939 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011940
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011941#ifdef FEAT_WINDOWS
11942 if (off == 1)
11943 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11944 else
11945 tp = curtab;
11946#endif
11947 win = find_win_by_nr(&argvars[off], tp);
11948 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011949 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011950
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011951 rettv->v_type = VAR_STRING;
11952 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011953
11954 if (win != NULL && varname != NULL)
11955 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011956 /* Set curwin to be our win, temporarily. Also set the tabpage,
11957 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020011958 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011959
Bram Moolenaar071d4272004-06-13 20:20:40 +000011960 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011961 {
11962 if (get_option_tv(&varname, rettv, 1) == OK)
11963 done = TRUE;
11964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011965 else
11966 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011967 /* Look up the variable. */
11968 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11969 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011970 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011971 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011972 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011973 done = TRUE;
11974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011976
11977 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020011978 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 }
11980
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011981 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11982 /* use the default return value */
11983 copy_tv(&argvars[off + 2], rettv);
11984
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985 --emsg_off;
11986}
11987
11988/*
11989 * "glob()" function
11990 */
11991 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011992f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011993 typval_T *argvars;
11994 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011996 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011997 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011998 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011999
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012000 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012001 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012002 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012003 if (argvars[1].v_type != VAR_UNKNOWN)
12004 {
12005 if (get_tv_number_chk(&argvars[1], &error))
12006 options |= WILD_KEEP_ALL;
12007 if (argvars[2].v_type != VAR_UNKNOWN
12008 && get_tv_number_chk(&argvars[2], &error))
12009 {
12010 rettv->v_type = VAR_LIST;
12011 rettv->vval.v_list = NULL;
12012 }
12013 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012014 if (!error)
12015 {
12016 ExpandInit(&xpc);
12017 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012018 if (p_wic)
12019 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012020 if (rettv->v_type == VAR_STRING)
12021 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012022 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012023 else if (rettv_list_alloc(rettv) != FAIL)
12024 {
12025 int i;
12026
12027 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12028 NULL, options, WILD_ALL_KEEP);
12029 for (i = 0; i < xpc.xp_numfiles; i++)
12030 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12031
12032 ExpandCleanup(&xpc);
12033 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012034 }
12035 else
12036 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012037}
12038
12039/*
12040 * "globpath()" function
12041 */
12042 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012043f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012044 typval_T *argvars;
12045 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012047 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012048 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012049 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012050 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012051
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012052 /* When the optional second argument is non-zero, don't remove matches
12053 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12054 if (argvars[2].v_type != VAR_UNKNOWN
12055 && get_tv_number_chk(&argvars[2], &error))
12056 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012057 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012058 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012059 rettv->vval.v_string = NULL;
12060 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012061 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12062 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012063}
12064
12065/*
12066 * "has()" function
12067 */
12068 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012069f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012070 typval_T *argvars;
12071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072{
12073 int i;
12074 char_u *name;
12075 int n = FALSE;
12076 static char *(has_list[]) =
12077 {
12078#ifdef AMIGA
12079 "amiga",
12080# ifdef FEAT_ARP
12081 "arp",
12082# endif
12083#endif
12084#ifdef __BEOS__
12085 "beos",
12086#endif
12087#ifdef MSDOS
12088# ifdef DJGPP
12089 "dos32",
12090# else
12091 "dos16",
12092# endif
12093#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012094#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012095 "mac",
12096#endif
12097#if defined(MACOS_X_UNIX)
12098 "macunix",
12099#endif
12100#ifdef OS2
12101 "os2",
12102#endif
12103#ifdef __QNX__
12104 "qnx",
12105#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106#ifdef UNIX
12107 "unix",
12108#endif
12109#ifdef VMS
12110 "vms",
12111#endif
12112#ifdef WIN16
12113 "win16",
12114#endif
12115#ifdef WIN32
12116 "win32",
12117#endif
12118#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12119 "win32unix",
12120#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012121#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012122 "win64",
12123#endif
12124#ifdef EBCDIC
12125 "ebcdic",
12126#endif
12127#ifndef CASE_INSENSITIVE_FILENAME
12128 "fname_case",
12129#endif
12130#ifdef FEAT_ARABIC
12131 "arabic",
12132#endif
12133#ifdef FEAT_AUTOCMD
12134 "autocmd",
12135#endif
12136#ifdef FEAT_BEVAL
12137 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012138# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12139 "balloon_multiline",
12140# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141#endif
12142#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12143 "builtin_terms",
12144# ifdef ALL_BUILTIN_TCAPS
12145 "all_builtin_terms",
12146# endif
12147#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012148#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12149 || defined(FEAT_GUI_W32) \
12150 || defined(FEAT_GUI_MOTIF))
12151 "browsefilter",
12152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153#ifdef FEAT_BYTEOFF
12154 "byte_offset",
12155#endif
12156#ifdef FEAT_CINDENT
12157 "cindent",
12158#endif
12159#ifdef FEAT_CLIENTSERVER
12160 "clientserver",
12161#endif
12162#ifdef FEAT_CLIPBOARD
12163 "clipboard",
12164#endif
12165#ifdef FEAT_CMDL_COMPL
12166 "cmdline_compl",
12167#endif
12168#ifdef FEAT_CMDHIST
12169 "cmdline_hist",
12170#endif
12171#ifdef FEAT_COMMENTS
12172 "comments",
12173#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012174#ifdef FEAT_CONCEAL
12175 "conceal",
12176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177#ifdef FEAT_CRYPT
12178 "cryptv",
12179#endif
12180#ifdef FEAT_CSCOPE
12181 "cscope",
12182#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012183#ifdef FEAT_CURSORBIND
12184 "cursorbind",
12185#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012186#ifdef CURSOR_SHAPE
12187 "cursorshape",
12188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189#ifdef DEBUG
12190 "debug",
12191#endif
12192#ifdef FEAT_CON_DIALOG
12193 "dialog_con",
12194#endif
12195#ifdef FEAT_GUI_DIALOG
12196 "dialog_gui",
12197#endif
12198#ifdef FEAT_DIFF
12199 "diff",
12200#endif
12201#ifdef FEAT_DIGRAPHS
12202 "digraphs",
12203#endif
12204#ifdef FEAT_DND
12205 "dnd",
12206#endif
12207#ifdef FEAT_EMACS_TAGS
12208 "emacs_tags",
12209#endif
12210 "eval", /* always present, of course! */
12211#ifdef FEAT_EX_EXTRA
12212 "ex_extra",
12213#endif
12214#ifdef FEAT_SEARCH_EXTRA
12215 "extra_search",
12216#endif
12217#ifdef FEAT_FKMAP
12218 "farsi",
12219#endif
12220#ifdef FEAT_SEARCHPATH
12221 "file_in_path",
12222#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012223#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012224 "filterpipe",
12225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226#ifdef FEAT_FIND_ID
12227 "find_in_path",
12228#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012229#ifdef FEAT_FLOAT
12230 "float",
12231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232#ifdef FEAT_FOLDING
12233 "folding",
12234#endif
12235#ifdef FEAT_FOOTER
12236 "footer",
12237#endif
12238#if !defined(USE_SYSTEM) && defined(UNIX)
12239 "fork",
12240#endif
12241#ifdef FEAT_GETTEXT
12242 "gettext",
12243#endif
12244#ifdef FEAT_GUI
12245 "gui",
12246#endif
12247#ifdef FEAT_GUI_ATHENA
12248# ifdef FEAT_GUI_NEXTAW
12249 "gui_neXtaw",
12250# else
12251 "gui_athena",
12252# endif
12253#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012254#ifdef FEAT_GUI_GTK
12255 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012257#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012258#ifdef FEAT_GUI_GNOME
12259 "gui_gnome",
12260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012261#ifdef FEAT_GUI_MAC
12262 "gui_mac",
12263#endif
12264#ifdef FEAT_GUI_MOTIF
12265 "gui_motif",
12266#endif
12267#ifdef FEAT_GUI_PHOTON
12268 "gui_photon",
12269#endif
12270#ifdef FEAT_GUI_W16
12271 "gui_win16",
12272#endif
12273#ifdef FEAT_GUI_W32
12274 "gui_win32",
12275#endif
12276#ifdef FEAT_HANGULIN
12277 "hangul_input",
12278#endif
12279#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12280 "iconv",
12281#endif
12282#ifdef FEAT_INS_EXPAND
12283 "insert_expand",
12284#endif
12285#ifdef FEAT_JUMPLIST
12286 "jumplist",
12287#endif
12288#ifdef FEAT_KEYMAP
12289 "keymap",
12290#endif
12291#ifdef FEAT_LANGMAP
12292 "langmap",
12293#endif
12294#ifdef FEAT_LIBCALL
12295 "libcall",
12296#endif
12297#ifdef FEAT_LINEBREAK
12298 "linebreak",
12299#endif
12300#ifdef FEAT_LISP
12301 "lispindent",
12302#endif
12303#ifdef FEAT_LISTCMDS
12304 "listcmds",
12305#endif
12306#ifdef FEAT_LOCALMAP
12307 "localmap",
12308#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012309#ifdef FEAT_LUA
12310# ifndef DYNAMIC_LUA
12311 "lua",
12312# endif
12313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314#ifdef FEAT_MENU
12315 "menu",
12316#endif
12317#ifdef FEAT_SESSION
12318 "mksession",
12319#endif
12320#ifdef FEAT_MODIFY_FNAME
12321 "modify_fname",
12322#endif
12323#ifdef FEAT_MOUSE
12324 "mouse",
12325#endif
12326#ifdef FEAT_MOUSESHAPE
12327 "mouseshape",
12328#endif
12329#if defined(UNIX) || defined(VMS)
12330# ifdef FEAT_MOUSE_DEC
12331 "mouse_dec",
12332# endif
12333# ifdef FEAT_MOUSE_GPM
12334 "mouse_gpm",
12335# endif
12336# ifdef FEAT_MOUSE_JSB
12337 "mouse_jsbterm",
12338# endif
12339# ifdef FEAT_MOUSE_NET
12340 "mouse_netterm",
12341# endif
12342# ifdef FEAT_MOUSE_PTERM
12343 "mouse_pterm",
12344# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012345# ifdef FEAT_MOUSE_SGR
12346 "mouse_sgr",
12347# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012348# ifdef FEAT_SYSMOUSE
12349 "mouse_sysmouse",
12350# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012351# ifdef FEAT_MOUSE_URXVT
12352 "mouse_urxvt",
12353# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354# ifdef FEAT_MOUSE_XTERM
12355 "mouse_xterm",
12356# endif
12357#endif
12358#ifdef FEAT_MBYTE
12359 "multi_byte",
12360#endif
12361#ifdef FEAT_MBYTE_IME
12362 "multi_byte_ime",
12363#endif
12364#ifdef FEAT_MULTI_LANG
12365 "multi_lang",
12366#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012367#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012368#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012369 "mzscheme",
12370#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372#ifdef FEAT_OLE
12373 "ole",
12374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012375#ifdef FEAT_PATH_EXTRA
12376 "path_extra",
12377#endif
12378#ifdef FEAT_PERL
12379#ifndef DYNAMIC_PERL
12380 "perl",
12381#endif
12382#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012383#ifdef FEAT_PERSISTENT_UNDO
12384 "persistent_undo",
12385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386#ifdef FEAT_PYTHON
12387#ifndef DYNAMIC_PYTHON
12388 "python",
12389#endif
12390#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012391#ifdef FEAT_PYTHON3
12392#ifndef DYNAMIC_PYTHON3
12393 "python3",
12394#endif
12395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012396#ifdef FEAT_POSTSCRIPT
12397 "postscript",
12398#endif
12399#ifdef FEAT_PRINTER
12400 "printer",
12401#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012402#ifdef FEAT_PROFILE
12403 "profile",
12404#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012405#ifdef FEAT_RELTIME
12406 "reltime",
12407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408#ifdef FEAT_QUICKFIX
12409 "quickfix",
12410#endif
12411#ifdef FEAT_RIGHTLEFT
12412 "rightleft",
12413#endif
12414#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12415 "ruby",
12416#endif
12417#ifdef FEAT_SCROLLBIND
12418 "scrollbind",
12419#endif
12420#ifdef FEAT_CMDL_INFO
12421 "showcmd",
12422 "cmdline_info",
12423#endif
12424#ifdef FEAT_SIGNS
12425 "signs",
12426#endif
12427#ifdef FEAT_SMARTINDENT
12428 "smartindent",
12429#endif
12430#ifdef FEAT_SNIFF
12431 "sniff",
12432#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012433#ifdef STARTUPTIME
12434 "startuptime",
12435#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436#ifdef FEAT_STL_OPT
12437 "statusline",
12438#endif
12439#ifdef FEAT_SUN_WORKSHOP
12440 "sun_workshop",
12441#endif
12442#ifdef FEAT_NETBEANS_INTG
12443 "netbeans_intg",
12444#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012445#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012446 "spell",
12447#endif
12448#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449 "syntax",
12450#endif
12451#if defined(USE_SYSTEM) || !defined(UNIX)
12452 "system",
12453#endif
12454#ifdef FEAT_TAG_BINS
12455 "tag_binary",
12456#endif
12457#ifdef FEAT_TAG_OLDSTATIC
12458 "tag_old_static",
12459#endif
12460#ifdef FEAT_TAG_ANYWHITE
12461 "tag_any_white",
12462#endif
12463#ifdef FEAT_TCL
12464# ifndef DYNAMIC_TCL
12465 "tcl",
12466# endif
12467#endif
12468#ifdef TERMINFO
12469 "terminfo",
12470#endif
12471#ifdef FEAT_TERMRESPONSE
12472 "termresponse",
12473#endif
12474#ifdef FEAT_TEXTOBJ
12475 "textobjects",
12476#endif
12477#ifdef HAVE_TGETENT
12478 "tgetent",
12479#endif
12480#ifdef FEAT_TITLE
12481 "title",
12482#endif
12483#ifdef FEAT_TOOLBAR
12484 "toolbar",
12485#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012486#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12487 "unnamedplus",
12488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489#ifdef FEAT_USR_CMDS
12490 "user-commands", /* was accidentally included in 5.4 */
12491 "user_commands",
12492#endif
12493#ifdef FEAT_VIMINFO
12494 "viminfo",
12495#endif
12496#ifdef FEAT_VERTSPLIT
12497 "vertsplit",
12498#endif
12499#ifdef FEAT_VIRTUALEDIT
12500 "virtualedit",
12501#endif
12502#ifdef FEAT_VISUAL
12503 "visual",
12504#endif
12505#ifdef FEAT_VISUALEXTRA
12506 "visualextra",
12507#endif
12508#ifdef FEAT_VREPLACE
12509 "vreplace",
12510#endif
12511#ifdef FEAT_WILDIGN
12512 "wildignore",
12513#endif
12514#ifdef FEAT_WILDMENU
12515 "wildmenu",
12516#endif
12517#ifdef FEAT_WINDOWS
12518 "windows",
12519#endif
12520#ifdef FEAT_WAK
12521 "winaltkeys",
12522#endif
12523#ifdef FEAT_WRITEBACKUP
12524 "writebackup",
12525#endif
12526#ifdef FEAT_XIM
12527 "xim",
12528#endif
12529#ifdef FEAT_XFONTSET
12530 "xfontset",
12531#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012532#ifdef FEAT_XPM_W32
12533 "xpm_w32",
12534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012535#ifdef USE_XSMP
12536 "xsmp",
12537#endif
12538#ifdef USE_XSMP_INTERACT
12539 "xsmp_interact",
12540#endif
12541#ifdef FEAT_XCLIPBOARD
12542 "xterm_clipboard",
12543#endif
12544#ifdef FEAT_XTERM_SAVE
12545 "xterm_save",
12546#endif
12547#if defined(UNIX) && defined(FEAT_X11)
12548 "X11",
12549#endif
12550 NULL
12551 };
12552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012553 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012554 for (i = 0; has_list[i] != NULL; ++i)
12555 if (STRICMP(name, has_list[i]) == 0)
12556 {
12557 n = TRUE;
12558 break;
12559 }
12560
12561 if (n == FALSE)
12562 {
12563 if (STRNICMP(name, "patch", 5) == 0)
12564 n = has_patch(atoi((char *)name + 5));
12565 else if (STRICMP(name, "vim_starting") == 0)
12566 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012567#ifdef FEAT_MBYTE
12568 else if (STRICMP(name, "multi_byte_encoding") == 0)
12569 n = has_mbyte;
12570#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012571#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12572 else if (STRICMP(name, "balloon_multiline") == 0)
12573 n = multiline_balloon_available();
12574#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012575#ifdef DYNAMIC_TCL
12576 else if (STRICMP(name, "tcl") == 0)
12577 n = tcl_enabled(FALSE);
12578#endif
12579#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12580 else if (STRICMP(name, "iconv") == 0)
12581 n = iconv_enabled(FALSE);
12582#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012583#ifdef DYNAMIC_LUA
12584 else if (STRICMP(name, "lua") == 0)
12585 n = lua_enabled(FALSE);
12586#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012587#ifdef DYNAMIC_MZSCHEME
12588 else if (STRICMP(name, "mzscheme") == 0)
12589 n = mzscheme_enabled(FALSE);
12590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591#ifdef DYNAMIC_RUBY
12592 else if (STRICMP(name, "ruby") == 0)
12593 n = ruby_enabled(FALSE);
12594#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012595#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012596#ifdef DYNAMIC_PYTHON
12597 else if (STRICMP(name, "python") == 0)
12598 n = python_enabled(FALSE);
12599#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012600#endif
12601#ifdef FEAT_PYTHON3
12602#ifdef DYNAMIC_PYTHON3
12603 else if (STRICMP(name, "python3") == 0)
12604 n = python3_enabled(FALSE);
12605#endif
12606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607#ifdef DYNAMIC_PERL
12608 else if (STRICMP(name, "perl") == 0)
12609 n = perl_enabled(FALSE);
12610#endif
12611#ifdef FEAT_GUI
12612 else if (STRICMP(name, "gui_running") == 0)
12613 n = (gui.in_use || gui.starting);
12614# ifdef FEAT_GUI_W32
12615 else if (STRICMP(name, "gui_win32s") == 0)
12616 n = gui_is_win32s();
12617# endif
12618# ifdef FEAT_BROWSE
12619 else if (STRICMP(name, "browse") == 0)
12620 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12621# endif
12622#endif
12623#ifdef FEAT_SYN_HL
12624 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012625 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012626#endif
12627#if defined(WIN3264)
12628 else if (STRICMP(name, "win95") == 0)
12629 n = mch_windows95();
12630#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012631#ifdef FEAT_NETBEANS_INTG
12632 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012633 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012635 }
12636
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012637 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012638}
12639
12640/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012641 * "has_key()" function
12642 */
12643 static void
12644f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012645 typval_T *argvars;
12646 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012647{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012648 if (argvars[0].v_type != VAR_DICT)
12649 {
12650 EMSG(_(e_dictreq));
12651 return;
12652 }
12653 if (argvars[0].vval.v_dict == NULL)
12654 return;
12655
12656 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012657 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012658}
12659
12660/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012661 * "haslocaldir()" function
12662 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012663 static void
12664f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012665 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012666 typval_T *rettv;
12667{
12668 rettv->vval.v_number = (curwin->w_localdir != NULL);
12669}
12670
12671/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012672 * "hasmapto()" function
12673 */
12674 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012675f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012676 typval_T *argvars;
12677 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678{
12679 char_u *name;
12680 char_u *mode;
12681 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012682 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012683
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012684 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012685 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686 mode = (char_u *)"nvo";
12687 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012689 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012690 if (argvars[2].v_type != VAR_UNKNOWN)
12691 abbr = get_tv_number(&argvars[2]);
12692 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012693
Bram Moolenaar2c932302006-03-18 21:42:09 +000012694 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012695 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012696 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012698}
12699
12700/*
12701 * "histadd()" function
12702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012704f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012705 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012707{
12708#ifdef FEAT_CMDHIST
12709 int histype;
12710 char_u *str;
12711 char_u buf[NUMBUFLEN];
12712#endif
12713
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 if (check_restricted() || check_secure())
12716 return;
12717#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012718 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12719 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720 if (histype >= 0)
12721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012722 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723 if (*str != NUL)
12724 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012725 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012727 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728 return;
12729 }
12730 }
12731#endif
12732}
12733
12734/*
12735 * "histdel()" function
12736 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012738f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012739 typval_T *argvars UNUSED;
12740 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741{
12742#ifdef FEAT_CMDHIST
12743 int n;
12744 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012745 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012747 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12748 if (str == NULL)
12749 n = 0;
12750 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012751 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012752 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012753 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012755 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012756 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757 else
12758 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012759 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012760 get_tv_string_buf(&argvars[1], buf));
12761 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762#endif
12763}
12764
12765/*
12766 * "histget()" function
12767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012769f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012770 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012772{
12773#ifdef FEAT_CMDHIST
12774 int type;
12775 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012776 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012777
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012778 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12779 if (str == NULL)
12780 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012781 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012782 {
12783 type = get_histtype(str);
12784 if (argvars[1].v_type == VAR_UNKNOWN)
12785 idx = get_history_idx(type);
12786 else
12787 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12788 /* -1 on type error */
12789 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012791#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012792 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012795}
12796
12797/*
12798 * "histnr()" function
12799 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012802 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012803 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012804{
12805 int i;
12806
12807#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012808 char_u *history = get_tv_string_chk(&argvars[0]);
12809
12810 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 if (i >= HIST_CMD && i < HIST_COUNT)
12812 i = get_history_idx(i);
12813 else
12814#endif
12815 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012816 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817}
12818
12819/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820 * "highlightID(name)" function
12821 */
12822 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012823f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012824 typval_T *argvars;
12825 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012826{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012827 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828}
12829
12830/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012831 * "highlight_exists()" function
12832 */
12833 static void
12834f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012835 typval_T *argvars;
12836 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012837{
12838 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12839}
12840
12841/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842 * "hostname()" function
12843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012846 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012848{
12849 char_u hostname[256];
12850
12851 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012852 rettv->v_type = VAR_STRING;
12853 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854}
12855
12856/*
12857 * iconv() function
12858 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012860f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012861 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012862 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863{
12864#ifdef FEAT_MBYTE
12865 char_u buf1[NUMBUFLEN];
12866 char_u buf2[NUMBUFLEN];
12867 char_u *from, *to, *str;
12868 vimconv_T vimconv;
12869#endif
12870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 rettv->v_type = VAR_STRING;
12872 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873
12874#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012875 str = get_tv_string(&argvars[0]);
12876 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12877 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878 vimconv.vc_type = CONV_NONE;
12879 convert_setup(&vimconv, from, to);
12880
12881 /* If the encodings are equal, no conversion needed. */
12882 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012884 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886
12887 convert_setup(&vimconv, NULL, NULL);
12888 vim_free(from);
12889 vim_free(to);
12890#endif
12891}
12892
12893/*
12894 * "indent()" function
12895 */
12896 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012898 typval_T *argvars;
12899 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900{
12901 linenr_T lnum;
12902
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012903 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908}
12909
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012910/*
12911 * "index()" function
12912 */
12913 static void
12914f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012915 typval_T *argvars;
12916 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012917{
Bram Moolenaar33570922005-01-25 22:26:29 +000012918 list_T *l;
12919 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012920 long idx = 0;
12921 int ic = FALSE;
12922
12923 rettv->vval.v_number = -1;
12924 if (argvars[0].v_type != VAR_LIST)
12925 {
12926 EMSG(_(e_listreq));
12927 return;
12928 }
12929 l = argvars[0].vval.v_list;
12930 if (l != NULL)
12931 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012932 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012933 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012934 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012935 int error = FALSE;
12936
Bram Moolenaar758711c2005-02-02 23:11:38 +000012937 /* Start at specified item. Use the cached index that list_find()
12938 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012939 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012940 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012941 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012942 ic = get_tv_number_chk(&argvars[3], &error);
12943 if (error)
12944 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012945 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012946
Bram Moolenaar758711c2005-02-02 23:11:38 +000012947 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012948 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012949 {
12950 rettv->vval.v_number = idx;
12951 break;
12952 }
12953 }
12954}
12955
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956static int inputsecret_flag = 0;
12957
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012958static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12959
Bram Moolenaar071d4272004-06-13 20:20:40 +000012960/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012961 * This function is used by f_input() and f_inputdialog() functions. The third
12962 * argument to f_input() specifies the type of completion to use at the
12963 * prompt. The third argument to f_inputdialog() specifies the value to return
12964 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012965 */
12966 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012967get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012968 typval_T *argvars;
12969 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012970 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012971{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012972 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973 char_u *p = NULL;
12974 int c;
12975 char_u buf[NUMBUFLEN];
12976 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012977 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012978 int xp_type = EXPAND_NOTHING;
12979 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012981 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012982 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012983
12984#ifdef NO_CONSOLE_INPUT
12985 /* While starting up, there is no place to enter text. */
12986 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988#endif
12989
12990 cmd_silent = FALSE; /* Want to see the prompt. */
12991 if (prompt != NULL)
12992 {
12993 /* Only the part of the message after the last NL is considered as
12994 * prompt for the command line */
12995 p = vim_strrchr(prompt, '\n');
12996 if (p == NULL)
12997 p = prompt;
12998 else
12999 {
13000 ++p;
13001 c = *p;
13002 *p = NUL;
13003 msg_start();
13004 msg_clr_eos();
13005 msg_puts_attr(prompt, echo_attr);
13006 msg_didout = FALSE;
13007 msg_starthere();
13008 *p = c;
13009 }
13010 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013012 if (argvars[1].v_type != VAR_UNKNOWN)
13013 {
13014 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13015 if (defstr != NULL)
13016 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013017
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013018 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013019 {
13020 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013021 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013022 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013023
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013024 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013025 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013026
Bram Moolenaar4463f292005-09-25 22:20:24 +000013027 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13028 if (xp_name == NULL)
13029 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013030
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013031 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013032
Bram Moolenaar4463f292005-09-25 22:20:24 +000013033 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13034 &xp_arg) == FAIL)
13035 return;
13036 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013037 }
13038
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013039 if (defstr != NULL)
13040 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013041 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13042 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013043 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013044 && argvars[1].v_type != VAR_UNKNOWN
13045 && argvars[2].v_type != VAR_UNKNOWN)
13046 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13047 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013048
13049 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013051 /* since the user typed this, no need to wait for return */
13052 need_wait_return = FALSE;
13053 msg_didout = FALSE;
13054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055 cmd_silent = cmd_silent_save;
13056}
13057
13058/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013059 * "input()" function
13060 * Also handles inputsecret() when inputsecret is set.
13061 */
13062 static void
13063f_input(argvars, rettv)
13064 typval_T *argvars;
13065 typval_T *rettv;
13066{
13067 get_user_input(argvars, rettv, FALSE);
13068}
13069
13070/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071 * "inputdialog()" function
13072 */
13073 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013074f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013075 typval_T *argvars;
13076 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013077{
13078#if defined(FEAT_GUI_TEXTDIALOG)
13079 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13080 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13081 {
13082 char_u *message;
13083 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013084 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013086 message = get_tv_string_chk(&argvars[0]);
13087 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013088 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013089 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013090 else
13091 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013092 if (message != NULL && defstr != NULL
13093 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013094 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013095 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013096 else
13097 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013098 if (message != NULL && defstr != NULL
13099 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013100 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013101 rettv->vval.v_string = vim_strsave(
13102 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013103 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013104 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013106 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 }
13108 else
13109#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013110 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111}
13112
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013113/*
13114 * "inputlist()" function
13115 */
13116 static void
13117f_inputlist(argvars, rettv)
13118 typval_T *argvars;
13119 typval_T *rettv;
13120{
13121 listitem_T *li;
13122 int selected;
13123 int mouse_used;
13124
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013125#ifdef NO_CONSOLE_INPUT
13126 /* While starting up, there is no place to enter text. */
13127 if (no_console_input())
13128 return;
13129#endif
13130 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13131 {
13132 EMSG2(_(e_listarg), "inputlist()");
13133 return;
13134 }
13135
13136 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013137 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013138 lines_left = Rows; /* avoid more prompt */
13139 msg_scroll = TRUE;
13140 msg_clr_eos();
13141
13142 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13143 {
13144 msg_puts(get_tv_string(&li->li_tv));
13145 msg_putchar('\n');
13146 }
13147
13148 /* Ask for choice. */
13149 selected = prompt_for_number(&mouse_used);
13150 if (mouse_used)
13151 selected -= lines_left;
13152
13153 rettv->vval.v_number = selected;
13154}
13155
13156
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13158
13159/*
13160 * "inputrestore()" function
13161 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013162 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013163f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013164 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013165 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166{
13167 if (ga_userinput.ga_len > 0)
13168 {
13169 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13171 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013172 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 }
13174 else if (p_verbose > 1)
13175 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013176 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013177 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178 }
13179}
13180
13181/*
13182 * "inputsave()" function
13183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013184 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013185f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013186 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013187 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013188{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013189 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 if (ga_grow(&ga_userinput, 1) == OK)
13191 {
13192 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13193 + ga_userinput.ga_len);
13194 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013195 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196 }
13197 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013198 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013199}
13200
13201/*
13202 * "inputsecret()" function
13203 */
13204 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013205f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013206 typval_T *argvars;
13207 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013208{
13209 ++cmdline_star;
13210 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013211 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 --cmdline_star;
13213 --inputsecret_flag;
13214}
13215
13216/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013217 * "insert()" function
13218 */
13219 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013220f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013221 typval_T *argvars;
13222 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013223{
13224 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013225 listitem_T *item;
13226 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013227 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013228
13229 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013230 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013231 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013232 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013233 {
13234 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013235 before = get_tv_number_chk(&argvars[2], &error);
13236 if (error)
13237 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013238
Bram Moolenaar758711c2005-02-02 23:11:38 +000013239 if (before == l->lv_len)
13240 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013241 else
13242 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013243 item = list_find(l, before);
13244 if (item == NULL)
13245 {
13246 EMSGN(_(e_listidx), before);
13247 l = NULL;
13248 }
13249 }
13250 if (l != NULL)
13251 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013252 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013253 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013254 }
13255 }
13256}
13257
13258/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013259 * "invert(expr)" function
13260 */
13261 static void
13262f_invert(argvars, rettv)
13263 typval_T *argvars;
13264 typval_T *rettv;
13265{
13266 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13267}
13268
13269/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013270 * "isdirectory()" function
13271 */
13272 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013273f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013274 typval_T *argvars;
13275 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013277 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278}
13279
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013280/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013281 * "islocked()" function
13282 */
13283 static void
13284f_islocked(argvars, rettv)
13285 typval_T *argvars;
13286 typval_T *rettv;
13287{
13288 lval_T lv;
13289 char_u *end;
13290 dictitem_T *di;
13291
13292 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013293 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13294 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013295 if (end != NULL && lv.ll_name != NULL)
13296 {
13297 if (*end != NUL)
13298 EMSG(_(e_trailing));
13299 else
13300 {
13301 if (lv.ll_tv == NULL)
13302 {
13303 if (check_changedtick(lv.ll_name))
13304 rettv->vval.v_number = 1; /* always locked */
13305 else
13306 {
13307 di = find_var(lv.ll_name, NULL);
13308 if (di != NULL)
13309 {
13310 /* Consider a variable locked when:
13311 * 1. the variable itself is locked
13312 * 2. the value of the variable is locked.
13313 * 3. the List or Dict value is locked.
13314 */
13315 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13316 || tv_islocked(&di->di_tv));
13317 }
13318 }
13319 }
13320 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013321 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013322 else if (lv.ll_newkey != NULL)
13323 EMSG2(_(e_dictkey), lv.ll_newkey);
13324 else if (lv.ll_list != NULL)
13325 /* List item. */
13326 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13327 else
13328 /* Dictionary item. */
13329 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13330 }
13331 }
13332
13333 clear_lval(&lv);
13334}
13335
Bram Moolenaar33570922005-01-25 22:26:29 +000013336static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013337
13338/*
13339 * Turn a dict into a list:
13340 * "what" == 0: list of keys
13341 * "what" == 1: list of values
13342 * "what" == 2: list of items
13343 */
13344 static void
13345dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013346 typval_T *argvars;
13347 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013348 int what;
13349{
Bram Moolenaar33570922005-01-25 22:26:29 +000013350 list_T *l2;
13351 dictitem_T *di;
13352 hashitem_T *hi;
13353 listitem_T *li;
13354 listitem_T *li2;
13355 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013356 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013357
Bram Moolenaar8c711452005-01-14 21:53:12 +000013358 if (argvars[0].v_type != VAR_DICT)
13359 {
13360 EMSG(_(e_dictreq));
13361 return;
13362 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013363 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013364 return;
13365
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013366 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013367 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013368
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013369 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013370 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013372 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013373 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013374 --todo;
13375 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013376
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013377 li = listitem_alloc();
13378 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013379 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013380 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013381
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013382 if (what == 0)
13383 {
13384 /* keys() */
13385 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013386 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013387 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13388 }
13389 else if (what == 1)
13390 {
13391 /* values() */
13392 copy_tv(&di->di_tv, &li->li_tv);
13393 }
13394 else
13395 {
13396 /* items() */
13397 l2 = list_alloc();
13398 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013399 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013400 li->li_tv.vval.v_list = l2;
13401 if (l2 == NULL)
13402 break;
13403 ++l2->lv_refcount;
13404
13405 li2 = listitem_alloc();
13406 if (li2 == NULL)
13407 break;
13408 list_append(l2, li2);
13409 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013410 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013411 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13412
13413 li2 = listitem_alloc();
13414 if (li2 == NULL)
13415 break;
13416 list_append(l2, li2);
13417 copy_tv(&di->di_tv, &li2->li_tv);
13418 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013419 }
13420 }
13421}
13422
13423/*
13424 * "items(dict)" function
13425 */
13426 static void
13427f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013428 typval_T *argvars;
13429 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013430{
13431 dict_list(argvars, rettv, 2);
13432}
13433
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013435 * "join()" function
13436 */
13437 static void
13438f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013439 typval_T *argvars;
13440 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013441{
13442 garray_T ga;
13443 char_u *sep;
13444
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013445 if (argvars[0].v_type != VAR_LIST)
13446 {
13447 EMSG(_(e_listreq));
13448 return;
13449 }
13450 if (argvars[0].vval.v_list == NULL)
13451 return;
13452 if (argvars[1].v_type == VAR_UNKNOWN)
13453 sep = (char_u *)" ";
13454 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013455 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013456
13457 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013458
13459 if (sep != NULL)
13460 {
13461 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013462 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013463 ga_append(&ga, NUL);
13464 rettv->vval.v_string = (char_u *)ga.ga_data;
13465 }
13466 else
13467 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013468}
13469
13470/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013471 * "keys()" function
13472 */
13473 static void
13474f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013475 typval_T *argvars;
13476 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013477{
13478 dict_list(argvars, rettv, 0);
13479}
13480
13481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013482 * "last_buffer_nr()" function.
13483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013485f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013486 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013487 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488{
13489 int n = 0;
13490 buf_T *buf;
13491
13492 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13493 if (n < buf->b_fnum)
13494 n = buf->b_fnum;
13495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013496 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013497}
13498
13499/*
13500 * "len()" function
13501 */
13502 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013503f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013504 typval_T *argvars;
13505 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013506{
13507 switch (argvars[0].v_type)
13508 {
13509 case VAR_STRING:
13510 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013511 rettv->vval.v_number = (varnumber_T)STRLEN(
13512 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013513 break;
13514 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013515 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013516 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013517 case VAR_DICT:
13518 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13519 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013520 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013521 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013522 break;
13523 }
13524}
13525
Bram Moolenaar33570922005-01-25 22:26:29 +000013526static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013527
13528 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013529libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013530 typval_T *argvars;
13531 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013532 int type;
13533{
13534#ifdef FEAT_LIBCALL
13535 char_u *string_in;
13536 char_u **string_result;
13537 int nr_result;
13538#endif
13539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013540 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013541 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013542 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013543
13544 if (check_restricted() || check_secure())
13545 return;
13546
13547#ifdef FEAT_LIBCALL
13548 /* The first two args must be strings, otherwise its meaningless */
13549 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13550 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013551 string_in = NULL;
13552 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013553 string_in = argvars[2].vval.v_string;
13554 if (type == VAR_NUMBER)
13555 string_result = NULL;
13556 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013558 if (mch_libcall(argvars[0].vval.v_string,
13559 argvars[1].vval.v_string,
13560 string_in,
13561 argvars[2].vval.v_number,
13562 string_result,
13563 &nr_result) == OK
13564 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013565 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013566 }
13567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568}
13569
13570/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013571 * "libcall()" function
13572 */
13573 static void
13574f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013575 typval_T *argvars;
13576 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013577{
13578 libcall_common(argvars, rettv, VAR_STRING);
13579}
13580
13581/*
13582 * "libcallnr()" function
13583 */
13584 static void
13585f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013586 typval_T *argvars;
13587 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013588{
13589 libcall_common(argvars, rettv, VAR_NUMBER);
13590}
13591
13592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593 * "line(string)" function
13594 */
13595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013596f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013597 typval_T *argvars;
13598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599{
13600 linenr_T lnum = 0;
13601 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013602 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013603
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013604 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013605 if (fp != NULL)
13606 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013607 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608}
13609
13610/*
13611 * "line2byte(lnum)" function
13612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013614f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013615 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617{
13618#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013619 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620#else
13621 linenr_T lnum;
13622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013625 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13628 if (rettv->vval.v_number >= 0)
13629 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630#endif
13631}
13632
13633/*
13634 * "lispindent(lnum)" function
13635 */
13636 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013637f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013638 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013639 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640{
13641#ifdef FEAT_LISP
13642 pos_T pos;
13643 linenr_T lnum;
13644
13645 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013646 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13648 {
13649 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013650 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651 curwin->w_cursor = pos;
13652 }
13653 else
13654#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013655 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656}
13657
13658/*
13659 * "localtime()" function
13660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013662f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013663 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013664 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013666 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667}
13668
Bram Moolenaar33570922005-01-25 22:26:29 +000013669static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013670
13671 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013672get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013673 typval_T *argvars;
13674 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675 int exact;
13676{
13677 char_u *keys;
13678 char_u *which;
13679 char_u buf[NUMBUFLEN];
13680 char_u *keys_buf = NULL;
13681 char_u *rhs;
13682 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013683 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013684 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013685 mapblock_T *mp;
13686 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687
13688 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013689 rettv->v_type = VAR_STRING;
13690 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013692 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 if (*keys == NUL)
13694 return;
13695
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013696 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013697 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013698 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013699 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013700 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013701 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013702 if (argvars[3].v_type != VAR_UNKNOWN)
13703 get_dict = get_tv_number(&argvars[3]);
13704 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013706 else
13707 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013708 if (which == NULL)
13709 return;
13710
Bram Moolenaar071d4272004-06-13 20:20:40 +000013711 mode = get_map_mode(&which, 0);
13712
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013713 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013714 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013716
13717 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013719 /* Return a string. */
13720 if (rhs != NULL)
13721 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722
Bram Moolenaarbd743252010-10-20 21:23:33 +020013723 }
13724 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13725 {
13726 /* Return a dictionary. */
13727 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13728 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13729 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730
Bram Moolenaarbd743252010-10-20 21:23:33 +020013731 dict_add_nr_str(dict, "lhs", 0L, lhs);
13732 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13733 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13734 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13735 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13736 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13737 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
13738 dict_add_nr_str(dict, "mode", 0L, mapmode);
13739
13740 vim_free(lhs);
13741 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742 }
13743}
13744
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013745#ifdef FEAT_FLOAT
13746/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013747 * "log()" function
13748 */
13749 static void
13750f_log(argvars, rettv)
13751 typval_T *argvars;
13752 typval_T *rettv;
13753{
13754 float_T f;
13755
13756 rettv->v_type = VAR_FLOAT;
13757 if (get_float_arg(argvars, &f) == OK)
13758 rettv->vval.v_float = log(f);
13759 else
13760 rettv->vval.v_float = 0.0;
13761}
13762
13763/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013764 * "log10()" function
13765 */
13766 static void
13767f_log10(argvars, rettv)
13768 typval_T *argvars;
13769 typval_T *rettv;
13770{
13771 float_T f;
13772
13773 rettv->v_type = VAR_FLOAT;
13774 if (get_float_arg(argvars, &f) == OK)
13775 rettv->vval.v_float = log10(f);
13776 else
13777 rettv->vval.v_float = 0.0;
13778}
13779#endif
13780
Bram Moolenaar1dced572012-04-05 16:54:08 +020013781#ifdef FEAT_LUA
13782/*
13783 * "luaeval()" function
13784 */
13785 static void
13786f_luaeval(argvars, rettv)
13787 typval_T *argvars;
13788 typval_T *rettv;
13789{
13790 char_u *str;
13791 char_u buf[NUMBUFLEN];
13792
13793 str = get_tv_string_buf(&argvars[0], buf);
13794 do_luaeval(str, argvars + 1, rettv);
13795}
13796#endif
13797
Bram Moolenaar071d4272004-06-13 20:20:40 +000013798/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013799 * "map()" function
13800 */
13801 static void
13802f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013803 typval_T *argvars;
13804 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013805{
13806 filter_map(argvars, rettv, TRUE);
13807}
13808
13809/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013810 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811 */
13812 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013813f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013814 typval_T *argvars;
13815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013817 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013818}
13819
13820/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013821 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822 */
13823 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013824f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013825 typval_T *argvars;
13826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013828 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829}
13830
Bram Moolenaar33570922005-01-25 22:26:29 +000013831static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832
13833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013834find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013835 typval_T *argvars;
13836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013837 int type;
13838{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013839 char_u *str = NULL;
13840 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841 char_u *pat;
13842 regmatch_T regmatch;
13843 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013844 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845 char_u *save_cpo;
13846 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013847 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013848 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013849 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013850 list_T *l = NULL;
13851 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013852 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013853 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854
13855 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13856 save_cpo = p_cpo;
13857 p_cpo = (char_u *)"";
13858
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013859 rettv->vval.v_number = -1;
13860 if (type == 3)
13861 {
13862 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013863 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013864 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013865 }
13866 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013867 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013868 rettv->v_type = VAR_STRING;
13869 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013872 if (argvars[0].v_type == VAR_LIST)
13873 {
13874 if ((l = argvars[0].vval.v_list) == NULL)
13875 goto theend;
13876 li = l->lv_first;
13877 }
13878 else
13879 expr = str = get_tv_string(&argvars[0]);
13880
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013881 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13882 if (pat == NULL)
13883 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013884
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013885 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013887 int error = FALSE;
13888
13889 start = get_tv_number_chk(&argvars[2], &error);
13890 if (error)
13891 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013892 if (l != NULL)
13893 {
13894 li = list_find(l, start);
13895 if (li == NULL)
13896 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013897 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013898 }
13899 else
13900 {
13901 if (start < 0)
13902 start = 0;
13903 if (start > (long)STRLEN(str))
13904 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013905 /* When "count" argument is there ignore matches before "start",
13906 * otherwise skip part of the string. Differs when pattern is "^"
13907 * or "\<". */
13908 if (argvars[3].v_type != VAR_UNKNOWN)
13909 startcol = start;
13910 else
13911 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013912 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013913
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013914 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013915 nth = get_tv_number_chk(&argvars[3], &error);
13916 if (error)
13917 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013918 }
13919
13920 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13921 if (regmatch.regprog != NULL)
13922 {
13923 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013924
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013925 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013926 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013927 if (l != NULL)
13928 {
13929 if (li == NULL)
13930 {
13931 match = FALSE;
13932 break;
13933 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013934 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013935 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013936 if (str == NULL)
13937 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013938 }
13939
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013940 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013941
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013942 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013943 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013944 if (l == NULL && !match)
13945 break;
13946
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013947 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013948 if (l != NULL)
13949 {
13950 li = li->li_next;
13951 ++idx;
13952 }
13953 else
13954 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013955#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013956 startcol = (colnr_T)(regmatch.startp[0]
13957 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013958#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013959 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013960#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013961 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013962 }
13963
13964 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013965 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013966 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013967 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013968 int i;
13969
13970 /* return list with matched string and submatches */
13971 for (i = 0; i < NSUBEXP; ++i)
13972 {
13973 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013974 {
13975 if (list_append_string(rettv->vval.v_list,
13976 (char_u *)"", 0) == FAIL)
13977 break;
13978 }
13979 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013980 regmatch.startp[i],
13981 (int)(regmatch.endp[i] - regmatch.startp[i]))
13982 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013983 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013984 }
13985 }
13986 else if (type == 2)
13987 {
13988 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013989 if (l != NULL)
13990 copy_tv(&li->li_tv, rettv);
13991 else
13992 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013994 }
13995 else if (l != NULL)
13996 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997 else
13998 {
13999 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014000 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001 (varnumber_T)(regmatch.startp[0] - str);
14002 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014003 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014004 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014005 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006 }
14007 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014008 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014009 }
14010
14011theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014012 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013 p_cpo = save_cpo;
14014}
14015
14016/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014017 * "match()" function
14018 */
14019 static void
14020f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014021 typval_T *argvars;
14022 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014023{
14024 find_some_match(argvars, rettv, 1);
14025}
14026
14027/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014028 * "matchadd()" function
14029 */
14030 static void
14031f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014032 typval_T *argvars UNUSED;
14033 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014034{
14035#ifdef FEAT_SEARCH_EXTRA
14036 char_u buf[NUMBUFLEN];
14037 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14038 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14039 int prio = 10; /* default priority */
14040 int id = -1;
14041 int error = FALSE;
14042
14043 rettv->vval.v_number = -1;
14044
14045 if (grp == NULL || pat == NULL)
14046 return;
14047 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014048 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014049 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014050 if (argvars[3].v_type != VAR_UNKNOWN)
14051 id = get_tv_number_chk(&argvars[3], &error);
14052 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014053 if (error == TRUE)
14054 return;
14055 if (id >= 1 && id <= 3)
14056 {
14057 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14058 return;
14059 }
14060
14061 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14062#endif
14063}
14064
14065/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014066 * "matcharg()" function
14067 */
14068 static void
14069f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014070 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014071 typval_T *rettv;
14072{
14073 if (rettv_list_alloc(rettv) == OK)
14074 {
14075#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014076 int id = get_tv_number(&argvars[0]);
14077 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014078
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014079 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014080 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014081 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14082 {
14083 list_append_string(rettv->vval.v_list,
14084 syn_id2name(m->hlg_id), -1);
14085 list_append_string(rettv->vval.v_list, m->pattern, -1);
14086 }
14087 else
14088 {
14089 list_append_string(rettv->vval.v_list, NUL, -1);
14090 list_append_string(rettv->vval.v_list, NUL, -1);
14091 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014092 }
14093#endif
14094 }
14095}
14096
14097/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014098 * "matchdelete()" function
14099 */
14100 static void
14101f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014102 typval_T *argvars UNUSED;
14103 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014104{
14105#ifdef FEAT_SEARCH_EXTRA
14106 rettv->vval.v_number = match_delete(curwin,
14107 (int)get_tv_number(&argvars[0]), TRUE);
14108#endif
14109}
14110
14111/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014112 * "matchend()" function
14113 */
14114 static void
14115f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014116 typval_T *argvars;
14117 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014118{
14119 find_some_match(argvars, rettv, 0);
14120}
14121
14122/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014123 * "matchlist()" function
14124 */
14125 static void
14126f_matchlist(argvars, rettv)
14127 typval_T *argvars;
14128 typval_T *rettv;
14129{
14130 find_some_match(argvars, rettv, 3);
14131}
14132
14133/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014134 * "matchstr()" function
14135 */
14136 static void
14137f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014138 typval_T *argvars;
14139 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014140{
14141 find_some_match(argvars, rettv, 2);
14142}
14143
Bram Moolenaar33570922005-01-25 22:26:29 +000014144static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014145
14146 static void
14147max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014148 typval_T *argvars;
14149 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014150 int domax;
14151{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014152 long n = 0;
14153 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014154 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014155
14156 if (argvars[0].v_type == VAR_LIST)
14157 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014158 list_T *l;
14159 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014160
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014161 l = argvars[0].vval.v_list;
14162 if (l != NULL)
14163 {
14164 li = l->lv_first;
14165 if (li != NULL)
14166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014167 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014168 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014169 {
14170 li = li->li_next;
14171 if (li == NULL)
14172 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014173 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014174 if (domax ? i > n : i < n)
14175 n = i;
14176 }
14177 }
14178 }
14179 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014180 else if (argvars[0].v_type == VAR_DICT)
14181 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014182 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014183 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014184 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014185 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014186
14187 d = argvars[0].vval.v_dict;
14188 if (d != NULL)
14189 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014190 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014191 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014192 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014193 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014194 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014195 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014196 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014197 if (first)
14198 {
14199 n = i;
14200 first = FALSE;
14201 }
14202 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014203 n = i;
14204 }
14205 }
14206 }
14207 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014208 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014209 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014210 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014211}
14212
14213/*
14214 * "max()" function
14215 */
14216 static void
14217f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014218 typval_T *argvars;
14219 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014220{
14221 max_min(argvars, rettv, TRUE);
14222}
14223
14224/*
14225 * "min()" function
14226 */
14227 static void
14228f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014229 typval_T *argvars;
14230 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014231{
14232 max_min(argvars, rettv, FALSE);
14233}
14234
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014235static int mkdir_recurse __ARGS((char_u *dir, int prot));
14236
14237/*
14238 * Create the directory in which "dir" is located, and higher levels when
14239 * needed.
14240 */
14241 static int
14242mkdir_recurse(dir, prot)
14243 char_u *dir;
14244 int prot;
14245{
14246 char_u *p;
14247 char_u *updir;
14248 int r = FAIL;
14249
14250 /* Get end of directory name in "dir".
14251 * We're done when it's "/" or "c:/". */
14252 p = gettail_sep(dir);
14253 if (p <= get_past_head(dir))
14254 return OK;
14255
14256 /* If the directory exists we're done. Otherwise: create it.*/
14257 updir = vim_strnsave(dir, (int)(p - dir));
14258 if (updir == NULL)
14259 return FAIL;
14260 if (mch_isdir(updir))
14261 r = OK;
14262 else if (mkdir_recurse(updir, prot) == OK)
14263 r = vim_mkdir_emsg(updir, prot);
14264 vim_free(updir);
14265 return r;
14266}
14267
14268#ifdef vim_mkdir
14269/*
14270 * "mkdir()" function
14271 */
14272 static void
14273f_mkdir(argvars, rettv)
14274 typval_T *argvars;
14275 typval_T *rettv;
14276{
14277 char_u *dir;
14278 char_u buf[NUMBUFLEN];
14279 int prot = 0755;
14280
14281 rettv->vval.v_number = FAIL;
14282 if (check_restricted() || check_secure())
14283 return;
14284
14285 dir = get_tv_string_buf(&argvars[0], buf);
14286 if (argvars[1].v_type != VAR_UNKNOWN)
14287 {
14288 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014289 prot = get_tv_number_chk(&argvars[2], NULL);
14290 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014291 mkdir_recurse(dir, prot);
14292 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014293 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014294}
14295#endif
14296
Bram Moolenaar0d660222005-01-07 21:51:51 +000014297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014298 * "mode()" function
14299 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014300 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014301f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014302 typval_T *argvars;
14303 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014305 char_u buf[3];
14306
14307 buf[1] = NUL;
14308 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309
14310#ifdef FEAT_VISUAL
14311 if (VIsual_active)
14312 {
14313 if (VIsual_select)
14314 buf[0] = VIsual_mode + 's' - 'v';
14315 else
14316 buf[0] = VIsual_mode;
14317 }
14318 else
14319#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014320 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14321 || State == CONFIRM)
14322 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014324 if (State == ASKMORE)
14325 buf[1] = 'm';
14326 else if (State == CONFIRM)
14327 buf[1] = '?';
14328 }
14329 else if (State == EXTERNCMD)
14330 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 else if (State & INSERT)
14332 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014333#ifdef FEAT_VREPLACE
14334 if (State & VREPLACE_FLAG)
14335 {
14336 buf[0] = 'R';
14337 buf[1] = 'v';
14338 }
14339 else
14340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341 if (State & REPLACE_FLAG)
14342 buf[0] = 'R';
14343 else
14344 buf[0] = 'i';
14345 }
14346 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014347 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014349 if (exmode_active)
14350 buf[1] = 'v';
14351 }
14352 else if (exmode_active)
14353 {
14354 buf[0] = 'c';
14355 buf[1] = 'e';
14356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014358 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014360 if (finish_op)
14361 buf[1] = 'o';
14362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014363
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014364 /* Clear out the minor mode when the argument is not a non-zero number or
14365 * non-empty string. */
14366 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014367 buf[1] = NUL;
14368
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014369 rettv->vval.v_string = vim_strsave(buf);
14370 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371}
14372
Bram Moolenaar429fa852013-04-15 12:27:36 +020014373#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014374/*
14375 * "mzeval()" function
14376 */
14377 static void
14378f_mzeval(argvars, rettv)
14379 typval_T *argvars;
14380 typval_T *rettv;
14381{
14382 char_u *str;
14383 char_u buf[NUMBUFLEN];
14384
14385 str = get_tv_string_buf(&argvars[0], buf);
14386 do_mzeval(str, rettv);
14387}
Bram Moolenaar75676462013-01-30 14:55:42 +010014388
14389 void
14390mzscheme_call_vim(name, args, rettv)
14391 char_u *name;
14392 typval_T *args;
14393 typval_T *rettv;
14394{
14395 typval_T argvars[3];
14396
14397 argvars[0].v_type = VAR_STRING;
14398 argvars[0].vval.v_string = name;
14399 copy_tv(args, &argvars[1]);
14400 argvars[2].v_type = VAR_UNKNOWN;
14401 f_call(argvars, rettv);
14402 clear_tv(&argvars[1]);
14403}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014404#endif
14405
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014407 * "nextnonblank()" function
14408 */
14409 static void
14410f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014411 typval_T *argvars;
14412 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014413{
14414 linenr_T lnum;
14415
14416 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14417 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014418 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014419 {
14420 lnum = 0;
14421 break;
14422 }
14423 if (*skipwhite(ml_get(lnum)) != NUL)
14424 break;
14425 }
14426 rettv->vval.v_number = lnum;
14427}
14428
14429/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430 * "nr2char()" function
14431 */
14432 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014433f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014434 typval_T *argvars;
14435 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436{
14437 char_u buf[NUMBUFLEN];
14438
14439#ifdef FEAT_MBYTE
14440 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014441 {
14442 int utf8 = 0;
14443
14444 if (argvars[1].v_type != VAR_UNKNOWN)
14445 utf8 = get_tv_number_chk(&argvars[1], NULL);
14446 if (utf8)
14447 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14448 else
14449 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14450 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014451 else
14452#endif
14453 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014454 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455 buf[1] = NUL;
14456 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014457 rettv->v_type = VAR_STRING;
14458 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014459}
14460
14461/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014462 * "or(expr, expr)" function
14463 */
14464 static void
14465f_or(argvars, rettv)
14466 typval_T *argvars;
14467 typval_T *rettv;
14468{
14469 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14470 | get_tv_number_chk(&argvars[1], NULL);
14471}
14472
14473/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014474 * "pathshorten()" function
14475 */
14476 static void
14477f_pathshorten(argvars, rettv)
14478 typval_T *argvars;
14479 typval_T *rettv;
14480{
14481 char_u *p;
14482
14483 rettv->v_type = VAR_STRING;
14484 p = get_tv_string_chk(&argvars[0]);
14485 if (p == NULL)
14486 rettv->vval.v_string = NULL;
14487 else
14488 {
14489 p = vim_strsave(p);
14490 rettv->vval.v_string = p;
14491 if (p != NULL)
14492 shorten_dir(p);
14493 }
14494}
14495
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014496#ifdef FEAT_FLOAT
14497/*
14498 * "pow()" function
14499 */
14500 static void
14501f_pow(argvars, rettv)
14502 typval_T *argvars;
14503 typval_T *rettv;
14504{
14505 float_T fx, fy;
14506
14507 rettv->v_type = VAR_FLOAT;
14508 if (get_float_arg(argvars, &fx) == OK
14509 && get_float_arg(&argvars[1], &fy) == OK)
14510 rettv->vval.v_float = pow(fx, fy);
14511 else
14512 rettv->vval.v_float = 0.0;
14513}
14514#endif
14515
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014516/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014517 * "prevnonblank()" function
14518 */
14519 static void
14520f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014521 typval_T *argvars;
14522 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014523{
14524 linenr_T lnum;
14525
14526 lnum = get_tv_lnum(argvars);
14527 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14528 lnum = 0;
14529 else
14530 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14531 --lnum;
14532 rettv->vval.v_number = lnum;
14533}
14534
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014535#ifdef HAVE_STDARG_H
14536/* This dummy va_list is here because:
14537 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14538 * - locally in the function results in a "used before set" warning
14539 * - using va_start() to initialize it gives "function with fixed args" error */
14540static va_list ap;
14541#endif
14542
Bram Moolenaar8c711452005-01-14 21:53:12 +000014543/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014544 * "printf()" function
14545 */
14546 static void
14547f_printf(argvars, rettv)
14548 typval_T *argvars;
14549 typval_T *rettv;
14550{
14551 rettv->v_type = VAR_STRING;
14552 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014553#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014554 {
14555 char_u buf[NUMBUFLEN];
14556 int len;
14557 char_u *s;
14558 int saved_did_emsg = did_emsg;
14559 char *fmt;
14560
14561 /* Get the required length, allocate the buffer and do it for real. */
14562 did_emsg = FALSE;
14563 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014564 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014565 if (!did_emsg)
14566 {
14567 s = alloc(len + 1);
14568 if (s != NULL)
14569 {
14570 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014571 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014572 }
14573 }
14574 did_emsg |= saved_did_emsg;
14575 }
14576#endif
14577}
14578
14579/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014580 * "pumvisible()" function
14581 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014582 static void
14583f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014584 typval_T *argvars UNUSED;
14585 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014586{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014587#ifdef FEAT_INS_EXPAND
14588 if (pum_visible())
14589 rettv->vval.v_number = 1;
14590#endif
14591}
14592
Bram Moolenaardb913952012-06-29 12:54:53 +020014593#ifdef FEAT_PYTHON3
14594/*
14595 * "py3eval()" function
14596 */
14597 static void
14598f_py3eval(argvars, rettv)
14599 typval_T *argvars;
14600 typval_T *rettv;
14601{
14602 char_u *str;
14603 char_u buf[NUMBUFLEN];
14604
14605 str = get_tv_string_buf(&argvars[0], buf);
14606 do_py3eval(str, rettv);
14607}
14608#endif
14609
14610#ifdef FEAT_PYTHON
14611/*
14612 * "pyeval()" function
14613 */
14614 static void
14615f_pyeval(argvars, rettv)
14616 typval_T *argvars;
14617 typval_T *rettv;
14618{
14619 char_u *str;
14620 char_u buf[NUMBUFLEN];
14621
14622 str = get_tv_string_buf(&argvars[0], buf);
14623 do_pyeval(str, rettv);
14624}
14625#endif
14626
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014627/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014628 * "range()" function
14629 */
14630 static void
14631f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014632 typval_T *argvars;
14633 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014634{
14635 long start;
14636 long end;
14637 long stride = 1;
14638 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014639 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014640
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014641 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014642 if (argvars[1].v_type == VAR_UNKNOWN)
14643 {
14644 end = start - 1;
14645 start = 0;
14646 }
14647 else
14648 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014649 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014650 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014651 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014652 }
14653
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014654 if (error)
14655 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014656 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014657 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014658 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014659 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014660 else
14661 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014662 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014663 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014664 if (list_append_number(rettv->vval.v_list,
14665 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014666 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014667 }
14668}
14669
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014670/*
14671 * "readfile()" function
14672 */
14673 static void
14674f_readfile(argvars, rettv)
14675 typval_T *argvars;
14676 typval_T *rettv;
14677{
14678 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014679 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014680 char_u *fname;
14681 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014682 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14683 int io_size = sizeof(buf);
14684 int readlen; /* size of last fread() */
14685 char_u *prev = NULL; /* previously read bytes, if any */
14686 long prevlen = 0; /* length of data in prev */
14687 long prevsize = 0; /* size of prev buffer */
14688 long maxline = MAXLNUM;
14689 long cnt = 0;
14690 char_u *p; /* position in buf */
14691 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014692
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014693 if (argvars[1].v_type != VAR_UNKNOWN)
14694 {
14695 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14696 binary = TRUE;
14697 if (argvars[2].v_type != VAR_UNKNOWN)
14698 maxline = get_tv_number(&argvars[2]);
14699 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014700
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014701 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014702 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014703
14704 /* Always open the file in binary mode, library functions have a mind of
14705 * their own about CR-LF conversion. */
14706 fname = get_tv_string(&argvars[0]);
14707 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14708 {
14709 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14710 return;
14711 }
14712
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014713 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014714 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014715 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014716
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014717 /* This for loop processes what was read, but is also entered at end
14718 * of file so that either:
14719 * - an incomplete line gets written
14720 * - a "binary" file gets an empty line at the end if it ends in a
14721 * newline. */
14722 for (p = buf, start = buf;
14723 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14724 ++p)
14725 {
14726 if (*p == '\n' || readlen <= 0)
14727 {
14728 listitem_T *li;
14729 char_u *s = NULL;
14730 long_u len = p - start;
14731
14732 /* Finished a line. Remove CRs before NL. */
14733 if (readlen > 0 && !binary)
14734 {
14735 while (len > 0 && start[len - 1] == '\r')
14736 --len;
14737 /* removal may cross back to the "prev" string */
14738 if (len == 0)
14739 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14740 --prevlen;
14741 }
14742 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014743 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014744 else
14745 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014746 /* Change "prev" buffer to be the right size. This way
14747 * the bytes are only copied once, and very long lines are
14748 * allocated only once. */
14749 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014750 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014751 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014752 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014753 prev = NULL; /* the list will own the string */
14754 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014755 }
14756 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014757 if (s == NULL)
14758 {
14759 do_outofmem_msg((long_u) prevlen + len + 1);
14760 failed = TRUE;
14761 break;
14762 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014763
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014764 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014765 {
14766 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014767 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014768 break;
14769 }
14770 li->li_tv.v_type = VAR_STRING;
14771 li->li_tv.v_lock = 0;
14772 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014773 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014774
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014775 start = p + 1; /* step over newline */
14776 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014777 break;
14778 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014779 else if (*p == NUL)
14780 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014781#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014782 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14783 * when finding the BF and check the previous two bytes. */
14784 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014785 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014786 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14787 * + 1, these may be in the "prev" string. */
14788 char_u back1 = p >= buf + 1 ? p[-1]
14789 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14790 char_u back2 = p >= buf + 2 ? p[-2]
14791 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14792 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014793
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014794 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014795 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014796 char_u *dest = p - 2;
14797
14798 /* Usually a BOM is at the beginning of a file, and so at
14799 * the beginning of a line; then we can just step over it.
14800 */
14801 if (start == dest)
14802 start = p + 1;
14803 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014804 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014805 /* have to shuffle buf to close gap */
14806 int adjust_prevlen = 0;
14807
14808 if (dest < buf)
14809 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014810 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014811 dest = buf;
14812 }
14813 if (readlen > p - buf + 1)
14814 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14815 readlen -= 3 - adjust_prevlen;
14816 prevlen -= adjust_prevlen;
14817 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014818 }
14819 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014820 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014821#endif
14822 } /* for */
14823
14824 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14825 break;
14826 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014827 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014828 /* There's part of a line in buf, store it in "prev". */
14829 if (p - start + prevlen >= prevsize)
14830 {
14831 /* need bigger "prev" buffer */
14832 char_u *newprev;
14833
14834 /* A common use case is ordinary text files and "prev" gets a
14835 * fragment of a line, so the first allocation is made
14836 * small, to avoid repeatedly 'allocing' large and
14837 * 'reallocing' small. */
14838 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014839 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014840 else
14841 {
14842 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014843 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014844 prevsize = grow50pc > growmin ? grow50pc : growmin;
14845 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014846 newprev = prev == NULL ? alloc(prevsize)
14847 : vim_realloc(prev, prevsize);
14848 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014849 {
14850 do_outofmem_msg((long_u)prevsize);
14851 failed = TRUE;
14852 break;
14853 }
14854 prev = newprev;
14855 }
14856 /* Add the line part to end of "prev". */
14857 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014858 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014859 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014860 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014861
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014862 /*
14863 * For a negative line count use only the lines at the end of the file,
14864 * free the rest.
14865 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014866 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014867 while (cnt > -maxline)
14868 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014869 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014870 --cnt;
14871 }
14872
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014873 if (failed)
14874 {
14875 list_free(rettv->vval.v_list, TRUE);
14876 /* readfile doc says an empty list is returned on error */
14877 rettv->vval.v_list = list_alloc();
14878 }
14879
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014880 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014881 fclose(fd);
14882}
14883
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014884#if defined(FEAT_RELTIME)
14885static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14886
14887/*
14888 * Convert a List to proftime_T.
14889 * Return FAIL when there is something wrong.
14890 */
14891 static int
14892list2proftime(arg, tm)
14893 typval_T *arg;
14894 proftime_T *tm;
14895{
14896 long n1, n2;
14897 int error = FALSE;
14898
14899 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14900 || arg->vval.v_list->lv_len != 2)
14901 return FAIL;
14902 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14903 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14904# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014905 tm->HighPart = n1;
14906 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014907# else
14908 tm->tv_sec = n1;
14909 tm->tv_usec = n2;
14910# endif
14911 return error ? FAIL : OK;
14912}
14913#endif /* FEAT_RELTIME */
14914
14915/*
14916 * "reltime()" function
14917 */
14918 static void
14919f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014920 typval_T *argvars UNUSED;
14921 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014922{
14923#ifdef FEAT_RELTIME
14924 proftime_T res;
14925 proftime_T start;
14926
14927 if (argvars[0].v_type == VAR_UNKNOWN)
14928 {
14929 /* No arguments: get current time. */
14930 profile_start(&res);
14931 }
14932 else if (argvars[1].v_type == VAR_UNKNOWN)
14933 {
14934 if (list2proftime(&argvars[0], &res) == FAIL)
14935 return;
14936 profile_end(&res);
14937 }
14938 else
14939 {
14940 /* Two arguments: compute the difference. */
14941 if (list2proftime(&argvars[0], &start) == FAIL
14942 || list2proftime(&argvars[1], &res) == FAIL)
14943 return;
14944 profile_sub(&res, &start);
14945 }
14946
14947 if (rettv_list_alloc(rettv) == OK)
14948 {
14949 long n1, n2;
14950
14951# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014952 n1 = res.HighPart;
14953 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014954# else
14955 n1 = res.tv_sec;
14956 n2 = res.tv_usec;
14957# endif
14958 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14959 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14960 }
14961#endif
14962}
14963
14964/*
14965 * "reltimestr()" function
14966 */
14967 static void
14968f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014969 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014970 typval_T *rettv;
14971{
14972#ifdef FEAT_RELTIME
14973 proftime_T tm;
14974#endif
14975
14976 rettv->v_type = VAR_STRING;
14977 rettv->vval.v_string = NULL;
14978#ifdef FEAT_RELTIME
14979 if (list2proftime(&argvars[0], &tm) == OK)
14980 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14981#endif
14982}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014983
Bram Moolenaar0d660222005-01-07 21:51:51 +000014984#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14985static void make_connection __ARGS((void));
14986static int check_connection __ARGS((void));
14987
14988 static void
14989make_connection()
14990{
14991 if (X_DISPLAY == NULL
14992# ifdef FEAT_GUI
14993 && !gui.in_use
14994# endif
14995 )
14996 {
14997 x_force_connect = TRUE;
14998 setup_term_clip();
14999 x_force_connect = FALSE;
15000 }
15001}
15002
15003 static int
15004check_connection()
15005{
15006 make_connection();
15007 if (X_DISPLAY == NULL)
15008 {
15009 EMSG(_("E240: No connection to Vim server"));
15010 return FAIL;
15011 }
15012 return OK;
15013}
15014#endif
15015
15016#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015017static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015018
15019 static void
15020remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015021 typval_T *argvars;
15022 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015023 int expr;
15024{
15025 char_u *server_name;
15026 char_u *keys;
15027 char_u *r = NULL;
15028 char_u buf[NUMBUFLEN];
15029# ifdef WIN32
15030 HWND w;
15031# else
15032 Window w;
15033# endif
15034
15035 if (check_restricted() || check_secure())
15036 return;
15037
15038# ifdef FEAT_X11
15039 if (check_connection() == FAIL)
15040 return;
15041# endif
15042
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015043 server_name = get_tv_string_chk(&argvars[0]);
15044 if (server_name == NULL)
15045 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046 keys = get_tv_string_buf(&argvars[1], buf);
15047# ifdef WIN32
15048 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15049# else
15050 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15051 < 0)
15052# endif
15053 {
15054 if (r != NULL)
15055 EMSG(r); /* sending worked but evaluation failed */
15056 else
15057 EMSG2(_("E241: Unable to send to %s"), server_name);
15058 return;
15059 }
15060
15061 rettv->vval.v_string = r;
15062
15063 if (argvars[2].v_type != VAR_UNKNOWN)
15064 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015065 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015066 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015067 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015068
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015069 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015070 v.di_tv.v_type = VAR_STRING;
15071 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015072 idvar = get_tv_string_chk(&argvars[2]);
15073 if (idvar != NULL)
15074 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015075 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015076 }
15077}
15078#endif
15079
15080/*
15081 * "remote_expr()" function
15082 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015083 static void
15084f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015085 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015086 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015087{
15088 rettv->v_type = VAR_STRING;
15089 rettv->vval.v_string = NULL;
15090#ifdef FEAT_CLIENTSERVER
15091 remote_common(argvars, rettv, TRUE);
15092#endif
15093}
15094
15095/*
15096 * "remote_foreground()" function
15097 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015098 static void
15099f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015100 typval_T *argvars UNUSED;
15101 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015102{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015103#ifdef FEAT_CLIENTSERVER
15104# ifdef WIN32
15105 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015106 {
15107 char_u *server_name = get_tv_string_chk(&argvars[0]);
15108
15109 if (server_name != NULL)
15110 serverForeground(server_name);
15111 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112# else
15113 /* Send a foreground() expression to the server. */
15114 argvars[1].v_type = VAR_STRING;
15115 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15116 argvars[2].v_type = VAR_UNKNOWN;
15117 remote_common(argvars, rettv, TRUE);
15118 vim_free(argvars[1].vval.v_string);
15119# endif
15120#endif
15121}
15122
Bram Moolenaar0d660222005-01-07 21:51:51 +000015123 static void
15124f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015125 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015126 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015127{
15128#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015129 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015130 char_u *s = NULL;
15131# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015132 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015133# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015134 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015135
15136 if (check_restricted() || check_secure())
15137 {
15138 rettv->vval.v_number = -1;
15139 return;
15140 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015141 serverid = get_tv_string_chk(&argvars[0]);
15142 if (serverid == NULL)
15143 {
15144 rettv->vval.v_number = -1;
15145 return; /* type error; errmsg already given */
15146 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015147# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015148 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015149 if (n == 0)
15150 rettv->vval.v_number = -1;
15151 else
15152 {
15153 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15154 rettv->vval.v_number = (s != NULL);
15155 }
15156# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015157 if (check_connection() == FAIL)
15158 return;
15159
15160 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015161 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015162# endif
15163
15164 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15165 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015166 char_u *retvar;
15167
Bram Moolenaar33570922005-01-25 22:26:29 +000015168 v.di_tv.v_type = VAR_STRING;
15169 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015170 retvar = get_tv_string_chk(&argvars[1]);
15171 if (retvar != NULL)
15172 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015173 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015174 }
15175#else
15176 rettv->vval.v_number = -1;
15177#endif
15178}
15179
Bram Moolenaar0d660222005-01-07 21:51:51 +000015180 static void
15181f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015182 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015183 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015184{
15185 char_u *r = NULL;
15186
15187#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015188 char_u *serverid = get_tv_string_chk(&argvars[0]);
15189
15190 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015191 {
15192# ifdef WIN32
15193 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015194 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015195
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015196 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015197 if (n != 0)
15198 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15199 if (r == NULL)
15200# else
15201 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015202 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015203# endif
15204 EMSG(_("E277: Unable to read a server reply"));
15205 }
15206#endif
15207 rettv->v_type = VAR_STRING;
15208 rettv->vval.v_string = r;
15209}
15210
15211/*
15212 * "remote_send()" function
15213 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015214 static void
15215f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015216 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015217 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015218{
15219 rettv->v_type = VAR_STRING;
15220 rettv->vval.v_string = NULL;
15221#ifdef FEAT_CLIENTSERVER
15222 remote_common(argvars, rettv, FALSE);
15223#endif
15224}
15225
15226/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015227 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015228 */
15229 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015230f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015231 typval_T *argvars;
15232 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015233{
Bram Moolenaar33570922005-01-25 22:26:29 +000015234 list_T *l;
15235 listitem_T *item, *item2;
15236 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015237 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015238 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015239 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015240 dict_T *d;
15241 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015242 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015243
Bram Moolenaar8c711452005-01-14 21:53:12 +000015244 if (argvars[0].v_type == VAR_DICT)
15245 {
15246 if (argvars[2].v_type != VAR_UNKNOWN)
15247 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015248 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015249 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015250 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015251 key = get_tv_string_chk(&argvars[1]);
15252 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015253 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015254 di = dict_find(d, key, -1);
15255 if (di == NULL)
15256 EMSG2(_(e_dictkey), key);
15257 else
15258 {
15259 *rettv = di->di_tv;
15260 init_tv(&di->di_tv);
15261 dictitem_remove(d, di);
15262 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015263 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015264 }
15265 }
15266 else if (argvars[0].v_type != VAR_LIST)
15267 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015268 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015269 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015270 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015271 int error = FALSE;
15272
15273 idx = get_tv_number_chk(&argvars[1], &error);
15274 if (error)
15275 ; /* type error: do nothing, errmsg already given */
15276 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015277 EMSGN(_(e_listidx), idx);
15278 else
15279 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015280 if (argvars[2].v_type == VAR_UNKNOWN)
15281 {
15282 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015283 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015284 *rettv = item->li_tv;
15285 vim_free(item);
15286 }
15287 else
15288 {
15289 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015290 end = get_tv_number_chk(&argvars[2], &error);
15291 if (error)
15292 ; /* type error: do nothing */
15293 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015294 EMSGN(_(e_listidx), end);
15295 else
15296 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015297 int cnt = 0;
15298
15299 for (li = item; li != NULL; li = li->li_next)
15300 {
15301 ++cnt;
15302 if (li == item2)
15303 break;
15304 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015305 if (li == NULL) /* didn't find "item2" after "item" */
15306 EMSG(_(e_invrange));
15307 else
15308 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015309 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015310 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015311 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015312 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015313 l->lv_first = item;
15314 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015315 item->li_prev = NULL;
15316 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015317 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015318 }
15319 }
15320 }
15321 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015322 }
15323 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015324}
15325
15326/*
15327 * "rename({from}, {to})" function
15328 */
15329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015330f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015331 typval_T *argvars;
15332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333{
15334 char_u buf[NUMBUFLEN];
15335
15336 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015337 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015339 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15340 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341}
15342
15343/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015344 * "repeat()" function
15345 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015346 static void
15347f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015348 typval_T *argvars;
15349 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015350{
15351 char_u *p;
15352 int n;
15353 int slen;
15354 int len;
15355 char_u *r;
15356 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015357
15358 n = get_tv_number(&argvars[1]);
15359 if (argvars[0].v_type == VAR_LIST)
15360 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015361 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015362 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015363 if (list_extend(rettv->vval.v_list,
15364 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015365 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015366 }
15367 else
15368 {
15369 p = get_tv_string(&argvars[0]);
15370 rettv->v_type = VAR_STRING;
15371 rettv->vval.v_string = NULL;
15372
15373 slen = (int)STRLEN(p);
15374 len = slen * n;
15375 if (len <= 0)
15376 return;
15377
15378 r = alloc(len + 1);
15379 if (r != NULL)
15380 {
15381 for (i = 0; i < n; i++)
15382 mch_memmove(r + i * slen, p, (size_t)slen);
15383 r[len] = NUL;
15384 }
15385
15386 rettv->vval.v_string = r;
15387 }
15388}
15389
15390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391 * "resolve()" function
15392 */
15393 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015394f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015395 typval_T *argvars;
15396 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015397{
15398 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015399#ifdef HAVE_READLINK
15400 char_u *buf = NULL;
15401#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015403 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015404#ifdef FEAT_SHORTCUT
15405 {
15406 char_u *v = NULL;
15407
15408 v = mch_resolve_shortcut(p);
15409 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015410 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015412 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 }
15414#else
15415# ifdef HAVE_READLINK
15416 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417 char_u *cpy;
15418 int len;
15419 char_u *remain = NULL;
15420 char_u *q;
15421 int is_relative_to_current = FALSE;
15422 int has_trailing_pathsep = FALSE;
15423 int limit = 100;
15424
15425 p = vim_strsave(p);
15426
15427 if (p[0] == '.' && (vim_ispathsep(p[1])
15428 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15429 is_relative_to_current = TRUE;
15430
15431 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015432 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015433 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015434 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015435 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437
15438 q = getnextcomp(p);
15439 if (*q != NUL)
15440 {
15441 /* Separate the first path component in "p", and keep the
15442 * remainder (beginning with the path separator). */
15443 remain = vim_strsave(q - 1);
15444 q[-1] = NUL;
15445 }
15446
Bram Moolenaard9462e32011-04-11 21:35:11 +020015447 buf = alloc(MAXPATHL + 1);
15448 if (buf == NULL)
15449 goto fail;
15450
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 for (;;)
15452 {
15453 for (;;)
15454 {
15455 len = readlink((char *)p, (char *)buf, MAXPATHL);
15456 if (len <= 0)
15457 break;
15458 buf[len] = NUL;
15459
15460 if (limit-- == 0)
15461 {
15462 vim_free(p);
15463 vim_free(remain);
15464 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015465 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466 goto fail;
15467 }
15468
15469 /* Ensure that the result will have a trailing path separator
15470 * if the argument has one. */
15471 if (remain == NULL && has_trailing_pathsep)
15472 add_pathsep(buf);
15473
15474 /* Separate the first path component in the link value and
15475 * concatenate the remainders. */
15476 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15477 if (*q != NUL)
15478 {
15479 if (remain == NULL)
15480 remain = vim_strsave(q - 1);
15481 else
15482 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015483 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484 if (cpy != NULL)
15485 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015486 vim_free(remain);
15487 remain = cpy;
15488 }
15489 }
15490 q[-1] = NUL;
15491 }
15492
15493 q = gettail(p);
15494 if (q > p && *q == NUL)
15495 {
15496 /* Ignore trailing path separator. */
15497 q[-1] = NUL;
15498 q = gettail(p);
15499 }
15500 if (q > p && !mch_isFullName(buf))
15501 {
15502 /* symlink is relative to directory of argument */
15503 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15504 if (cpy != NULL)
15505 {
15506 STRCPY(cpy, p);
15507 STRCPY(gettail(cpy), buf);
15508 vim_free(p);
15509 p = cpy;
15510 }
15511 }
15512 else
15513 {
15514 vim_free(p);
15515 p = vim_strsave(buf);
15516 }
15517 }
15518
15519 if (remain == NULL)
15520 break;
15521
15522 /* Append the first path component of "remain" to "p". */
15523 q = getnextcomp(remain + 1);
15524 len = q - remain - (*q != NUL);
15525 cpy = vim_strnsave(p, STRLEN(p) + len);
15526 if (cpy != NULL)
15527 {
15528 STRNCAT(cpy, remain, len);
15529 vim_free(p);
15530 p = cpy;
15531 }
15532 /* Shorten "remain". */
15533 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015534 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 else
15536 {
15537 vim_free(remain);
15538 remain = NULL;
15539 }
15540 }
15541
15542 /* If the result is a relative path name, make it explicitly relative to
15543 * the current directory if and only if the argument had this form. */
15544 if (!vim_ispathsep(*p))
15545 {
15546 if (is_relative_to_current
15547 && *p != NUL
15548 && !(p[0] == '.'
15549 && (p[1] == NUL
15550 || vim_ispathsep(p[1])
15551 || (p[1] == '.'
15552 && (p[2] == NUL
15553 || vim_ispathsep(p[2]))))))
15554 {
15555 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015556 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 if (cpy != NULL)
15558 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015559 vim_free(p);
15560 p = cpy;
15561 }
15562 }
15563 else if (!is_relative_to_current)
15564 {
15565 /* Strip leading "./". */
15566 q = p;
15567 while (q[0] == '.' && vim_ispathsep(q[1]))
15568 q += 2;
15569 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015570 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015571 }
15572 }
15573
15574 /* Ensure that the result will have no trailing path separator
15575 * if the argument had none. But keep "/" or "//". */
15576 if (!has_trailing_pathsep)
15577 {
15578 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015579 if (after_pathsep(p, q))
15580 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015581 }
15582
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015583 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 }
15585# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015586 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587# endif
15588#endif
15589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015590 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591
15592#ifdef HAVE_READLINK
15593fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015594 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015595#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015596 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597}
15598
15599/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015600 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015601 */
15602 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015603f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015604 typval_T *argvars;
15605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015606{
Bram Moolenaar33570922005-01-25 22:26:29 +000015607 list_T *l;
15608 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015609
Bram Moolenaar0d660222005-01-07 21:51:51 +000015610 if (argvars[0].v_type != VAR_LIST)
15611 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015612 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015613 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015614 {
15615 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015616 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015617 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015618 while (li != NULL)
15619 {
15620 ni = li->li_prev;
15621 list_append(l, li);
15622 li = ni;
15623 }
15624 rettv->vval.v_list = l;
15625 rettv->v_type = VAR_LIST;
15626 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015627 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629}
15630
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015631#define SP_NOMOVE 0x01 /* don't move cursor */
15632#define SP_REPEAT 0x02 /* repeat to find outer pair */
15633#define SP_RETCOUNT 0x04 /* return matchcount */
15634#define SP_SETPCMARK 0x08 /* set previous context mark */
15635#define SP_START 0x10 /* accept match at start position */
15636#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15637#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015638
Bram Moolenaar33570922005-01-25 22:26:29 +000015639static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015640
15641/*
15642 * Get flags for a search function.
15643 * Possibly sets "p_ws".
15644 * Returns BACKWARD, FORWARD or zero (for an error).
15645 */
15646 static int
15647get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015648 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015649 int *flagsp;
15650{
15651 int dir = FORWARD;
15652 char_u *flags;
15653 char_u nbuf[NUMBUFLEN];
15654 int mask;
15655
15656 if (varp->v_type != VAR_UNKNOWN)
15657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015658 flags = get_tv_string_buf_chk(varp, nbuf);
15659 if (flags == NULL)
15660 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015661 while (*flags != NUL)
15662 {
15663 switch (*flags)
15664 {
15665 case 'b': dir = BACKWARD; break;
15666 case 'w': p_ws = TRUE; break;
15667 case 'W': p_ws = FALSE; break;
15668 default: mask = 0;
15669 if (flagsp != NULL)
15670 switch (*flags)
15671 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015672 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015673 case 'e': mask = SP_END; break;
15674 case 'm': mask = SP_RETCOUNT; break;
15675 case 'n': mask = SP_NOMOVE; break;
15676 case 'p': mask = SP_SUBPAT; break;
15677 case 'r': mask = SP_REPEAT; break;
15678 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015679 }
15680 if (mask == 0)
15681 {
15682 EMSG2(_(e_invarg2), flags);
15683 dir = 0;
15684 }
15685 else
15686 *flagsp |= mask;
15687 }
15688 if (dir == 0)
15689 break;
15690 ++flags;
15691 }
15692 }
15693 return dir;
15694}
15695
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015697 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015698 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015699 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015700search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015701 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015702 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015703 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015704{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015705 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015706 char_u *pat;
15707 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015708 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 int save_p_ws = p_ws;
15710 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015711 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015712 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015713 proftime_T tm;
15714#ifdef FEAT_RELTIME
15715 long time_limit = 0;
15716#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015717 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015718 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015720 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015721 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015722 if (dir == 0)
15723 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015724 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015725 if (flags & SP_START)
15726 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015727 if (flags & SP_END)
15728 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015729
Bram Moolenaar76929292008-01-06 19:07:36 +000015730 /* Optional arguments: line number to stop searching and timeout. */
15731 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015732 {
15733 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15734 if (lnum_stop < 0)
15735 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015736#ifdef FEAT_RELTIME
15737 if (argvars[3].v_type != VAR_UNKNOWN)
15738 {
15739 time_limit = get_tv_number_chk(&argvars[3], NULL);
15740 if (time_limit < 0)
15741 goto theend;
15742 }
15743#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015744 }
15745
Bram Moolenaar76929292008-01-06 19:07:36 +000015746#ifdef FEAT_RELTIME
15747 /* Set the time limit, if there is one. */
15748 profile_setlimit(time_limit, &tm);
15749#endif
15750
Bram Moolenaar231334e2005-07-25 20:46:57 +000015751 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015752 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015753 * Check to make sure only those flags are set.
15754 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15755 * flags cannot be set. Check for that condition also.
15756 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015757 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015758 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015759 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015760 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015761 goto theend;
15762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015764 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015765 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015766 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015767 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015769 if (flags & SP_SUBPAT)
15770 retval = subpatnum;
15771 else
15772 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015773 if (flags & SP_SETPCMARK)
15774 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015776 if (match_pos != NULL)
15777 {
15778 /* Store the match cursor position */
15779 match_pos->lnum = pos.lnum;
15780 match_pos->col = pos.col + 1;
15781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015782 /* "/$" will put the cursor after the end of the line, may need to
15783 * correct that here */
15784 check_cursor();
15785 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015786
15787 /* If 'n' flag is used: restore cursor position. */
15788 if (flags & SP_NOMOVE)
15789 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015790 else
15791 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015792theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015793 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015794
15795 return retval;
15796}
15797
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015798#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015799
15800/*
15801 * round() is not in C90, use ceil() or floor() instead.
15802 */
15803 float_T
15804vim_round(f)
15805 float_T f;
15806{
15807 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15808}
15809
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015810/*
15811 * "round({float})" function
15812 */
15813 static void
15814f_round(argvars, rettv)
15815 typval_T *argvars;
15816 typval_T *rettv;
15817{
15818 float_T f;
15819
15820 rettv->v_type = VAR_FLOAT;
15821 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015822 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015823 else
15824 rettv->vval.v_float = 0.0;
15825}
15826#endif
15827
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015828/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015829 * "screenattr()" function
15830 */
15831 static void
15832f_screenattr(argvars, rettv)
15833 typval_T *argvars UNUSED;
15834 typval_T *rettv;
15835{
15836 int row;
15837 int col;
15838 int c;
15839
15840 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15841 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15842 if (row < 0 || row >= screen_Rows
15843 || col < 0 || col >= screen_Columns)
15844 c = -1;
15845 else
15846 c = ScreenAttrs[LineOffset[row] + col];
15847 rettv->vval.v_number = c;
15848}
15849
15850/*
15851 * "screenchar()" function
15852 */
15853 static void
15854f_screenchar(argvars, rettv)
15855 typval_T *argvars UNUSED;
15856 typval_T *rettv;
15857{
15858 int row;
15859 int col;
15860 int off;
15861 int c;
15862
15863 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15864 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15865 if (row < 0 || row >= screen_Rows
15866 || col < 0 || col >= screen_Columns)
15867 c = -1;
15868 else
15869 {
15870 off = LineOffset[row] + col;
15871#ifdef FEAT_MBYTE
15872 if (enc_utf8 && ScreenLinesUC[off] != 0)
15873 c = ScreenLinesUC[off];
15874 else
15875#endif
15876 c = ScreenLines[off];
15877 }
15878 rettv->vval.v_number = c;
15879}
15880
15881/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015882 * "screencol()" function
15883 *
15884 * First column is 1 to be consistent with virtcol().
15885 */
15886 static void
15887f_screencol(argvars, rettv)
15888 typval_T *argvars UNUSED;
15889 typval_T *rettv;
15890{
15891 rettv->vval.v_number = screen_screencol() + 1;
15892}
15893
15894/*
15895 * "screenrow()" function
15896 */
15897 static void
15898f_screenrow(argvars, rettv)
15899 typval_T *argvars UNUSED;
15900 typval_T *rettv;
15901{
15902 rettv->vval.v_number = screen_screenrow() + 1;
15903}
15904
15905/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015906 * "search()" function
15907 */
15908 static void
15909f_search(argvars, rettv)
15910 typval_T *argvars;
15911 typval_T *rettv;
15912{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015913 int flags = 0;
15914
15915 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015916}
15917
Bram Moolenaar071d4272004-06-13 20:20:40 +000015918/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015919 * "searchdecl()" function
15920 */
15921 static void
15922f_searchdecl(argvars, rettv)
15923 typval_T *argvars;
15924 typval_T *rettv;
15925{
15926 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015927 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015928 int error = FALSE;
15929 char_u *name;
15930
15931 rettv->vval.v_number = 1; /* default: FAIL */
15932
15933 name = get_tv_string_chk(&argvars[0]);
15934 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015935 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015936 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015937 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15938 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15939 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015940 if (!error && name != NULL)
15941 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015942 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015943}
15944
15945/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015946 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015947 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015948 static int
15949searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015950 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015951 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952{
15953 char_u *spat, *mpat, *epat;
15954 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015955 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015956 int dir;
15957 int flags = 0;
15958 char_u nbuf1[NUMBUFLEN];
15959 char_u nbuf2[NUMBUFLEN];
15960 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015961 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015962 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015963 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015966 spat = get_tv_string_chk(&argvars[0]);
15967 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15968 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15969 if (spat == NULL || mpat == NULL || epat == NULL)
15970 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015971
Bram Moolenaar071d4272004-06-13 20:20:40 +000015972 /* Handle the optional fourth argument: flags */
15973 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015974 if (dir == 0)
15975 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015976
15977 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015978 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15979 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015980 if ((flags & (SP_END | SP_SUBPAT)) != 0
15981 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015982 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015983 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015984 goto theend;
15985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015986
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015987 /* Using 'r' implies 'W', otherwise it doesn't work. */
15988 if (flags & SP_REPEAT)
15989 p_ws = FALSE;
15990
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015991 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015992 if (argvars[3].v_type == VAR_UNKNOWN
15993 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015994 skip = (char_u *)"";
15995 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015996 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015997 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015998 if (argvars[5].v_type != VAR_UNKNOWN)
15999 {
16000 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16001 if (lnum_stop < 0)
16002 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016003#ifdef FEAT_RELTIME
16004 if (argvars[6].v_type != VAR_UNKNOWN)
16005 {
16006 time_limit = get_tv_number_chk(&argvars[6], NULL);
16007 if (time_limit < 0)
16008 goto theend;
16009 }
16010#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016011 }
16012 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016013 if (skip == NULL)
16014 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016015
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016016 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016017 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016018
16019theend:
16020 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016021
16022 return retval;
16023}
16024
16025/*
16026 * "searchpair()" function
16027 */
16028 static void
16029f_searchpair(argvars, rettv)
16030 typval_T *argvars;
16031 typval_T *rettv;
16032{
16033 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16034}
16035
16036/*
16037 * "searchpairpos()" function
16038 */
16039 static void
16040f_searchpairpos(argvars, rettv)
16041 typval_T *argvars;
16042 typval_T *rettv;
16043{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016044 pos_T match_pos;
16045 int lnum = 0;
16046 int col = 0;
16047
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016048 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016049 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016050
16051 if (searchpair_cmn(argvars, &match_pos) > 0)
16052 {
16053 lnum = match_pos.lnum;
16054 col = match_pos.col;
16055 }
16056
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016057 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16058 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016059}
16060
16061/*
16062 * Search for a start/middle/end thing.
16063 * Used by searchpair(), see its documentation for the details.
16064 * Returns 0 or -1 for no match,
16065 */
16066 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016067do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16068 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016069 char_u *spat; /* start pattern */
16070 char_u *mpat; /* middle pattern */
16071 char_u *epat; /* end pattern */
16072 int dir; /* BACKWARD or FORWARD */
16073 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016074 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016075 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016076 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016077 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016078{
16079 char_u *save_cpo;
16080 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16081 long retval = 0;
16082 pos_T pos;
16083 pos_T firstpos;
16084 pos_T foundpos;
16085 pos_T save_cursor;
16086 pos_T save_pos;
16087 int n;
16088 int r;
16089 int nest = 1;
16090 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016091 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016092 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016093
16094 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16095 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016096 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016097
Bram Moolenaar76929292008-01-06 19:07:36 +000016098#ifdef FEAT_RELTIME
16099 /* Set the time limit, if there is one. */
16100 profile_setlimit(time_limit, &tm);
16101#endif
16102
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016103 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16104 * start/middle/end (pat3, for the top pair). */
16105 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16106 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16107 if (pat2 == NULL || pat3 == NULL)
16108 goto theend;
16109 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16110 if (*mpat == NUL)
16111 STRCPY(pat3, pat2);
16112 else
16113 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16114 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016115 if (flags & SP_START)
16116 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016117
Bram Moolenaar071d4272004-06-13 20:20:40 +000016118 save_cursor = curwin->w_cursor;
16119 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016120 clearpos(&firstpos);
16121 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016122 pat = pat3;
16123 for (;;)
16124 {
16125 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016126 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016127 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16128 /* didn't find it or found the first match again: FAIL */
16129 break;
16130
16131 if (firstpos.lnum == 0)
16132 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016133 if (equalpos(pos, foundpos))
16134 {
16135 /* Found the same position again. Can happen with a pattern that
16136 * has "\zs" at the end and searching backwards. Advance one
16137 * character and try again. */
16138 if (dir == BACKWARD)
16139 decl(&pos);
16140 else
16141 incl(&pos);
16142 }
16143 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016145 /* clear the start flag to avoid getting stuck here */
16146 options &= ~SEARCH_START;
16147
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 /* If the skip pattern matches, ignore this match. */
16149 if (*skip != NUL)
16150 {
16151 save_pos = curwin->w_cursor;
16152 curwin->w_cursor = pos;
16153 r = eval_to_bool(skip, &err, NULL, FALSE);
16154 curwin->w_cursor = save_pos;
16155 if (err)
16156 {
16157 /* Evaluating {skip} caused an error, break here. */
16158 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016159 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016160 break;
16161 }
16162 if (r)
16163 continue;
16164 }
16165
16166 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16167 {
16168 /* Found end when searching backwards or start when searching
16169 * forward: nested pair. */
16170 ++nest;
16171 pat = pat2; /* nested, don't search for middle */
16172 }
16173 else
16174 {
16175 /* Found end when searching forward or start when searching
16176 * backward: end of (nested) pair; or found middle in outer pair. */
16177 if (--nest == 1)
16178 pat = pat3; /* outer level, search for middle */
16179 }
16180
16181 if (nest == 0)
16182 {
16183 /* Found the match: return matchcount or line number. */
16184 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016185 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016187 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016188 if (flags & SP_SETPCMARK)
16189 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016190 curwin->w_cursor = pos;
16191 if (!(flags & SP_REPEAT))
16192 break;
16193 nest = 1; /* search for next unmatched */
16194 }
16195 }
16196
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016197 if (match_pos != NULL)
16198 {
16199 /* Store the match cursor position */
16200 match_pos->lnum = curwin->w_cursor.lnum;
16201 match_pos->col = curwin->w_cursor.col + 1;
16202 }
16203
Bram Moolenaar071d4272004-06-13 20:20:40 +000016204 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016205 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206 curwin->w_cursor = save_cursor;
16207
16208theend:
16209 vim_free(pat2);
16210 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016211 if (p_cpo == empty_option)
16212 p_cpo = save_cpo;
16213 else
16214 /* Darn, evaluating the {skip} expression changed the value. */
16215 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016216
16217 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218}
16219
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016220/*
16221 * "searchpos()" function
16222 */
16223 static void
16224f_searchpos(argvars, rettv)
16225 typval_T *argvars;
16226 typval_T *rettv;
16227{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016228 pos_T match_pos;
16229 int lnum = 0;
16230 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016231 int n;
16232 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016233
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016234 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016235 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016236
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016237 n = search_cmn(argvars, &match_pos, &flags);
16238 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016239 {
16240 lnum = match_pos.lnum;
16241 col = match_pos.col;
16242 }
16243
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016244 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16245 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016246 if (flags & SP_SUBPAT)
16247 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016248}
16249
16250
Bram Moolenaar0d660222005-01-07 21:51:51 +000016251 static void
16252f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016253 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016254 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016256#ifdef FEAT_CLIENTSERVER
16257 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016258 char_u *server = get_tv_string_chk(&argvars[0]);
16259 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016260
Bram Moolenaar0d660222005-01-07 21:51:51 +000016261 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016262 if (server == NULL || reply == NULL)
16263 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016264 if (check_restricted() || check_secure())
16265 return;
16266# ifdef FEAT_X11
16267 if (check_connection() == FAIL)
16268 return;
16269# endif
16270
16271 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016272 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016273 EMSG(_("E258: Unable to send to client"));
16274 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016275 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016276 rettv->vval.v_number = 0;
16277#else
16278 rettv->vval.v_number = -1;
16279#endif
16280}
16281
Bram Moolenaar0d660222005-01-07 21:51:51 +000016282 static void
16283f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016284 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016285 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016286{
16287 char_u *r = NULL;
16288
16289#ifdef FEAT_CLIENTSERVER
16290# ifdef WIN32
16291 r = serverGetVimNames();
16292# else
16293 make_connection();
16294 if (X_DISPLAY != NULL)
16295 r = serverGetVimNames(X_DISPLAY);
16296# endif
16297#endif
16298 rettv->v_type = VAR_STRING;
16299 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016300}
16301
16302/*
16303 * "setbufvar()" function
16304 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016305 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016306f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016307 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016308 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309{
16310 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016312 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016313 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 char_u nbuf[NUMBUFLEN];
16315
16316 if (check_restricted() || check_secure())
16317 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016318 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16319 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016320 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 varp = &argvars[2];
16322
16323 if (buf != NULL && varname != NULL && varp != NULL)
16324 {
16325 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327
16328 if (*varname == '&')
16329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016330 long numval;
16331 char_u *strval;
16332 int error = FALSE;
16333
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016335 numval = get_tv_number_chk(varp, &error);
16336 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016337 if (!error && strval != NULL)
16338 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339 }
16340 else
16341 {
16342 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16343 if (bufvarname != NULL)
16344 {
16345 STRCPY(bufvarname, "b:");
16346 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016347 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 vim_free(bufvarname);
16349 }
16350 }
16351
16352 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016355}
16356
16357/*
16358 * "setcmdpos()" function
16359 */
16360 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016361f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016362 typval_T *argvars;
16363 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016365 int pos = (int)get_tv_number(&argvars[0]) - 1;
16366
16367 if (pos >= 0)
16368 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016369}
16370
16371/*
16372 * "setline()" function
16373 */
16374 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016375f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016376 typval_T *argvars;
16377 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378{
16379 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016380 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016381 list_T *l = NULL;
16382 listitem_T *li = NULL;
16383 long added = 0;
16384 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016385
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016386 lnum = get_tv_lnum(&argvars[0]);
16387 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016389 l = argvars[1].vval.v_list;
16390 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016392 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016393 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016394
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016395 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016396 for (;;)
16397 {
16398 if (l != NULL)
16399 {
16400 /* list argument, get next string */
16401 if (li == NULL)
16402 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016403 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016404 li = li->li_next;
16405 }
16406
16407 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016408 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016409 break;
16410 if (lnum <= curbuf->b_ml.ml_line_count)
16411 {
16412 /* existing line, replace it */
16413 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16414 {
16415 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016416 if (lnum == curwin->w_cursor.lnum)
16417 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016418 rettv->vval.v_number = 0; /* OK */
16419 }
16420 }
16421 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16422 {
16423 /* lnum is one past the last line, append the line */
16424 ++added;
16425 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16426 rettv->vval.v_number = 0; /* OK */
16427 }
16428
16429 if (l == NULL) /* only one string argument */
16430 break;
16431 ++lnum;
16432 }
16433
16434 if (added > 0)
16435 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016436}
16437
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016438static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16439
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016441 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016442 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016443 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016444set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016445 win_T *wp UNUSED;
16446 typval_T *list_arg UNUSED;
16447 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016448 typval_T *rettv;
16449{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016450#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016451 char_u *act;
16452 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016453#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016454
Bram Moolenaar2641f772005-03-25 21:58:17 +000016455 rettv->vval.v_number = -1;
16456
16457#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016458 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016459 EMSG(_(e_listreq));
16460 else
16461 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016462 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016463
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016464 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016465 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016466 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016467 if (act == NULL)
16468 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016469 if (*act == 'a' || *act == 'r')
16470 action = *act;
16471 }
16472
Bram Moolenaar81484f42012-12-05 15:16:47 +010016473 if (l != NULL && set_errorlist(wp, l, action,
16474 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016475 rettv->vval.v_number = 0;
16476 }
16477#endif
16478}
16479
16480/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016481 * "setloclist()" function
16482 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016483 static void
16484f_setloclist(argvars, rettv)
16485 typval_T *argvars;
16486 typval_T *rettv;
16487{
16488 win_T *win;
16489
16490 rettv->vval.v_number = -1;
16491
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016492 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016493 if (win != NULL)
16494 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16495}
16496
16497/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016498 * "setmatches()" function
16499 */
16500 static void
16501f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016502 typval_T *argvars UNUSED;
16503 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016504{
16505#ifdef FEAT_SEARCH_EXTRA
16506 list_T *l;
16507 listitem_T *li;
16508 dict_T *d;
16509
16510 rettv->vval.v_number = -1;
16511 if (argvars[0].v_type != VAR_LIST)
16512 {
16513 EMSG(_(e_listreq));
16514 return;
16515 }
16516 if ((l = argvars[0].vval.v_list) != NULL)
16517 {
16518
16519 /* To some extent make sure that we are dealing with a list from
16520 * "getmatches()". */
16521 li = l->lv_first;
16522 while (li != NULL)
16523 {
16524 if (li->li_tv.v_type != VAR_DICT
16525 || (d = li->li_tv.vval.v_dict) == NULL)
16526 {
16527 EMSG(_(e_invarg));
16528 return;
16529 }
16530 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16531 && dict_find(d, (char_u *)"pattern", -1) != NULL
16532 && dict_find(d, (char_u *)"priority", -1) != NULL
16533 && dict_find(d, (char_u *)"id", -1) != NULL))
16534 {
16535 EMSG(_(e_invarg));
16536 return;
16537 }
16538 li = li->li_next;
16539 }
16540
16541 clear_matches(curwin);
16542 li = l->lv_first;
16543 while (li != NULL)
16544 {
16545 d = li->li_tv.vval.v_dict;
16546 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16547 get_dict_string(d, (char_u *)"pattern", FALSE),
16548 (int)get_dict_number(d, (char_u *)"priority"),
16549 (int)get_dict_number(d, (char_u *)"id"));
16550 li = li->li_next;
16551 }
16552 rettv->vval.v_number = 0;
16553 }
16554#endif
16555}
16556
16557/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016558 * "setpos()" function
16559 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016560 static void
16561f_setpos(argvars, rettv)
16562 typval_T *argvars;
16563 typval_T *rettv;
16564{
16565 pos_T pos;
16566 int fnum;
16567 char_u *name;
16568
Bram Moolenaar08250432008-02-13 11:42:46 +000016569 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016570 name = get_tv_string_chk(argvars);
16571 if (name != NULL)
16572 {
16573 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16574 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016575 if (--pos.col < 0)
16576 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016577 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016578 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016579 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016580 if (fnum == curbuf->b_fnum)
16581 {
16582 curwin->w_cursor = pos;
16583 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016584 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016585 }
16586 else
16587 EMSG(_(e_invarg));
16588 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016589 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16590 {
16591 /* set mark */
16592 if (setmark_pos(name[1], &pos, fnum) == OK)
16593 rettv->vval.v_number = 0;
16594 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016595 else
16596 EMSG(_(e_invarg));
16597 }
16598 }
16599}
16600
16601/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016602 * "setqflist()" function
16603 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016604 static void
16605f_setqflist(argvars, rettv)
16606 typval_T *argvars;
16607 typval_T *rettv;
16608{
16609 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16610}
16611
16612/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016613 * "setreg()" function
16614 */
16615 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016616f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016617 typval_T *argvars;
16618 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016619{
16620 int regname;
16621 char_u *strregname;
16622 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016623 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016624 int append;
16625 char_u yank_type;
16626 long block_len;
16627
16628 block_len = -1;
16629 yank_type = MAUTO;
16630 append = FALSE;
16631
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016632 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016633 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016634
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016635 if (strregname == NULL)
16636 return; /* type error; errmsg already given */
16637 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016638 if (regname == 0 || regname == '@')
16639 regname = '"';
16640 else if (regname == '=')
16641 return;
16642
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016643 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016644 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016645 stropt = get_tv_string_chk(&argvars[2]);
16646 if (stropt == NULL)
16647 return; /* type error */
16648 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649 switch (*stropt)
16650 {
16651 case 'a': case 'A': /* append */
16652 append = TRUE;
16653 break;
16654 case 'v': case 'c': /* character-wise selection */
16655 yank_type = MCHAR;
16656 break;
16657 case 'V': case 'l': /* line-wise selection */
16658 yank_type = MLINE;
16659 break;
16660#ifdef FEAT_VISUAL
16661 case 'b': case Ctrl_V: /* block-wise selection */
16662 yank_type = MBLOCK;
16663 if (VIM_ISDIGIT(stropt[1]))
16664 {
16665 ++stropt;
16666 block_len = getdigits(&stropt) - 1;
16667 --stropt;
16668 }
16669 break;
16670#endif
16671 }
16672 }
16673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016674 strval = get_tv_string_chk(&argvars[1]);
16675 if (strval != NULL)
16676 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016677 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016678 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679}
16680
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016681/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016682 * "settabvar()" function
16683 */
16684 static void
16685f_settabvar(argvars, rettv)
16686 typval_T *argvars;
16687 typval_T *rettv;
16688{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016689#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016690 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016691 tabpage_T *tp;
16692#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016693 char_u *varname, *tabvarname;
16694 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016695
16696 rettv->vval.v_number = 0;
16697
16698 if (check_restricted() || check_secure())
16699 return;
16700
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016701#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016702 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016703#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016704 varname = get_tv_string_chk(&argvars[1]);
16705 varp = &argvars[2];
16706
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016707 if (varname != NULL && varp != NULL
16708#ifdef FEAT_WINDOWS
16709 && tp != NULL
16710#endif
16711 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016712 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016713#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016714 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016715 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016716#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016717
16718 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16719 if (tabvarname != NULL)
16720 {
16721 STRCPY(tabvarname, "t:");
16722 STRCPY(tabvarname + 2, varname);
16723 set_var(tabvarname, varp, TRUE);
16724 vim_free(tabvarname);
16725 }
16726
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016727#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016728 /* Restore current tabpage */
16729 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016730 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016731#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016732 }
16733}
16734
16735/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016736 * "settabwinvar()" function
16737 */
16738 static void
16739f_settabwinvar(argvars, rettv)
16740 typval_T *argvars;
16741 typval_T *rettv;
16742{
16743 setwinvar(argvars, rettv, 1);
16744}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016745
16746/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016747 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016749 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016750f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016751 typval_T *argvars;
16752 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016753{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016754 setwinvar(argvars, rettv, 0);
16755}
16756
16757/*
16758 * "setwinvar()" and "settabwinvar()" functions
16759 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016760
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016761 static void
16762setwinvar(argvars, rettv, off)
16763 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016764 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016765 int off;
16766{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 win_T *win;
16768#ifdef FEAT_WINDOWS
16769 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016770 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771#endif
16772 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016773 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016774 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016775 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776
16777 if (check_restricted() || check_secure())
16778 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016779
16780#ifdef FEAT_WINDOWS
16781 if (off == 1)
16782 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16783 else
16784 tp = curtab;
16785#endif
16786 win = find_win_by_nr(&argvars[off], tp);
16787 varname = get_tv_string_chk(&argvars[off + 1]);
16788 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789
16790 if (win != NULL && varname != NULL && varp != NULL)
16791 {
16792#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016793 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016794 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016795#endif
16796
16797 if (*varname == '&')
16798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016799 long numval;
16800 char_u *strval;
16801 int error = FALSE;
16802
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016804 numval = get_tv_number_chk(varp, &error);
16805 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016806 if (!error && strval != NULL)
16807 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016808 }
16809 else
16810 {
16811 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16812 if (winvarname != NULL)
16813 {
16814 STRCPY(winvarname, "w:");
16815 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016816 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016817 vim_free(winvarname);
16818 }
16819 }
16820
16821#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016822 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016823#endif
16824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825}
16826
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016827#ifdef FEAT_CRYPT
16828/*
16829 * "sha256({string})" function
16830 */
16831 static void
16832f_sha256(argvars, rettv)
16833 typval_T *argvars;
16834 typval_T *rettv;
16835{
16836 char_u *p;
16837
16838 p = get_tv_string(&argvars[0]);
16839 rettv->vval.v_string = vim_strsave(
16840 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16841 rettv->v_type = VAR_STRING;
16842}
16843#endif /* FEAT_CRYPT */
16844
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016846 * "shellescape({string})" function
16847 */
16848 static void
16849f_shellescape(argvars, rettv)
16850 typval_T *argvars;
16851 typval_T *rettv;
16852{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016853 rettv->vval.v_string = vim_strsave_shellescape(
16854 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016855 rettv->v_type = VAR_STRING;
16856}
16857
16858/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016859 * shiftwidth() function
16860 */
16861 static void
16862f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016863 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016864 typval_T *rettv;
16865{
16866 rettv->vval.v_number = get_sw_value();
16867}
16868
16869/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016870 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016871 */
16872 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016873f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016874 typval_T *argvars;
16875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016877 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878
Bram Moolenaar0d660222005-01-07 21:51:51 +000016879 p = get_tv_string(&argvars[0]);
16880 rettv->vval.v_string = vim_strsave(p);
16881 simplify_filename(rettv->vval.v_string); /* simplify in place */
16882 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016883}
16884
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016885#ifdef FEAT_FLOAT
16886/*
16887 * "sin()" function
16888 */
16889 static void
16890f_sin(argvars, rettv)
16891 typval_T *argvars;
16892 typval_T *rettv;
16893{
16894 float_T f;
16895
16896 rettv->v_type = VAR_FLOAT;
16897 if (get_float_arg(argvars, &f) == OK)
16898 rettv->vval.v_float = sin(f);
16899 else
16900 rettv->vval.v_float = 0.0;
16901}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016902
16903/*
16904 * "sinh()" function
16905 */
16906 static void
16907f_sinh(argvars, rettv)
16908 typval_T *argvars;
16909 typval_T *rettv;
16910{
16911 float_T f;
16912
16913 rettv->v_type = VAR_FLOAT;
16914 if (get_float_arg(argvars, &f) == OK)
16915 rettv->vval.v_float = sinh(f);
16916 else
16917 rettv->vval.v_float = 0.0;
16918}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016919#endif
16920
Bram Moolenaar0d660222005-01-07 21:51:51 +000016921static int
16922#ifdef __BORLANDC__
16923 _RTLENTRYF
16924#endif
16925 item_compare __ARGS((const void *s1, const void *s2));
16926static int
16927#ifdef __BORLANDC__
16928 _RTLENTRYF
16929#endif
16930 item_compare2 __ARGS((const void *s1, const void *s2));
16931
16932static int item_compare_ic;
16933static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016934static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016935static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936#define ITEM_COMPARE_FAIL 999
16937
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016940 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016941 static int
16942#ifdef __BORLANDC__
16943_RTLENTRYF
16944#endif
16945item_compare(s1, s2)
16946 const void *s1;
16947 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016948{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016949 char_u *p1, *p2;
16950 char_u *tofree1, *tofree2;
16951 int res;
16952 char_u numbuf1[NUMBUFLEN];
16953 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016955 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16956 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016957 if (p1 == NULL)
16958 p1 = (char_u *)"";
16959 if (p2 == NULL)
16960 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016961 if (item_compare_ic)
16962 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016964 res = STRCMP(p1, p2);
16965 vim_free(tofree1);
16966 vim_free(tofree2);
16967 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016968}
16969
16970 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971#ifdef __BORLANDC__
16972_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974item_compare2(s1, s2)
16975 const void *s1;
16976 const void *s2;
16977{
16978 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016979 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016980 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016981 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016982
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016983 /* shortcut after failure in previous call; compare all items equal */
16984 if (item_compare_func_err)
16985 return 0;
16986
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016987 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
16988 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000016989 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
16990 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016991
16992 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016993 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020016994 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
16995 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016996 clear_tv(&argv[0]);
16997 clear_tv(&argv[1]);
16998
16999 if (res == FAIL)
17000 res = ITEM_COMPARE_FAIL;
17001 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017002 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17003 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017004 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017005 clear_tv(&rettv);
17006 return res;
17007}
17008
17009/*
17010 * "sort({list})" function
17011 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017012 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017013f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017014 typval_T *argvars;
17015 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017016{
Bram Moolenaar33570922005-01-25 22:26:29 +000017017 list_T *l;
17018 listitem_T *li;
17019 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017020 long len;
17021 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017022
Bram Moolenaar0d660222005-01-07 21:51:51 +000017023 if (argvars[0].v_type != VAR_LIST)
17024 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 else
17026 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017027 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017028 if (l == NULL || tv_check_lock(l->lv_lock,
17029 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017030 return;
17031 rettv->vval.v_list = l;
17032 rettv->v_type = VAR_LIST;
17033 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034
Bram Moolenaar0d660222005-01-07 21:51:51 +000017035 len = list_len(l);
17036 if (len <= 1)
17037 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017038
Bram Moolenaar0d660222005-01-07 21:51:51 +000017039 item_compare_ic = FALSE;
17040 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017041 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017042 if (argvars[1].v_type != VAR_UNKNOWN)
17043 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017044 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017045 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017046 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017047 else
17048 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017049 int error = FALSE;
17050
17051 i = get_tv_number_chk(&argvars[1], &error);
17052 if (error)
17053 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017054 if (i == 1)
17055 item_compare_ic = TRUE;
17056 else
17057 item_compare_func = get_tv_string(&argvars[1]);
17058 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017059
17060 if (argvars[2].v_type != VAR_UNKNOWN)
17061 {
17062 /* optional third argument: {dict} */
17063 if (argvars[2].v_type != VAR_DICT)
17064 {
17065 EMSG(_(e_dictreq));
17066 return;
17067 }
17068 item_compare_selfdict = argvars[2].vval.v_dict;
17069 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017071
Bram Moolenaar0d660222005-01-07 21:51:51 +000017072 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017073 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017074 if (ptrs == NULL)
17075 return;
17076 i = 0;
17077 for (li = l->lv_first; li != NULL; li = li->li_next)
17078 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017080 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081 /* test the compare function */
17082 if (item_compare_func != NULL
17083 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17084 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017085 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017087 {
17088 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017089 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017090 item_compare_func == NULL ? item_compare : item_compare2);
17091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017092 if (!item_compare_func_err)
17093 {
17094 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017095 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017096 l->lv_len = 0;
17097 for (i = 0; i < len; ++i)
17098 list_append(l, ptrs[i]);
17099 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017100 }
17101
17102 vim_free(ptrs);
17103 }
17104}
17105
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017106/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017107 * "soundfold({word})" function
17108 */
17109 static void
17110f_soundfold(argvars, rettv)
17111 typval_T *argvars;
17112 typval_T *rettv;
17113{
17114 char_u *s;
17115
17116 rettv->v_type = VAR_STRING;
17117 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017118#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017119 rettv->vval.v_string = eval_soundfold(s);
17120#else
17121 rettv->vval.v_string = vim_strsave(s);
17122#endif
17123}
17124
17125/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017126 * "spellbadword()" function
17127 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017128 static void
17129f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017130 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017131 typval_T *rettv;
17132{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017133 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017134 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017135 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017136
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017137 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017138 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017139
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017140#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017141 if (argvars[0].v_type == VAR_UNKNOWN)
17142 {
17143 /* Find the start and length of the badly spelled word. */
17144 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17145 if (len != 0)
17146 word = ml_get_cursor();
17147 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017148 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017149 {
17150 char_u *str = get_tv_string_chk(&argvars[0]);
17151 int capcol = -1;
17152
17153 if (str != NULL)
17154 {
17155 /* Check the argument for spelling. */
17156 while (*str != NUL)
17157 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017158 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017159 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017160 {
17161 word = str;
17162 break;
17163 }
17164 str += len;
17165 }
17166 }
17167 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017168#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017169
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017170 list_append_string(rettv->vval.v_list, word, len);
17171 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017172 attr == HLF_SPB ? "bad" :
17173 attr == HLF_SPR ? "rare" :
17174 attr == HLF_SPL ? "local" :
17175 attr == HLF_SPC ? "caps" :
17176 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017177}
17178
17179/*
17180 * "spellsuggest()" function
17181 */
17182 static void
17183f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017184 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017185 typval_T *rettv;
17186{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017187#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017188 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017189 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017190 int maxcount;
17191 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017192 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017193 listitem_T *li;
17194 int need_capital = FALSE;
17195#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017196
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017197 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017198 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017199
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017200#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017201 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017202 {
17203 str = get_tv_string(&argvars[0]);
17204 if (argvars[1].v_type != VAR_UNKNOWN)
17205 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017206 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017207 if (maxcount <= 0)
17208 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017209 if (argvars[2].v_type != VAR_UNKNOWN)
17210 {
17211 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17212 if (typeerr)
17213 return;
17214 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017215 }
17216 else
17217 maxcount = 25;
17218
Bram Moolenaar4770d092006-01-12 23:22:24 +000017219 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017220
17221 for (i = 0; i < ga.ga_len; ++i)
17222 {
17223 str = ((char_u **)ga.ga_data)[i];
17224
17225 li = listitem_alloc();
17226 if (li == NULL)
17227 vim_free(str);
17228 else
17229 {
17230 li->li_tv.v_type = VAR_STRING;
17231 li->li_tv.v_lock = 0;
17232 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017233 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017234 }
17235 }
17236 ga_clear(&ga);
17237 }
17238#endif
17239}
17240
Bram Moolenaar0d660222005-01-07 21:51:51 +000017241 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017242f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017243 typval_T *argvars;
17244 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017245{
17246 char_u *str;
17247 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017248 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017249 regmatch_T regmatch;
17250 char_u patbuf[NUMBUFLEN];
17251 char_u *save_cpo;
17252 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017253 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017254 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017255 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017256
17257 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17258 save_cpo = p_cpo;
17259 p_cpo = (char_u *)"";
17260
17261 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017262 if (argvars[1].v_type != VAR_UNKNOWN)
17263 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017264 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17265 if (pat == NULL)
17266 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017267 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017268 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017269 }
17270 if (pat == NULL || *pat == NUL)
17271 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017272
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017273 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017275 if (typeerr)
17276 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277
Bram Moolenaar0d660222005-01-07 21:51:51 +000017278 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17279 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017281 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017282 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017283 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017284 if (*str == NUL)
17285 match = FALSE; /* empty item at the end */
17286 else
17287 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017288 if (match)
17289 end = regmatch.startp[0];
17290 else
17291 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017292 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17293 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017294 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017295 if (list_append_string(rettv->vval.v_list, str,
17296 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017297 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017298 }
17299 if (!match)
17300 break;
17301 /* Advance to just after the match. */
17302 if (regmatch.endp[0] > str)
17303 col = 0;
17304 else
17305 {
17306 /* Don't get stuck at the same match. */
17307#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017308 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017309#else
17310 col = 1;
17311#endif
17312 }
17313 str = regmatch.endp[0];
17314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315
Bram Moolenaar473de612013-06-08 18:19:48 +020017316 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017318
Bram Moolenaar0d660222005-01-07 21:51:51 +000017319 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017320}
17321
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017322#ifdef FEAT_FLOAT
17323/*
17324 * "sqrt()" function
17325 */
17326 static void
17327f_sqrt(argvars, rettv)
17328 typval_T *argvars;
17329 typval_T *rettv;
17330{
17331 float_T f;
17332
17333 rettv->v_type = VAR_FLOAT;
17334 if (get_float_arg(argvars, &f) == OK)
17335 rettv->vval.v_float = sqrt(f);
17336 else
17337 rettv->vval.v_float = 0.0;
17338}
17339
17340/*
17341 * "str2float()" function
17342 */
17343 static void
17344f_str2float(argvars, rettv)
17345 typval_T *argvars;
17346 typval_T *rettv;
17347{
17348 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17349
17350 if (*p == '+')
17351 p = skipwhite(p + 1);
17352 (void)string2float(p, &rettv->vval.v_float);
17353 rettv->v_type = VAR_FLOAT;
17354}
17355#endif
17356
Bram Moolenaar2c932302006-03-18 21:42:09 +000017357/*
17358 * "str2nr()" function
17359 */
17360 static void
17361f_str2nr(argvars, rettv)
17362 typval_T *argvars;
17363 typval_T *rettv;
17364{
17365 int base = 10;
17366 char_u *p;
17367 long n;
17368
17369 if (argvars[1].v_type != VAR_UNKNOWN)
17370 {
17371 base = get_tv_number(&argvars[1]);
17372 if (base != 8 && base != 10 && base != 16)
17373 {
17374 EMSG(_(e_invarg));
17375 return;
17376 }
17377 }
17378
17379 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017380 if (*p == '+')
17381 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017382 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17383 rettv->vval.v_number = n;
17384}
17385
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386#ifdef HAVE_STRFTIME
17387/*
17388 * "strftime({format}[, {time}])" function
17389 */
17390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017391f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017392 typval_T *argvars;
17393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017394{
17395 char_u result_buf[256];
17396 struct tm *curtime;
17397 time_t seconds;
17398 char_u *p;
17399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017400 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017401
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017402 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017403 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404 seconds = time(NULL);
17405 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017406 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017407 curtime = localtime(&seconds);
17408 /* MSVC returns NULL for an invalid value of seconds. */
17409 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017410 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411 else
17412 {
17413# ifdef FEAT_MBYTE
17414 vimconv_T conv;
17415 char_u *enc;
17416
17417 conv.vc_type = CONV_NONE;
17418 enc = enc_locale();
17419 convert_setup(&conv, p_enc, enc);
17420 if (conv.vc_type != CONV_NONE)
17421 p = string_convert(&conv, p, NULL);
17422# endif
17423 if (p != NULL)
17424 (void)strftime((char *)result_buf, sizeof(result_buf),
17425 (char *)p, curtime);
17426 else
17427 result_buf[0] = NUL;
17428
17429# ifdef FEAT_MBYTE
17430 if (conv.vc_type != CONV_NONE)
17431 vim_free(p);
17432 convert_setup(&conv, enc, p_enc);
17433 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017434 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435 else
17436# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017437 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438
17439# ifdef FEAT_MBYTE
17440 /* Release conversion descriptors */
17441 convert_setup(&conv, NULL, NULL);
17442 vim_free(enc);
17443# endif
17444 }
17445}
17446#endif
17447
17448/*
17449 * "stridx()" function
17450 */
17451 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017452f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017453 typval_T *argvars;
17454 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017455{
17456 char_u buf[NUMBUFLEN];
17457 char_u *needle;
17458 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017459 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017461 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017462
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017463 needle = get_tv_string_chk(&argvars[1]);
17464 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017465 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017466 if (needle == NULL || haystack == NULL)
17467 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468
Bram Moolenaar33570922005-01-25 22:26:29 +000017469 if (argvars[2].v_type != VAR_UNKNOWN)
17470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017471 int error = FALSE;
17472
17473 start_idx = get_tv_number_chk(&argvars[2], &error);
17474 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017475 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017476 if (start_idx >= 0)
17477 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017478 }
17479
17480 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17481 if (pos != NULL)
17482 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483}
17484
17485/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017486 * "string()" function
17487 */
17488 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017489f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017490 typval_T *argvars;
17491 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017492{
17493 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017494 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017495
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017496 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017497 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017498 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017499 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017500 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501}
17502
17503/*
17504 * "strlen()" function
17505 */
17506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017507f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017508 typval_T *argvars;
17509 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017511 rettv->vval.v_number = (varnumber_T)(STRLEN(
17512 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513}
17514
17515/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017516 * "strchars()" function
17517 */
17518 static void
17519f_strchars(argvars, rettv)
17520 typval_T *argvars;
17521 typval_T *rettv;
17522{
17523 char_u *s = get_tv_string(&argvars[0]);
17524#ifdef FEAT_MBYTE
17525 varnumber_T len = 0;
17526
17527 while (*s != NUL)
17528 {
17529 mb_cptr2char_adv(&s);
17530 ++len;
17531 }
17532 rettv->vval.v_number = len;
17533#else
17534 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17535#endif
17536}
17537
17538/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017539 * "strdisplaywidth()" function
17540 */
17541 static void
17542f_strdisplaywidth(argvars, rettv)
17543 typval_T *argvars;
17544 typval_T *rettv;
17545{
17546 char_u *s = get_tv_string(&argvars[0]);
17547 int col = 0;
17548
17549 if (argvars[1].v_type != VAR_UNKNOWN)
17550 col = get_tv_number(&argvars[1]);
17551
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017552 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017553}
17554
17555/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017556 * "strwidth()" function
17557 */
17558 static void
17559f_strwidth(argvars, rettv)
17560 typval_T *argvars;
17561 typval_T *rettv;
17562{
17563 char_u *s = get_tv_string(&argvars[0]);
17564
17565 rettv->vval.v_number = (varnumber_T)(
17566#ifdef FEAT_MBYTE
17567 mb_string2cells(s, -1)
17568#else
17569 STRLEN(s)
17570#endif
17571 );
17572}
17573
17574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575 * "strpart()" function
17576 */
17577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017578f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017579 typval_T *argvars;
17580 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581{
17582 char_u *p;
17583 int n;
17584 int len;
17585 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017586 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017588 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017589 slen = (int)STRLEN(p);
17590
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017591 n = get_tv_number_chk(&argvars[1], &error);
17592 if (error)
17593 len = 0;
17594 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017595 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017596 else
17597 len = slen - n; /* default len: all bytes that are available. */
17598
17599 /*
17600 * Only return the overlap between the specified part and the actual
17601 * string.
17602 */
17603 if (n < 0)
17604 {
17605 len += n;
17606 n = 0;
17607 }
17608 else if (n > slen)
17609 n = slen;
17610 if (len < 0)
17611 len = 0;
17612 else if (n + len > slen)
17613 len = slen - n;
17614
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017615 rettv->v_type = VAR_STRING;
17616 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617}
17618
17619/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017620 * "strridx()" function
17621 */
17622 static void
17623f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017624 typval_T *argvars;
17625 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017626{
17627 char_u buf[NUMBUFLEN];
17628 char_u *needle;
17629 char_u *haystack;
17630 char_u *rest;
17631 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017632 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017633
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017634 needle = get_tv_string_chk(&argvars[1]);
17635 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017636
17637 rettv->vval.v_number = -1;
17638 if (needle == NULL || haystack == NULL)
17639 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017640
17641 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017642 if (argvars[2].v_type != VAR_UNKNOWN)
17643 {
17644 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017645 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017646 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017647 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017648 }
17649 else
17650 end_idx = haystack_len;
17651
Bram Moolenaar0d660222005-01-07 21:51:51 +000017652 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017653 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017654 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017655 lastmatch = haystack + end_idx;
17656 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017657 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017658 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017659 for (rest = haystack; *rest != '\0'; ++rest)
17660 {
17661 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017662 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017663 break;
17664 lastmatch = rest;
17665 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017666 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017667
17668 if (lastmatch == NULL)
17669 rettv->vval.v_number = -1;
17670 else
17671 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17672}
17673
17674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 * "strtrans()" function
17676 */
17677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017678f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017679 typval_T *argvars;
17680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017681{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017682 rettv->v_type = VAR_STRING;
17683 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017684}
17685
17686/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017687 * "submatch()" function
17688 */
17689 static void
17690f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017691 typval_T *argvars;
17692 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017693{
17694 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017695 rettv->vval.v_string =
17696 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017697}
17698
17699/*
17700 * "substitute()" function
17701 */
17702 static void
17703f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017704 typval_T *argvars;
17705 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017706{
17707 char_u patbuf[NUMBUFLEN];
17708 char_u subbuf[NUMBUFLEN];
17709 char_u flagsbuf[NUMBUFLEN];
17710
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017711 char_u *str = get_tv_string_chk(&argvars[0]);
17712 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17713 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17714 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17715
Bram Moolenaar0d660222005-01-07 21:51:51 +000017716 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017717 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17718 rettv->vval.v_string = NULL;
17719 else
17720 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017721}
17722
17723/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017724 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017725 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017727f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017728 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017729 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730{
17731 int id = 0;
17732#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017733 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017734 long col;
17735 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017736 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017738 lnum = get_tv_lnum(argvars); /* -1 on type error */
17739 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17740 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017741
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017742 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017743 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017744 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017745#endif
17746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017747 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748}
17749
17750/*
17751 * "synIDattr(id, what [, mode])" function
17752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017754f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017755 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017756 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757{
17758 char_u *p = NULL;
17759#ifdef FEAT_SYN_HL
17760 int id;
17761 char_u *what;
17762 char_u *mode;
17763 char_u modebuf[NUMBUFLEN];
17764 int modec;
17765
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017766 id = get_tv_number(&argvars[0]);
17767 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017768 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017770 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017772 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 modec = 0; /* replace invalid with current */
17774 }
17775 else
17776 {
17777#ifdef FEAT_GUI
17778 if (gui.in_use)
17779 modec = 'g';
17780 else
17781#endif
17782 if (t_colors > 1)
17783 modec = 'c';
17784 else
17785 modec = 't';
17786 }
17787
17788
17789 switch (TOLOWER_ASC(what[0]))
17790 {
17791 case 'b':
17792 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17793 p = highlight_color(id, what, modec);
17794 else /* bold */
17795 p = highlight_has_attr(id, HL_BOLD, modec);
17796 break;
17797
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017798 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799 p = highlight_color(id, what, modec);
17800 break;
17801
17802 case 'i':
17803 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17804 p = highlight_has_attr(id, HL_INVERSE, modec);
17805 else /* italic */
17806 p = highlight_has_attr(id, HL_ITALIC, modec);
17807 break;
17808
17809 case 'n': /* name */
17810 p = get_highlight_name(NULL, id - 1);
17811 break;
17812
17813 case 'r': /* reverse */
17814 p = highlight_has_attr(id, HL_INVERSE, modec);
17815 break;
17816
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017817 case 's':
17818 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17819 p = highlight_color(id, what, modec);
17820 else /* standout */
17821 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822 break;
17823
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017824 case 'u':
17825 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17826 /* underline */
17827 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17828 else
17829 /* undercurl */
17830 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017831 break;
17832 }
17833
17834 if (p != NULL)
17835 p = vim_strsave(p);
17836#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017837 rettv->v_type = VAR_STRING;
17838 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017839}
17840
17841/*
17842 * "synIDtrans(id)" function
17843 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017845f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017846 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848{
17849 int id;
17850
17851#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017852 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017853
17854 if (id > 0)
17855 id = syn_get_final_id(id);
17856 else
17857#endif
17858 id = 0;
17859
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017860 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861}
17862
17863/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017864 * "synconcealed(lnum, col)" function
17865 */
17866 static void
17867f_synconcealed(argvars, rettv)
17868 typval_T *argvars UNUSED;
17869 typval_T *rettv;
17870{
17871#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17872 long lnum;
17873 long col;
17874 int syntax_flags = 0;
17875 int cchar;
17876 int matchid = 0;
17877 char_u str[NUMBUFLEN];
17878#endif
17879
17880 rettv->v_type = VAR_LIST;
17881 rettv->vval.v_list = NULL;
17882
17883#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17884 lnum = get_tv_lnum(argvars); /* -1 on type error */
17885 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17886
17887 vim_memset(str, NUL, sizeof(str));
17888
17889 if (rettv_list_alloc(rettv) != FAIL)
17890 {
17891 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17892 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17893 && curwin->w_p_cole > 0)
17894 {
17895 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17896 syntax_flags = get_syntax_info(&matchid);
17897
17898 /* get the conceal character */
17899 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17900 {
17901 cchar = syn_get_sub_char();
17902 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17903 cchar = lcs_conceal;
17904 if (cchar != NUL)
17905 {
17906# ifdef FEAT_MBYTE
17907 if (has_mbyte)
17908 (*mb_char2bytes)(cchar, str);
17909 else
17910# endif
17911 str[0] = cchar;
17912 }
17913 }
17914 }
17915
17916 list_append_number(rettv->vval.v_list,
17917 (syntax_flags & HL_CONCEAL) != 0);
17918 /* -1 to auto-determine strlen */
17919 list_append_string(rettv->vval.v_list, str, -1);
17920 list_append_number(rettv->vval.v_list, matchid);
17921 }
17922#endif
17923}
17924
17925/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017926 * "synstack(lnum, col)" function
17927 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017928 static void
17929f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017930 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017931 typval_T *rettv;
17932{
17933#ifdef FEAT_SYN_HL
17934 long lnum;
17935 long col;
17936 int i;
17937 int id;
17938#endif
17939
17940 rettv->v_type = VAR_LIST;
17941 rettv->vval.v_list = NULL;
17942
17943#ifdef FEAT_SYN_HL
17944 lnum = get_tv_lnum(argvars); /* -1 on type error */
17945 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17946
17947 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017948 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017949 && rettv_list_alloc(rettv) != FAIL)
17950 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017951 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017952 for (i = 0; ; ++i)
17953 {
17954 id = syn_get_stack_item(i);
17955 if (id < 0)
17956 break;
17957 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17958 break;
17959 }
17960 }
17961#endif
17962}
17963
17964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017965 * "system()" function
17966 */
17967 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017968f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017969 typval_T *argvars;
17970 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017972 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017974 char_u *infile = NULL;
17975 char_u buf[NUMBUFLEN];
17976 int err = FALSE;
17977 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017978
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017979 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017980 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017981
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017982 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017983 {
17984 /*
17985 * Write the string to a temp file, to be used for input of the shell
17986 * command.
17987 */
17988 if ((infile = vim_tempname('i')) == NULL)
17989 {
17990 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017991 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017992 }
17993
17994 fd = mch_fopen((char *)infile, WRITEBIN);
17995 if (fd == NULL)
17996 {
17997 EMSG2(_(e_notopen), infile);
17998 goto done;
17999 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018000 p = get_tv_string_buf_chk(&argvars[1], buf);
18001 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018002 {
18003 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018004 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018005 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018006 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18007 err = TRUE;
18008 if (fclose(fd) != 0)
18009 err = TRUE;
18010 if (err)
18011 {
18012 EMSG(_("E677: Error writing temp file"));
18013 goto done;
18014 }
18015 }
18016
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018017 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18018 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018019
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020#ifdef USE_CR
18021 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018022 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018023 {
18024 char_u *s;
18025
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018026 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018027 {
18028 if (*s == CAR)
18029 *s = NL;
18030 }
18031 }
18032#else
18033# ifdef USE_CRNL
18034 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018035 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 {
18037 char_u *s, *d;
18038
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018039 d = res;
18040 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 {
18042 if (s[0] == CAR && s[1] == NL)
18043 ++s;
18044 *d++ = *s;
18045 }
18046 *d = NUL;
18047 }
18048# endif
18049#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018050
18051done:
18052 if (infile != NULL)
18053 {
18054 mch_remove(infile);
18055 vim_free(infile);
18056 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018057 rettv->v_type = VAR_STRING;
18058 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059}
18060
18061/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018062 * "tabpagebuflist()" function
18063 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018064 static void
18065f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018066 typval_T *argvars UNUSED;
18067 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018068{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018069#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018070 tabpage_T *tp;
18071 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018072
18073 if (argvars[0].v_type == VAR_UNKNOWN)
18074 wp = firstwin;
18075 else
18076 {
18077 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18078 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018079 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018080 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018081 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018082 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018083 for (; wp != NULL; wp = wp->w_next)
18084 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018085 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018086 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018087 }
18088#endif
18089}
18090
18091
18092/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018093 * "tabpagenr()" function
18094 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018095 static void
18096f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018097 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018098 typval_T *rettv;
18099{
18100 int nr = 1;
18101#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018102 char_u *arg;
18103
18104 if (argvars[0].v_type != VAR_UNKNOWN)
18105 {
18106 arg = get_tv_string_chk(&argvars[0]);
18107 nr = 0;
18108 if (arg != NULL)
18109 {
18110 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018111 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018112 else
18113 EMSG2(_(e_invexpr2), arg);
18114 }
18115 }
18116 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018117 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018118#endif
18119 rettv->vval.v_number = nr;
18120}
18121
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018122
18123#ifdef FEAT_WINDOWS
18124static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18125
18126/*
18127 * Common code for tabpagewinnr() and winnr().
18128 */
18129 static int
18130get_winnr(tp, argvar)
18131 tabpage_T *tp;
18132 typval_T *argvar;
18133{
18134 win_T *twin;
18135 int nr = 1;
18136 win_T *wp;
18137 char_u *arg;
18138
18139 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18140 if (argvar->v_type != VAR_UNKNOWN)
18141 {
18142 arg = get_tv_string_chk(argvar);
18143 if (arg == NULL)
18144 nr = 0; /* type error; errmsg already given */
18145 else if (STRCMP(arg, "$") == 0)
18146 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18147 else if (STRCMP(arg, "#") == 0)
18148 {
18149 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18150 if (twin == NULL)
18151 nr = 0;
18152 }
18153 else
18154 {
18155 EMSG2(_(e_invexpr2), arg);
18156 nr = 0;
18157 }
18158 }
18159
18160 if (nr > 0)
18161 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18162 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018163 {
18164 if (wp == NULL)
18165 {
18166 /* didn't find it in this tabpage */
18167 nr = 0;
18168 break;
18169 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018170 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018171 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018172 return nr;
18173}
18174#endif
18175
18176/*
18177 * "tabpagewinnr()" function
18178 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018179 static void
18180f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018181 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018182 typval_T *rettv;
18183{
18184 int nr = 1;
18185#ifdef FEAT_WINDOWS
18186 tabpage_T *tp;
18187
18188 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18189 if (tp == NULL)
18190 nr = 0;
18191 else
18192 nr = get_winnr(tp, &argvars[1]);
18193#endif
18194 rettv->vval.v_number = nr;
18195}
18196
18197
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018198/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018199 * "tagfiles()" function
18200 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018201 static void
18202f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018203 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018204 typval_T *rettv;
18205{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018206 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018207 tagname_T tn;
18208 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018209
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018210 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018211 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018212 fname = alloc(MAXPATHL);
18213 if (fname == NULL)
18214 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018215
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018216 for (first = TRUE; ; first = FALSE)
18217 if (get_tagfname(&tn, first, fname) == FAIL
18218 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018219 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018220 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018221 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018222}
18223
18224/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018225 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018226 */
18227 static void
18228f_taglist(argvars, rettv)
18229 typval_T *argvars;
18230 typval_T *rettv;
18231{
18232 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018233
18234 tag_pattern = get_tv_string(&argvars[0]);
18235
18236 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018237 if (*tag_pattern == NUL)
18238 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018239
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018240 if (rettv_list_alloc(rettv) == OK)
18241 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018242}
18243
18244/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245 * "tempname()" function
18246 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018248f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018249 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018250 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018251{
18252 static int x = 'A';
18253
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018254 rettv->v_type = VAR_STRING;
18255 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256
18257 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18258 * names. Skip 'I' and 'O', they are used for shell redirection. */
18259 do
18260 {
18261 if (x == 'Z')
18262 x = '0';
18263 else if (x == '9')
18264 x = 'A';
18265 else
18266 {
18267#ifdef EBCDIC
18268 if (x == 'I')
18269 x = 'J';
18270 else if (x == 'R')
18271 x = 'S';
18272 else
18273#endif
18274 ++x;
18275 }
18276 } while (x == 'I' || x == 'O');
18277}
18278
18279/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018280 * "test(list)" function: Just checking the walls...
18281 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018282 static void
18283f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018284 typval_T *argvars UNUSED;
18285 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018286{
18287 /* Used for unit testing. Change the code below to your liking. */
18288#if 0
18289 listitem_T *li;
18290 list_T *l;
18291 char_u *bad, *good;
18292
18293 if (argvars[0].v_type != VAR_LIST)
18294 return;
18295 l = argvars[0].vval.v_list;
18296 if (l == NULL)
18297 return;
18298 li = l->lv_first;
18299 if (li == NULL)
18300 return;
18301 bad = get_tv_string(&li->li_tv);
18302 li = li->li_next;
18303 if (li == NULL)
18304 return;
18305 good = get_tv_string(&li->li_tv);
18306 rettv->vval.v_number = test_edit_score(bad, good);
18307#endif
18308}
18309
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018310#ifdef FEAT_FLOAT
18311/*
18312 * "tan()" function
18313 */
18314 static void
18315f_tan(argvars, rettv)
18316 typval_T *argvars;
18317 typval_T *rettv;
18318{
18319 float_T f;
18320
18321 rettv->v_type = VAR_FLOAT;
18322 if (get_float_arg(argvars, &f) == OK)
18323 rettv->vval.v_float = tan(f);
18324 else
18325 rettv->vval.v_float = 0.0;
18326}
18327
18328/*
18329 * "tanh()" function
18330 */
18331 static void
18332f_tanh(argvars, rettv)
18333 typval_T *argvars;
18334 typval_T *rettv;
18335{
18336 float_T f;
18337
18338 rettv->v_type = VAR_FLOAT;
18339 if (get_float_arg(argvars, &f) == OK)
18340 rettv->vval.v_float = tanh(f);
18341 else
18342 rettv->vval.v_float = 0.0;
18343}
18344#endif
18345
Bram Moolenaard52d9742005-08-21 22:20:28 +000018346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347 * "tolower(string)" function
18348 */
18349 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018350f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018351 typval_T *argvars;
18352 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018353{
18354 char_u *p;
18355
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018356 p = vim_strsave(get_tv_string(&argvars[0]));
18357 rettv->v_type = VAR_STRING;
18358 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359
18360 if (p != NULL)
18361 while (*p != NUL)
18362 {
18363#ifdef FEAT_MBYTE
18364 int l;
18365
18366 if (enc_utf8)
18367 {
18368 int c, lc;
18369
18370 c = utf_ptr2char(p);
18371 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018372 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 /* TODO: reallocate string when byte count changes. */
18374 if (utf_char2len(lc) == l)
18375 utf_char2bytes(lc, p);
18376 p += l;
18377 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018378 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018379 p += l; /* skip multi-byte character */
18380 else
18381#endif
18382 {
18383 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18384 ++p;
18385 }
18386 }
18387}
18388
18389/*
18390 * "toupper(string)" function
18391 */
18392 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018393f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018394 typval_T *argvars;
18395 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018397 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018398 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018399}
18400
18401/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018402 * "tr(string, fromstr, tostr)" function
18403 */
18404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018405f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018406 typval_T *argvars;
18407 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018408{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018409 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018410 char_u *fromstr;
18411 char_u *tostr;
18412 char_u *p;
18413#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018414 int inlen;
18415 int fromlen;
18416 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018417 int idx;
18418 char_u *cpstr;
18419 int cplen;
18420 int first = TRUE;
18421#endif
18422 char_u buf[NUMBUFLEN];
18423 char_u buf2[NUMBUFLEN];
18424 garray_T ga;
18425
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018426 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018427 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18428 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018429
18430 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018431 rettv->v_type = VAR_STRING;
18432 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018433 if (fromstr == NULL || tostr == NULL)
18434 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018435 ga_init2(&ga, (int)sizeof(char), 80);
18436
18437#ifdef FEAT_MBYTE
18438 if (!has_mbyte)
18439#endif
18440 /* not multi-byte: fromstr and tostr must be the same length */
18441 if (STRLEN(fromstr) != STRLEN(tostr))
18442 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018443#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018444error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018445#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018446 EMSG2(_(e_invarg2), fromstr);
18447 ga_clear(&ga);
18448 return;
18449 }
18450
18451 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018452 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018453 {
18454#ifdef FEAT_MBYTE
18455 if (has_mbyte)
18456 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018457 inlen = (*mb_ptr2len)(in_str);
18458 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018459 cplen = inlen;
18460 idx = 0;
18461 for (p = fromstr; *p != NUL; p += fromlen)
18462 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018463 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018464 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018465 {
18466 for (p = tostr; *p != NUL; p += tolen)
18467 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018468 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018469 if (idx-- == 0)
18470 {
18471 cplen = tolen;
18472 cpstr = p;
18473 break;
18474 }
18475 }
18476 if (*p == NUL) /* tostr is shorter than fromstr */
18477 goto error;
18478 break;
18479 }
18480 ++idx;
18481 }
18482
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018483 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018484 {
18485 /* Check that fromstr and tostr have the same number of
18486 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018487 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018488 first = FALSE;
18489 for (p = tostr; *p != NUL; p += tolen)
18490 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018491 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018492 --idx;
18493 }
18494 if (idx != 0)
18495 goto error;
18496 }
18497
18498 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018499 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018500 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018501
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018502 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018503 }
18504 else
18505#endif
18506 {
18507 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018508 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018509 if (p != NULL)
18510 ga_append(&ga, tostr[p - fromstr]);
18511 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018512 ga_append(&ga, *in_str);
18513 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018514 }
18515 }
18516
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018517 /* add a terminating NUL */
18518 ga_grow(&ga, 1);
18519 ga_append(&ga, NUL);
18520
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018521 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018522}
18523
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018524#ifdef FEAT_FLOAT
18525/*
18526 * "trunc({float})" function
18527 */
18528 static void
18529f_trunc(argvars, rettv)
18530 typval_T *argvars;
18531 typval_T *rettv;
18532{
18533 float_T f;
18534
18535 rettv->v_type = VAR_FLOAT;
18536 if (get_float_arg(argvars, &f) == OK)
18537 /* trunc() is not in C90, use floor() or ceil() instead. */
18538 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18539 else
18540 rettv->vval.v_float = 0.0;
18541}
18542#endif
18543
Bram Moolenaar8299df92004-07-10 09:47:34 +000018544/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018545 * "type(expr)" function
18546 */
18547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018548f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018549 typval_T *argvars;
18550 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018552 int n;
18553
18554 switch (argvars[0].v_type)
18555 {
18556 case VAR_NUMBER: n = 0; break;
18557 case VAR_STRING: n = 1; break;
18558 case VAR_FUNC: n = 2; break;
18559 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018560 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018561#ifdef FEAT_FLOAT
18562 case VAR_FLOAT: n = 5; break;
18563#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018564 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18565 }
18566 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018567}
18568
18569/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018570 * "undofile(name)" function
18571 */
18572 static void
18573f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018574 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018575 typval_T *rettv;
18576{
18577 rettv->v_type = VAR_STRING;
18578#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018579 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018580 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018581
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018582 if (*fname == NUL)
18583 {
18584 /* If there is no file name there will be no undo file. */
18585 rettv->vval.v_string = NULL;
18586 }
18587 else
18588 {
18589 char_u *ffname = FullName_save(fname, FALSE);
18590
18591 if (ffname != NULL)
18592 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18593 vim_free(ffname);
18594 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018595 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018596#else
18597 rettv->vval.v_string = NULL;
18598#endif
18599}
18600
18601/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018602 * "undotree()" function
18603 */
18604 static void
18605f_undotree(argvars, rettv)
18606 typval_T *argvars UNUSED;
18607 typval_T *rettv;
18608{
18609 if (rettv_dict_alloc(rettv) == OK)
18610 {
18611 dict_T *dict = rettv->vval.v_dict;
18612 list_T *list;
18613
Bram Moolenaar730cde92010-06-27 05:18:54 +020018614 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018615 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018616 dict_add_nr_str(dict, "save_last",
18617 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018618 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18619 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018620 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018621
18622 list = list_alloc();
18623 if (list != NULL)
18624 {
18625 u_eval_tree(curbuf->b_u_oldhead, list);
18626 dict_add_list(dict, "entries", list);
18627 }
18628 }
18629}
18630
18631/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018632 * "values(dict)" function
18633 */
18634 static void
18635f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018636 typval_T *argvars;
18637 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018638{
18639 dict_list(argvars, rettv, 1);
18640}
18641
18642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018643 * "virtcol(string)" function
18644 */
18645 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018646f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018647 typval_T *argvars;
18648 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018649{
18650 colnr_T vcol = 0;
18651 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018652 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018654 fp = var2fpos(&argvars[0], FALSE, &fnum);
18655 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18656 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657 {
18658 getvvcol(curwin, fp, NULL, NULL, &vcol);
18659 ++vcol;
18660 }
18661
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018662 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018663}
18664
18665/*
18666 * "visualmode()" function
18667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018669f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018670 typval_T *argvars UNUSED;
18671 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018672{
18673#ifdef FEAT_VISUAL
18674 char_u str[2];
18675
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018676 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018677 str[0] = curbuf->b_visual_mode_eval;
18678 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018679 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018680
18681 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018682 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018683 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684#endif
18685}
18686
18687/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018688 * "wildmenumode()" function
18689 */
18690 static void
18691f_wildmenumode(argvars, rettv)
18692 typval_T *argvars UNUSED;
18693 typval_T *rettv UNUSED;
18694{
18695#ifdef FEAT_WILDMENU
18696 if (wild_menu_showing)
18697 rettv->vval.v_number = 1;
18698#endif
18699}
18700
18701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702 * "winbufnr(nr)" function
18703 */
18704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018705f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018706 typval_T *argvars;
18707 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708{
18709 win_T *wp;
18710
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018711 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018713 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018715 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716}
18717
18718/*
18719 * "wincol()" function
18720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018722f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018723 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725{
18726 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018727 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728}
18729
18730/*
18731 * "winheight(nr)" function
18732 */
18733 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018734f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018735 typval_T *argvars;
18736 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737{
18738 win_T *wp;
18739
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018740 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018742 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018744 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745}
18746
18747/*
18748 * "winline()" function
18749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018751f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018752 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018753 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754{
18755 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018756 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757}
18758
18759/*
18760 * "winnr()" function
18761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018763f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018764 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766{
18767 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018768
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018770 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018772 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773}
18774
18775/*
18776 * "winrestcmd()" function
18777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018779f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018780 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018782{
18783#ifdef FEAT_WINDOWS
18784 win_T *wp;
18785 int winnr = 1;
18786 garray_T ga;
18787 char_u buf[50];
18788
18789 ga_init2(&ga, (int)sizeof(char), 70);
18790 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18791 {
18792 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18793 ga_concat(&ga, buf);
18794# ifdef FEAT_VERTSPLIT
18795 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18796 ga_concat(&ga, buf);
18797# endif
18798 ++winnr;
18799 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018800 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018802 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018804 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018806 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807}
18808
18809/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018810 * "winrestview()" function
18811 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018812 static void
18813f_winrestview(argvars, rettv)
18814 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018815 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018816{
18817 dict_T *dict;
18818
18819 if (argvars[0].v_type != VAR_DICT
18820 || (dict = argvars[0].vval.v_dict) == NULL)
18821 EMSG(_(e_invarg));
18822 else
18823 {
18824 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18825 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18826#ifdef FEAT_VIRTUALEDIT
18827 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18828#endif
18829 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018830 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018831
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018832 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018833#ifdef FEAT_DIFF
18834 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18835#endif
18836 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18837 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18838
18839 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018840 win_new_height(curwin, curwin->w_height);
18841# ifdef FEAT_VERTSPLIT
18842 win_new_width(curwin, W_WIDTH(curwin));
18843# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018844 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018845
18846 if (curwin->w_topline == 0)
18847 curwin->w_topline = 1;
18848 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18849 curwin->w_topline = curbuf->b_ml.ml_line_count;
18850#ifdef FEAT_DIFF
18851 check_topfill(curwin, TRUE);
18852#endif
18853 }
18854}
18855
18856/*
18857 * "winsaveview()" function
18858 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018859 static void
18860f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018861 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018862 typval_T *rettv;
18863{
18864 dict_T *dict;
18865
Bram Moolenaara800b422010-06-27 01:15:55 +020018866 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018867 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018868 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018869
18870 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18871 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18872#ifdef FEAT_VIRTUALEDIT
18873 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18874#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018875 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018876 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18877
18878 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18879#ifdef FEAT_DIFF
18880 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18881#endif
18882 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18883 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18884}
18885
18886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018887 * "winwidth(nr)" function
18888 */
18889 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018890f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018891 typval_T *argvars;
18892 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018893{
18894 win_T *wp;
18895
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018896 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018898 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899 else
18900#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018901 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018903 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018904#endif
18905}
18906
Bram Moolenaar071d4272004-06-13 20:20:40 +000018907/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018908 * "writefile()" function
18909 */
18910 static void
18911f_writefile(argvars, rettv)
18912 typval_T *argvars;
18913 typval_T *rettv;
18914{
18915 int binary = FALSE;
18916 char_u *fname;
18917 FILE *fd;
18918 listitem_T *li;
18919 char_u *s;
18920 int ret = 0;
18921 int c;
18922
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018923 if (check_restricted() || check_secure())
18924 return;
18925
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018926 if (argvars[0].v_type != VAR_LIST)
18927 {
18928 EMSG2(_(e_listarg), "writefile()");
18929 return;
18930 }
18931 if (argvars[0].vval.v_list == NULL)
18932 return;
18933
18934 if (argvars[2].v_type != VAR_UNKNOWN
18935 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18936 binary = TRUE;
18937
18938 /* Always open the file in binary mode, library functions have a mind of
18939 * their own about CR-LF conversion. */
18940 fname = get_tv_string(&argvars[1]);
18941 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18942 {
18943 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18944 ret = -1;
18945 }
18946 else
18947 {
18948 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18949 li = li->li_next)
18950 {
18951 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18952 {
18953 if (*s == '\n')
18954 c = putc(NUL, fd);
18955 else
18956 c = putc(*s, fd);
18957 if (c == EOF)
18958 {
18959 ret = -1;
18960 break;
18961 }
18962 }
18963 if (!binary || li->li_next != NULL)
18964 if (putc('\n', fd) == EOF)
18965 {
18966 ret = -1;
18967 break;
18968 }
18969 if (ret < 0)
18970 {
18971 EMSG(_(e_write));
18972 break;
18973 }
18974 }
18975 fclose(fd);
18976 }
18977
18978 rettv->vval.v_number = ret;
18979}
18980
18981/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010018982 * "xor(expr, expr)" function
18983 */
18984 static void
18985f_xor(argvars, rettv)
18986 typval_T *argvars;
18987 typval_T *rettv;
18988{
18989 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
18990 ^ get_tv_number_chk(&argvars[1], NULL);
18991}
18992
18993
18994/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018995 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018996 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018997 */
18998 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000018999var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019000 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019001 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019002 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019004 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019005 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019006 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007
Bram Moolenaara5525202006-03-02 22:52:09 +000019008 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019009 if (varp->v_type == VAR_LIST)
19010 {
19011 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019012 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019013 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019014 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019015
19016 l = varp->vval.v_list;
19017 if (l == NULL)
19018 return NULL;
19019
19020 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019021 pos.lnum = list_find_nr(l, 0L, &error);
19022 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019023 return NULL; /* invalid line number */
19024
19025 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019026 pos.col = list_find_nr(l, 1L, &error);
19027 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019028 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019029 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019030
19031 /* We accept "$" for the column number: last column. */
19032 li = list_find(l, 1L);
19033 if (li != NULL && li->li_tv.v_type == VAR_STRING
19034 && li->li_tv.vval.v_string != NULL
19035 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19036 pos.col = len + 1;
19037
Bram Moolenaara5525202006-03-02 22:52:09 +000019038 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019039 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019040 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019041 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019042
Bram Moolenaara5525202006-03-02 22:52:09 +000019043#ifdef FEAT_VIRTUALEDIT
19044 /* Get the virtual offset. Defaults to zero. */
19045 pos.coladd = list_find_nr(l, 2L, &error);
19046 if (error)
19047 pos.coladd = 0;
19048#endif
19049
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019050 return &pos;
19051 }
19052
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019053 name = get_tv_string_chk(varp);
19054 if (name == NULL)
19055 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019056 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019058#ifdef FEAT_VISUAL
19059 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19060 {
19061 if (VIsual_active)
19062 return &VIsual;
19063 return &curwin->w_cursor;
19064 }
19065#endif
19066 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019068 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019069 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19070 return NULL;
19071 return pp;
19072 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019073
19074#ifdef FEAT_VIRTUALEDIT
19075 pos.coladd = 0;
19076#endif
19077
Bram Moolenaar477933c2007-07-17 14:32:23 +000019078 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019079 {
19080 pos.col = 0;
19081 if (name[1] == '0') /* "w0": first visible line */
19082 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019083 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019084 pos.lnum = curwin->w_topline;
19085 return &pos;
19086 }
19087 else if (name[1] == '$') /* "w$": last visible line */
19088 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019089 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019090 pos.lnum = curwin->w_botline - 1;
19091 return &pos;
19092 }
19093 }
19094 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019096 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097 {
19098 pos.lnum = curbuf->b_ml.ml_line_count;
19099 pos.col = 0;
19100 }
19101 else
19102 {
19103 pos.lnum = curwin->w_cursor.lnum;
19104 pos.col = (colnr_T)STRLEN(ml_get_curline());
19105 }
19106 return &pos;
19107 }
19108 return NULL;
19109}
19110
19111/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019112 * Convert list in "arg" into a position and optional file number.
19113 * When "fnump" is NULL there is no file number, only 3 items.
19114 * Note that the column is passed on as-is, the caller may want to decrement
19115 * it to use 1 for the first column.
19116 * Return FAIL when conversion is not possible, doesn't check the position for
19117 * validity.
19118 */
19119 static int
19120list2fpos(arg, posp, fnump)
19121 typval_T *arg;
19122 pos_T *posp;
19123 int *fnump;
19124{
19125 list_T *l = arg->vval.v_list;
19126 long i = 0;
19127 long n;
19128
Bram Moolenaarbde35262006-07-23 20:12:24 +000019129 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19130 * when "fnump" isn't NULL and "coladd" is optional. */
19131 if (arg->v_type != VAR_LIST
19132 || l == NULL
19133 || l->lv_len < (fnump == NULL ? 2 : 3)
19134 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019135 return FAIL;
19136
19137 if (fnump != NULL)
19138 {
19139 n = list_find_nr(l, i++, NULL); /* fnum */
19140 if (n < 0)
19141 return FAIL;
19142 if (n == 0)
19143 n = curbuf->b_fnum; /* current buffer */
19144 *fnump = n;
19145 }
19146
19147 n = list_find_nr(l, i++, NULL); /* lnum */
19148 if (n < 0)
19149 return FAIL;
19150 posp->lnum = n;
19151
19152 n = list_find_nr(l, i++, NULL); /* col */
19153 if (n < 0)
19154 return FAIL;
19155 posp->col = n;
19156
19157#ifdef FEAT_VIRTUALEDIT
19158 n = list_find_nr(l, i, NULL);
19159 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019160 posp->coladd = 0;
19161 else
19162 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019163#endif
19164
19165 return OK;
19166}
19167
19168/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169 * Get the length of an environment variable name.
19170 * Advance "arg" to the first character after the name.
19171 * Return 0 for error.
19172 */
19173 static int
19174get_env_len(arg)
19175 char_u **arg;
19176{
19177 char_u *p;
19178 int len;
19179
19180 for (p = *arg; vim_isIDc(*p); ++p)
19181 ;
19182 if (p == *arg) /* no name found */
19183 return 0;
19184
19185 len = (int)(p - *arg);
19186 *arg = p;
19187 return len;
19188}
19189
19190/*
19191 * Get the length of the name of a function or internal variable.
19192 * "arg" is advanced to the first non-white character after the name.
19193 * Return 0 if something is wrong.
19194 */
19195 static int
19196get_id_len(arg)
19197 char_u **arg;
19198{
19199 char_u *p;
19200 int len;
19201
19202 /* Find the end of the name. */
19203 for (p = *arg; eval_isnamec(*p); ++p)
19204 ;
19205 if (p == *arg) /* no name found */
19206 return 0;
19207
19208 len = (int)(p - *arg);
19209 *arg = skipwhite(p);
19210
19211 return len;
19212}
19213
19214/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019215 * Get the length of the name of a variable or function.
19216 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019217 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019218 * Return -1 if curly braces expansion failed.
19219 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019220 * If the name contains 'magic' {}'s, expand them and return the
19221 * expanded name in an allocated string via 'alias' - caller must free.
19222 */
19223 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019224get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019225 char_u **arg;
19226 char_u **alias;
19227 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019228 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019229{
19230 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019231 char_u *p;
19232 char_u *expr_start;
19233 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234
19235 *alias = NULL; /* default to no alias */
19236
19237 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19238 && (*arg)[2] == (int)KE_SNR)
19239 {
19240 /* hard coded <SNR>, already translated */
19241 *arg += 3;
19242 return get_id_len(arg) + 3;
19243 }
19244 len = eval_fname_script(*arg);
19245 if (len > 0)
19246 {
19247 /* literal "<SID>", "s:" or "<SNR>" */
19248 *arg += len;
19249 }
19250
Bram Moolenaar071d4272004-06-13 20:20:40 +000019251 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019252 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019254 p = find_name_end(*arg, &expr_start, &expr_end,
19255 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256 if (expr_start != NULL)
19257 {
19258 char_u *temp_string;
19259
19260 if (!evaluate)
19261 {
19262 len += (int)(p - *arg);
19263 *arg = skipwhite(p);
19264 return len;
19265 }
19266
19267 /*
19268 * Include any <SID> etc in the expanded string:
19269 * Thus the -len here.
19270 */
19271 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19272 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019273 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274 *alias = temp_string;
19275 *arg = skipwhite(p);
19276 return (int)STRLEN(temp_string);
19277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278
19279 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019280 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281 EMSG2(_(e_invexpr2), *arg);
19282
19283 return len;
19284}
19285
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019286/*
19287 * Find the end of a variable or function name, taking care of magic braces.
19288 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19289 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019290 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019291 * Return a pointer to just after the name. Equal to "arg" if there is no
19292 * valid name.
19293 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019294 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019295find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 char_u *arg;
19297 char_u **expr_start;
19298 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019299 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019301 int mb_nest = 0;
19302 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019303 char_u *p;
19304
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019305 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019307 *expr_start = NULL;
19308 *expr_end = NULL;
19309 }
19310
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019311 /* Quick check for valid starting character. */
19312 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19313 return arg;
19314
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019315 for (p = arg; *p != NUL
19316 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019317 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019318 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019319 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019320 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019321 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019322 if (*p == '\'')
19323 {
19324 /* skip over 'string' to avoid counting [ and ] inside it. */
19325 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19326 ;
19327 if (*p == NUL)
19328 break;
19329 }
19330 else if (*p == '"')
19331 {
19332 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19333 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19334 if (*p == '\\' && p[1] != NUL)
19335 ++p;
19336 if (*p == NUL)
19337 break;
19338 }
19339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019340 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019342 if (*p == '[')
19343 ++br_nest;
19344 else if (*p == ']')
19345 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019346 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019347
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019348 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019350 if (*p == '{')
19351 {
19352 mb_nest++;
19353 if (expr_start != NULL && *expr_start == NULL)
19354 *expr_start = p;
19355 }
19356 else if (*p == '}')
19357 {
19358 mb_nest--;
19359 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19360 *expr_end = p;
19361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019363 }
19364
19365 return p;
19366}
19367
19368/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019369 * Expands out the 'magic' {}'s in a variable/function name.
19370 * Note that this can call itself recursively, to deal with
19371 * constructs like foo{bar}{baz}{bam}
19372 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19373 * "in_start" ^
19374 * "expr_start" ^
19375 * "expr_end" ^
19376 * "in_end" ^
19377 *
19378 * Returns a new allocated string, which the caller must free.
19379 * Returns NULL for failure.
19380 */
19381 static char_u *
19382make_expanded_name(in_start, expr_start, expr_end, in_end)
19383 char_u *in_start;
19384 char_u *expr_start;
19385 char_u *expr_end;
19386 char_u *in_end;
19387{
19388 char_u c1;
19389 char_u *retval = NULL;
19390 char_u *temp_result;
19391 char_u *nextcmd = NULL;
19392
19393 if (expr_end == NULL || in_end == NULL)
19394 return NULL;
19395 *expr_start = NUL;
19396 *expr_end = NUL;
19397 c1 = *in_end;
19398 *in_end = NUL;
19399
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019400 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019401 if (temp_result != NULL && nextcmd == NULL)
19402 {
19403 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19404 + (in_end - expr_end) + 1));
19405 if (retval != NULL)
19406 {
19407 STRCPY(retval, in_start);
19408 STRCAT(retval, temp_result);
19409 STRCAT(retval, expr_end + 1);
19410 }
19411 }
19412 vim_free(temp_result);
19413
19414 *in_end = c1; /* put char back for error messages */
19415 *expr_start = '{';
19416 *expr_end = '}';
19417
19418 if (retval != NULL)
19419 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019420 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019421 if (expr_start != NULL)
19422 {
19423 /* Further expansion! */
19424 temp_result = make_expanded_name(retval, expr_start,
19425 expr_end, temp_result);
19426 vim_free(retval);
19427 retval = temp_result;
19428 }
19429 }
19430
19431 return retval;
19432}
19433
19434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019435 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019436 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019437 */
19438 static int
19439eval_isnamec(c)
19440 int c;
19441{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019442 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19443}
19444
19445/*
19446 * Return TRUE if character "c" can be used as the first character in a
19447 * variable or function name (excluding '{' and '}').
19448 */
19449 static int
19450eval_isnamec1(c)
19451 int c;
19452{
19453 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019454}
19455
19456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457 * Set number v: variable to "val".
19458 */
19459 void
19460set_vim_var_nr(idx, val)
19461 int idx;
19462 long val;
19463{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019464 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019465}
19466
19467/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019468 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019469 */
19470 long
19471get_vim_var_nr(idx)
19472 int idx;
19473{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019474 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475}
19476
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019477/*
19478 * Get string v: variable value. Uses a static buffer, can only be used once.
19479 */
19480 char_u *
19481get_vim_var_str(idx)
19482 int idx;
19483{
19484 return get_tv_string(&vimvars[idx].vv_tv);
19485}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019486
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019488 * Get List v: variable value. Caller must take care of reference count when
19489 * needed.
19490 */
19491 list_T *
19492get_vim_var_list(idx)
19493 int idx;
19494{
19495 return vimvars[idx].vv_list;
19496}
19497
19498/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019499 * Set v:char to character "c".
19500 */
19501 void
19502set_vim_var_char(c)
19503 int c;
19504{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019505 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019506
19507#ifdef FEAT_MBYTE
19508 if (has_mbyte)
19509 buf[(*mb_char2bytes)(c, buf)] = NUL;
19510 else
19511#endif
19512 {
19513 buf[0] = c;
19514 buf[1] = NUL;
19515 }
19516 set_vim_var_string(VV_CHAR, buf, -1);
19517}
19518
19519/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019520 * Set v:count to "count" and v:count1 to "count1".
19521 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522 */
19523 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019524set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525 long count;
19526 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019527 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019529 if (set_prevcount)
19530 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019531 vimvars[VV_COUNT].vv_nr = count;
19532 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019533}
19534
19535/*
19536 * Set string v: variable to a copy of "val".
19537 */
19538 void
19539set_vim_var_string(idx, val, len)
19540 int idx;
19541 char_u *val;
19542 int len; /* length of "val" to use or -1 (whole string) */
19543{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019544 /* Need to do this (at least) once, since we can't initialize a union.
19545 * Will always be invoked when "v:progname" is set. */
19546 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19547
Bram Moolenaare9a41262005-01-15 22:18:47 +000019548 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019549 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019550 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019552 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019554 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019555}
19556
19557/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019558 * Set List v: variable to "val".
19559 */
19560 void
19561set_vim_var_list(idx, val)
19562 int idx;
19563 list_T *val;
19564{
19565 list_unref(vimvars[idx].vv_list);
19566 vimvars[idx].vv_list = val;
19567 if (val != NULL)
19568 ++val->lv_refcount;
19569}
19570
19571/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572 * Set v:register if needed.
19573 */
19574 void
19575set_reg_var(c)
19576 int c;
19577{
19578 char_u regname;
19579
19580 if (c == 0 || c == ' ')
19581 regname = '"';
19582 else
19583 regname = c;
19584 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019585 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586 set_vim_var_string(VV_REG, &regname, 1);
19587}
19588
19589/*
19590 * Get or set v:exception. If "oldval" == NULL, return the current value.
19591 * Otherwise, restore the value to "oldval" and return NULL.
19592 * Must always be called in pairs to save and restore v:exception! Does not
19593 * take care of memory allocations.
19594 */
19595 char_u *
19596v_exception(oldval)
19597 char_u *oldval;
19598{
19599 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019600 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601
Bram Moolenaare9a41262005-01-15 22:18:47 +000019602 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603 return NULL;
19604}
19605
19606/*
19607 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19608 * Otherwise, restore the value to "oldval" and return NULL.
19609 * Must always be called in pairs to save and restore v:throwpoint! Does not
19610 * take care of memory allocations.
19611 */
19612 char_u *
19613v_throwpoint(oldval)
19614 char_u *oldval;
19615{
19616 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019617 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618
Bram Moolenaare9a41262005-01-15 22:18:47 +000019619 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620 return NULL;
19621}
19622
19623#if defined(FEAT_AUTOCMD) || defined(PROTO)
19624/*
19625 * Set v:cmdarg.
19626 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19627 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19628 * Must always be called in pairs!
19629 */
19630 char_u *
19631set_cmdarg(eap, oldarg)
19632 exarg_T *eap;
19633 char_u *oldarg;
19634{
19635 char_u *oldval;
19636 char_u *newval;
19637 unsigned len;
19638
Bram Moolenaare9a41262005-01-15 22:18:47 +000019639 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019640 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019641 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019642 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019643 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019644 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019645 }
19646
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019647 if (eap->force_bin == FORCE_BIN)
19648 len = 6;
19649 else if (eap->force_bin == FORCE_NOBIN)
19650 len = 8;
19651 else
19652 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019653
19654 if (eap->read_edit)
19655 len += 7;
19656
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019657 if (eap->force_ff != 0)
19658 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19659# ifdef FEAT_MBYTE
19660 if (eap->force_enc != 0)
19661 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019662 if (eap->bad_char != 0)
19663 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019664# endif
19665
19666 newval = alloc(len + 1);
19667 if (newval == NULL)
19668 return NULL;
19669
19670 if (eap->force_bin == FORCE_BIN)
19671 sprintf((char *)newval, " ++bin");
19672 else if (eap->force_bin == FORCE_NOBIN)
19673 sprintf((char *)newval, " ++nobin");
19674 else
19675 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019676
19677 if (eap->read_edit)
19678 STRCAT(newval, " ++edit");
19679
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019680 if (eap->force_ff != 0)
19681 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19682 eap->cmd + eap->force_ff);
19683# ifdef FEAT_MBYTE
19684 if (eap->force_enc != 0)
19685 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19686 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019687 if (eap->bad_char == BAD_KEEP)
19688 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19689 else if (eap->bad_char == BAD_DROP)
19690 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19691 else if (eap->bad_char != 0)
19692 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019693# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019694 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019695 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696}
19697#endif
19698
19699/*
19700 * Get the value of internal variable "name".
19701 * Return OK or FAIL.
19702 */
19703 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019704get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019705 char_u *name;
19706 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019707 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019708 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709{
19710 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019711 typval_T *tv = NULL;
19712 typval_T atv;
19713 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019715
19716 /* truncate the name, so that we can use strcmp() */
19717 cc = name[len];
19718 name[len] = NUL;
19719
19720 /*
19721 * Check for "b:changedtick".
19722 */
19723 if (STRCMP(name, "b:changedtick") == 0)
19724 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019725 atv.v_type = VAR_NUMBER;
19726 atv.vval.v_number = curbuf->b_changedtick;
19727 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728 }
19729
19730 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731 * Check for user-defined variables.
19732 */
19733 else
19734 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019735 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019736 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019737 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019738 }
19739
Bram Moolenaare9a41262005-01-15 22:18:47 +000019740 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019742 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019743 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744 ret = FAIL;
19745 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019746 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019747 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019748
19749 name[len] = cc;
19750
19751 return ret;
19752}
19753
19754/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019755 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19756 * Also handle function call with Funcref variable: func(expr)
19757 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19758 */
19759 static int
19760handle_subscript(arg, rettv, evaluate, verbose)
19761 char_u **arg;
19762 typval_T *rettv;
19763 int evaluate; /* do more than finding the end */
19764 int verbose; /* give error messages */
19765{
19766 int ret = OK;
19767 dict_T *selfdict = NULL;
19768 char_u *s;
19769 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019770 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019771
19772 while (ret == OK
19773 && (**arg == '['
19774 || (**arg == '.' && rettv->v_type == VAR_DICT)
19775 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19776 && !vim_iswhite(*(*arg - 1)))
19777 {
19778 if (**arg == '(')
19779 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019780 /* need to copy the funcref so that we can clear rettv */
19781 functv = *rettv;
19782 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019783
19784 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019785 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019786 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019787 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19788 &len, evaluate, selfdict);
19789
19790 /* Clear the funcref afterwards, so that deleting it while
19791 * evaluating the arguments is possible (see test55). */
19792 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019793
19794 /* Stop the expression evaluation when immediately aborting on
19795 * error, or when an interrupt occurred or an exception was thrown
19796 * but not caught. */
19797 if (aborting())
19798 {
19799 if (ret == OK)
19800 clear_tv(rettv);
19801 ret = FAIL;
19802 }
19803 dict_unref(selfdict);
19804 selfdict = NULL;
19805 }
19806 else /* **arg == '[' || **arg == '.' */
19807 {
19808 dict_unref(selfdict);
19809 if (rettv->v_type == VAR_DICT)
19810 {
19811 selfdict = rettv->vval.v_dict;
19812 if (selfdict != NULL)
19813 ++selfdict->dv_refcount;
19814 }
19815 else
19816 selfdict = NULL;
19817 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19818 {
19819 clear_tv(rettv);
19820 ret = FAIL;
19821 }
19822 }
19823 }
19824 dict_unref(selfdict);
19825 return ret;
19826}
19827
19828/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019829 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019830 * value).
19831 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019832 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019833alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019834{
Bram Moolenaar33570922005-01-25 22:26:29 +000019835 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019836}
19837
19838/*
19839 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019840 * The string "s" must have been allocated, it is consumed.
19841 * Return NULL for out of memory, the variable otherwise.
19842 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019843 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019844alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845 char_u *s;
19846{
Bram Moolenaar33570922005-01-25 22:26:29 +000019847 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019848
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019849 rettv = alloc_tv();
19850 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019852 rettv->v_type = VAR_STRING;
19853 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 }
19855 else
19856 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019857 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858}
19859
19860/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019861 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019862 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019863 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019864free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019865 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866{
19867 if (varp != NULL)
19868 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019869 switch (varp->v_type)
19870 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019871 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019872 func_unref(varp->vval.v_string);
19873 /*FALLTHROUGH*/
19874 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019875 vim_free(varp->vval.v_string);
19876 break;
19877 case VAR_LIST:
19878 list_unref(varp->vval.v_list);
19879 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019880 case VAR_DICT:
19881 dict_unref(varp->vval.v_dict);
19882 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019883 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019884#ifdef FEAT_FLOAT
19885 case VAR_FLOAT:
19886#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019887 case VAR_UNKNOWN:
19888 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019889 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019890 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019891 break;
19892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893 vim_free(varp);
19894 }
19895}
19896
19897/*
19898 * Free the memory for a variable value and set the value to NULL or 0.
19899 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019900 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019901clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019902 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903{
19904 if (varp != NULL)
19905 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019906 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019908 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019909 func_unref(varp->vval.v_string);
19910 /*FALLTHROUGH*/
19911 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019912 vim_free(varp->vval.v_string);
19913 varp->vval.v_string = NULL;
19914 break;
19915 case VAR_LIST:
19916 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019917 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019918 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019919 case VAR_DICT:
19920 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019921 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019922 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019923 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019924 varp->vval.v_number = 0;
19925 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019926#ifdef FEAT_FLOAT
19927 case VAR_FLOAT:
19928 varp->vval.v_float = 0.0;
19929 break;
19930#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019931 case VAR_UNKNOWN:
19932 break;
19933 default:
19934 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019936 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937 }
19938}
19939
19940/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019941 * Set the value of a variable to NULL without freeing items.
19942 */
19943 static void
19944init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019945 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019946{
19947 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019948 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019949}
19950
19951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952 * Get the number value of a variable.
19953 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019954 * For incompatible types, return 0.
19955 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19956 * caller of incompatible types: it sets *denote to TRUE if "denote"
19957 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019958 */
19959 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019960get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019961 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019963 int error = FALSE;
19964
19965 return get_tv_number_chk(varp, &error); /* return 0L on error */
19966}
19967
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019968 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019969get_tv_number_chk(varp, denote)
19970 typval_T *varp;
19971 int *denote;
19972{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019973 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019975 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019977 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019978 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019979#ifdef FEAT_FLOAT
19980 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019981 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019982 break;
19983#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019984 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019985 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019986 break;
19987 case VAR_STRING:
19988 if (varp->vval.v_string != NULL)
19989 vim_str2nr(varp->vval.v_string, NULL, NULL,
19990 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019991 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019992 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019993 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000019994 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019995 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019996 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019997 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019998 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019999 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020000 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020001 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020002 if (denote == NULL) /* useful for values that must be unsigned */
20003 n = -1;
20004 else
20005 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020006 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007}
20008
20009/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020010 * Get the lnum from the first argument.
20011 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020012 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020013 */
20014 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020015get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020016 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020017{
Bram Moolenaar33570922005-01-25 22:26:29 +000020018 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 linenr_T lnum;
20020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020021 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020022 if (lnum == 0) /* no valid number, try using line() */
20023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020024 rettv.v_type = VAR_NUMBER;
20025 f_line(argvars, &rettv);
20026 lnum = rettv.vval.v_number;
20027 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020028 }
20029 return lnum;
20030}
20031
20032/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020033 * Get the lnum from the first argument.
20034 * Also accepts "$", then "buf" is used.
20035 * Returns 0 on error.
20036 */
20037 static linenr_T
20038get_tv_lnum_buf(argvars, buf)
20039 typval_T *argvars;
20040 buf_T *buf;
20041{
20042 if (argvars[0].v_type == VAR_STRING
20043 && argvars[0].vval.v_string != NULL
20044 && argvars[0].vval.v_string[0] == '$'
20045 && buf != NULL)
20046 return buf->b_ml.ml_line_count;
20047 return get_tv_number_chk(&argvars[0], NULL);
20048}
20049
20050/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051 * Get the string value of a variable.
20052 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020053 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20054 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 * If the String variable has never been set, return an empty string.
20056 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020057 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20058 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020059 */
20060 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020061get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020062 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063{
20064 static char_u mybuf[NUMBUFLEN];
20065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020066 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020067}
20068
20069 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020070get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020071 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020072 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020074 char_u *res = get_tv_string_buf_chk(varp, buf);
20075
20076 return res != NULL ? res : (char_u *)"";
20077}
20078
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020079 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020080get_tv_string_chk(varp)
20081 typval_T *varp;
20082{
20083 static char_u mybuf[NUMBUFLEN];
20084
20085 return get_tv_string_buf_chk(varp, mybuf);
20086}
20087
20088 static char_u *
20089get_tv_string_buf_chk(varp, buf)
20090 typval_T *varp;
20091 char_u *buf;
20092{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020093 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020095 case VAR_NUMBER:
20096 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20097 return buf;
20098 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020099 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020100 break;
20101 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020102 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020103 break;
20104 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020105 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020106 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020107#ifdef FEAT_FLOAT
20108 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020109 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020110 break;
20111#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020112 case VAR_STRING:
20113 if (varp->vval.v_string != NULL)
20114 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020115 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020116 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020117 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020118 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020119 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020120 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121}
20122
20123/*
20124 * Find variable "name" in the list of variables.
20125 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020126 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020127 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020128 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020130 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020131find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020132 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020133 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020135 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020136 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137
Bram Moolenaara7043832005-01-21 11:56:39 +000020138 ht = find_var_ht(name, &varname);
20139 if (htp != NULL)
20140 *htp = ht;
20141 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020142 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020143 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144}
20145
20146/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020147 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020148 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020149 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020150 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020151find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020152 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020153 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020154 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020155 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020156{
Bram Moolenaar33570922005-01-25 22:26:29 +000020157 hashitem_T *hi;
20158
20159 if (*varname == NUL)
20160 {
20161 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020162 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020163 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020164 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020165 case 'g': return &globvars_var;
20166 case 'v': return &vimvars_var;
20167 case 'b': return &curbuf->b_bufvar;
20168 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020169#ifdef FEAT_WINDOWS
20170 case 't': return &curtab->tp_winvar;
20171#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020172 case 'l': return current_funccal == NULL
20173 ? NULL : &current_funccal->l_vars_var;
20174 case 'a': return current_funccal == NULL
20175 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020176 }
20177 return NULL;
20178 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020179
20180 hi = hash_find(ht, varname);
20181 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020182 {
20183 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020184 * worked find the variable again. Don't auto-load a script if it was
20185 * loaded already, otherwise it would be loaded every time when
20186 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020187 if (ht == &globvarht && !writing)
20188 {
20189 /* Note: script_autoload() may make "hi" invalid. It must either
20190 * be obtained again or not used. */
20191 if (!script_autoload(varname, FALSE) || aborting())
20192 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020193 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020194 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020195 if (HASHITEM_EMPTY(hi))
20196 return NULL;
20197 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020198 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020199}
20200
20201/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020202 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020203 * Set "varname" to the start of name without ':'.
20204 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020205 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020206find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020207 char_u *name;
20208 char_u **varname;
20209{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020210 hashitem_T *hi;
20211
Bram Moolenaar071d4272004-06-13 20:20:40 +000020212 if (name[1] != ':')
20213 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020214 /* The name must not start with a colon or #. */
20215 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020216 return NULL;
20217 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020218
20219 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020220 hi = hash_find(&compat_hashtab, name);
20221 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020222 return &compat_hashtab;
20223
Bram Moolenaar071d4272004-06-13 20:20:40 +000020224 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020225 return &globvarht; /* global variable */
20226 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020227 }
20228 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020229 if (*name == 'g') /* global variable */
20230 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020231 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20232 */
20233 if (vim_strchr(name + 2, ':') != NULL
20234 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020235 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020236 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020237 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020238 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020239 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020240#ifdef FEAT_WINDOWS
20241 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020242 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020243#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020244 if (*name == 'v') /* v: variable */
20245 return &vimvarht;
20246 if (*name == 'a' && current_funccal != NULL) /* function argument */
20247 return &current_funccal->l_avars.dv_hashtab;
20248 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20249 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020250 if (*name == 's' /* script variable */
20251 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20252 return &SCRIPT_VARS(current_SID);
20253 return NULL;
20254}
20255
20256/*
20257 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020258 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259 * Returns NULL when it doesn't exist.
20260 */
20261 char_u *
20262get_var_value(name)
20263 char_u *name;
20264{
Bram Moolenaar33570922005-01-25 22:26:29 +000020265 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020266
Bram Moolenaara7043832005-01-21 11:56:39 +000020267 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268 if (v == NULL)
20269 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020270 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271}
20272
20273/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020274 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020275 * sourcing this script and when executing functions defined in the script.
20276 */
20277 void
20278new_script_vars(id)
20279 scid_T id;
20280{
Bram Moolenaara7043832005-01-21 11:56:39 +000020281 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020282 hashtab_T *ht;
20283 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020284
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20286 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020287 /* Re-allocating ga_data means that an ht_array pointing to
20288 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020289 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020290 for (i = 1; i <= ga_scripts.ga_len; ++i)
20291 {
20292 ht = &SCRIPT_VARS(i);
20293 if (ht->ht_mask == HT_INIT_SIZE - 1)
20294 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020295 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020296 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020297 }
20298
Bram Moolenaar071d4272004-06-13 20:20:40 +000020299 while (ga_scripts.ga_len < id)
20300 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020301 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020302 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020303 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305 }
20306 }
20307}
20308
20309/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020310 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20311 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 */
20313 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020314init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020315 dict_T *dict;
20316 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020317 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020318{
Bram Moolenaar33570922005-01-25 22:26:29 +000020319 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020320 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020321 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020322 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020323 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020324 dict_var->di_tv.vval.v_dict = dict;
20325 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020326 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020327 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20328 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020329}
20330
20331/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020332 * Unreference a dictionary initialized by init_var_dict().
20333 */
20334 void
20335unref_var_dict(dict)
20336 dict_T *dict;
20337{
20338 /* Now the dict needs to be freed if no one else is using it, go back to
20339 * normal reference counting. */
20340 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20341 dict_unref(dict);
20342}
20343
20344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020345 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020346 * Frees all allocated variables and the value they contain.
20347 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 */
20349 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020350vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020351 hashtab_T *ht;
20352{
20353 vars_clear_ext(ht, TRUE);
20354}
20355
20356/*
20357 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20358 */
20359 static void
20360vars_clear_ext(ht, free_val)
20361 hashtab_T *ht;
20362 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363{
Bram Moolenaara7043832005-01-21 11:56:39 +000020364 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020365 hashitem_T *hi;
20366 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020367
Bram Moolenaar33570922005-01-25 22:26:29 +000020368 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020369 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020370 for (hi = ht->ht_array; todo > 0; ++hi)
20371 {
20372 if (!HASHITEM_EMPTY(hi))
20373 {
20374 --todo;
20375
Bram Moolenaar33570922005-01-25 22:26:29 +000020376 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020377 * ht_array might change then. hash_clear() takes care of it
20378 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020379 v = HI2DI(hi);
20380 if (free_val)
20381 clear_tv(&v->di_tv);
20382 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20383 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020384 }
20385 }
20386 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020387 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020388}
20389
Bram Moolenaara7043832005-01-21 11:56:39 +000020390/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020391 * Delete a variable from hashtab "ht" at item "hi".
20392 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020393 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020395delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020396 hashtab_T *ht;
20397 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398{
Bram Moolenaar33570922005-01-25 22:26:29 +000020399 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020400
20401 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020402 clear_tv(&di->di_tv);
20403 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020404}
20405
20406/*
20407 * List the value of one internal variable.
20408 */
20409 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020410list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020411 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020413 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020415 char_u *tofree;
20416 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020417 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020418
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020419 current_copyID += COPYID_INC;
20420 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020421 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020422 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020423 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424}
20425
Bram Moolenaar071d4272004-06-13 20:20:40 +000020426 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020427list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020428 char_u *prefix;
20429 char_u *name;
20430 int type;
20431 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020432 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433{
Bram Moolenaar31859182007-08-14 20:41:13 +000020434 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20435 msg_start();
20436 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020437 if (name != NULL) /* "a:" vars don't have a name stored */
20438 msg_puts(name);
20439 msg_putchar(' ');
20440 msg_advance(22);
20441 if (type == VAR_NUMBER)
20442 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020443 else if (type == VAR_FUNC)
20444 msg_putchar('*');
20445 else if (type == VAR_LIST)
20446 {
20447 msg_putchar('[');
20448 if (*string == '[')
20449 ++string;
20450 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020451 else if (type == VAR_DICT)
20452 {
20453 msg_putchar('{');
20454 if (*string == '{')
20455 ++string;
20456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020457 else
20458 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020459
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020461
20462 if (type == VAR_FUNC)
20463 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020464 if (*first)
20465 {
20466 msg_clr_eos();
20467 *first = FALSE;
20468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469}
20470
20471/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020472 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 * If the variable already exists, the value is updated.
20474 * Otherwise the variable is created.
20475 */
20476 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020477set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020479 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020480 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020481{
Bram Moolenaar33570922005-01-25 22:26:29 +000020482 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020484 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020486 ht = find_var_ht(name, &varname);
20487 if (ht == NULL || *varname == NUL)
20488 {
20489 EMSG2(_(e_illvar), name);
20490 return;
20491 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020492 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020493
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020494 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20495 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020496
Bram Moolenaar33570922005-01-25 22:26:29 +000020497 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020498 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020499 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020500 if (var_check_ro(v->di_flags, name)
20501 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020502 return;
20503 if (v->di_tv.v_type != tv->v_type
20504 && !((v->di_tv.v_type == VAR_STRING
20505 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020506 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020507 || tv->v_type == VAR_NUMBER))
20508#ifdef FEAT_FLOAT
20509 && !((v->di_tv.v_type == VAR_NUMBER
20510 || v->di_tv.v_type == VAR_FLOAT)
20511 && (tv->v_type == VAR_NUMBER
20512 || tv->v_type == VAR_FLOAT))
20513#endif
20514 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020515 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020516 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020517 return;
20518 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020519
20520 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020521 * Handle setting internal v: variables separately: we don't change
20522 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020523 */
20524 if (ht == &vimvarht)
20525 {
20526 if (v->di_tv.v_type == VAR_STRING)
20527 {
20528 vim_free(v->di_tv.vval.v_string);
20529 if (copy || tv->v_type != VAR_STRING)
20530 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20531 else
20532 {
20533 /* Take over the string to avoid an extra alloc/free. */
20534 v->di_tv.vval.v_string = tv->vval.v_string;
20535 tv->vval.v_string = NULL;
20536 }
20537 }
20538 else if (v->di_tv.v_type != VAR_NUMBER)
20539 EMSG2(_(e_intern2), "set_var()");
20540 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020541 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020542 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020543 if (STRCMP(varname, "searchforward") == 0)
20544 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20545 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020546 return;
20547 }
20548
20549 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550 }
20551 else /* add a new variable */
20552 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020553 /* Can't add "v:" variable. */
20554 if (ht == &vimvarht)
20555 {
20556 EMSG2(_(e_illvar), name);
20557 return;
20558 }
20559
Bram Moolenaar92124a32005-06-17 22:03:40 +000020560 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020561 if (!valid_varname(varname))
20562 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020563
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020564 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20565 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020566 if (v == NULL)
20567 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020568 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020569 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020571 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020572 return;
20573 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020574 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020576
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020577 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020578 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020579 else
20580 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020581 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020582 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020583 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585}
20586
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020587/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020588 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020589 * Also give an error message.
20590 */
20591 static int
20592var_check_ro(flags, name)
20593 int flags;
20594 char_u *name;
20595{
20596 if (flags & DI_FLAGS_RO)
20597 {
20598 EMSG2(_(e_readonlyvar), name);
20599 return TRUE;
20600 }
20601 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20602 {
20603 EMSG2(_(e_readonlysbx), name);
20604 return TRUE;
20605 }
20606 return FALSE;
20607}
20608
20609/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020610 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20611 * Also give an error message.
20612 */
20613 static int
20614var_check_fixed(flags, name)
20615 int flags;
20616 char_u *name;
20617{
20618 if (flags & DI_FLAGS_FIX)
20619 {
20620 EMSG2(_("E795: Cannot delete variable %s"), name);
20621 return TRUE;
20622 }
20623 return FALSE;
20624}
20625
20626/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020627 * Check if a funcref is assigned to a valid variable name.
20628 * Return TRUE and give an error if not.
20629 */
20630 static int
20631var_check_func_name(name, new_var)
20632 char_u *name; /* points to start of variable name */
20633 int new_var; /* TRUE when creating the variable */
20634{
20635 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20636 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20637 ? name[2] : name[0]))
20638 {
20639 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20640 name);
20641 return TRUE;
20642 }
20643 /* Don't allow hiding a function. When "v" is not NULL we might be
20644 * assigning another function to the same var, the type is checked
20645 * below. */
20646 if (new_var && function_exists(name))
20647 {
20648 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20649 name);
20650 return TRUE;
20651 }
20652 return FALSE;
20653}
20654
20655/*
20656 * Check if a variable name is valid.
20657 * Return FALSE and give an error if not.
20658 */
20659 static int
20660valid_varname(varname)
20661 char_u *varname;
20662{
20663 char_u *p;
20664
20665 for (p = varname; *p != NUL; ++p)
20666 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20667 && *p != AUTOLOAD_CHAR)
20668 {
20669 EMSG2(_(e_illvar), varname);
20670 return FALSE;
20671 }
20672 return TRUE;
20673}
20674
20675/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020676 * Return TRUE if typeval "tv" is set to be locked (immutable).
20677 * Also give an error message, using "name".
20678 */
20679 static int
20680tv_check_lock(lock, name)
20681 int lock;
20682 char_u *name;
20683{
20684 if (lock & VAR_LOCKED)
20685 {
20686 EMSG2(_("E741: Value is locked: %s"),
20687 name == NULL ? (char_u *)_("Unknown") : name);
20688 return TRUE;
20689 }
20690 if (lock & VAR_FIXED)
20691 {
20692 EMSG2(_("E742: Cannot change value of %s"),
20693 name == NULL ? (char_u *)_("Unknown") : name);
20694 return TRUE;
20695 }
20696 return FALSE;
20697}
20698
20699/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020700 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020701 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020702 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020703 * It is OK for "from" and "to" to point to the same item. This is used to
20704 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020705 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020706 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020707copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020708 typval_T *from;
20709 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020710{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020711 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020712 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020713 switch (from->v_type)
20714 {
20715 case VAR_NUMBER:
20716 to->vval.v_number = from->vval.v_number;
20717 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020718#ifdef FEAT_FLOAT
20719 case VAR_FLOAT:
20720 to->vval.v_float = from->vval.v_float;
20721 break;
20722#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020723 case VAR_STRING:
20724 case VAR_FUNC:
20725 if (from->vval.v_string == NULL)
20726 to->vval.v_string = NULL;
20727 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020728 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020729 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020730 if (from->v_type == VAR_FUNC)
20731 func_ref(to->vval.v_string);
20732 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020733 break;
20734 case VAR_LIST:
20735 if (from->vval.v_list == NULL)
20736 to->vval.v_list = NULL;
20737 else
20738 {
20739 to->vval.v_list = from->vval.v_list;
20740 ++to->vval.v_list->lv_refcount;
20741 }
20742 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020743 case VAR_DICT:
20744 if (from->vval.v_dict == NULL)
20745 to->vval.v_dict = NULL;
20746 else
20747 {
20748 to->vval.v_dict = from->vval.v_dict;
20749 ++to->vval.v_dict->dv_refcount;
20750 }
20751 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020752 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020753 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020754 break;
20755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020756}
20757
20758/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020759 * Make a copy of an item.
20760 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020761 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20762 * reference to an already copied list/dict can be used.
20763 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020764 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020765 static int
20766item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020767 typval_T *from;
20768 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020769 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020770 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020771{
20772 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020773 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020774
Bram Moolenaar33570922005-01-25 22:26:29 +000020775 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020776 {
20777 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020778 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020779 }
20780 ++recurse;
20781
20782 switch (from->v_type)
20783 {
20784 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020785#ifdef FEAT_FLOAT
20786 case VAR_FLOAT:
20787#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020788 case VAR_STRING:
20789 case VAR_FUNC:
20790 copy_tv(from, to);
20791 break;
20792 case VAR_LIST:
20793 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020794 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020795 if (from->vval.v_list == NULL)
20796 to->vval.v_list = NULL;
20797 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20798 {
20799 /* use the copy made earlier */
20800 to->vval.v_list = from->vval.v_list->lv_copylist;
20801 ++to->vval.v_list->lv_refcount;
20802 }
20803 else
20804 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20805 if (to->vval.v_list == NULL)
20806 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020807 break;
20808 case VAR_DICT:
20809 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020810 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020811 if (from->vval.v_dict == NULL)
20812 to->vval.v_dict = NULL;
20813 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20814 {
20815 /* use the copy made earlier */
20816 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20817 ++to->vval.v_dict->dv_refcount;
20818 }
20819 else
20820 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20821 if (to->vval.v_dict == NULL)
20822 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020823 break;
20824 default:
20825 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020826 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020827 }
20828 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020829 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020830}
20831
20832/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833 * ":echo expr1 ..." print each argument separated with a space, add a
20834 * newline at the end.
20835 * ":echon expr1 ..." print each argument plain.
20836 */
20837 void
20838ex_echo(eap)
20839 exarg_T *eap;
20840{
20841 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020842 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020843 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844 char_u *p;
20845 int needclr = TRUE;
20846 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020847 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020848
20849 if (eap->skip)
20850 ++emsg_skip;
20851 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20852 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020853 /* If eval1() causes an error message the text from the command may
20854 * still need to be cleared. E.g., "echo 22,44". */
20855 need_clr_eos = needclr;
20856
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020858 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020859 {
20860 /*
20861 * Report the invalid expression unless the expression evaluation
20862 * has been cancelled due to an aborting error, an interrupt, or an
20863 * exception.
20864 */
20865 if (!aborting())
20866 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020867 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 break;
20869 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020870 need_clr_eos = FALSE;
20871
Bram Moolenaar071d4272004-06-13 20:20:40 +000020872 if (!eap->skip)
20873 {
20874 if (atstart)
20875 {
20876 atstart = FALSE;
20877 /* Call msg_start() after eval1(), evaluating the expression
20878 * may cause a message to appear. */
20879 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020880 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020881 /* Mark the saved text as finishing the line, so that what
20882 * follows is displayed on a new line when scrolling back
20883 * at the more prompt. */
20884 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020887 }
20888 else if (eap->cmdidx == CMD_echo)
20889 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020890 current_copyID += COPYID_INC;
20891 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020892 if (p != NULL)
20893 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020895 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020896 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020897 if (*p != TAB && needclr)
20898 {
20899 /* remove any text still there from the command */
20900 msg_clr_eos();
20901 needclr = FALSE;
20902 }
20903 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 }
20905 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020906 {
20907#ifdef FEAT_MBYTE
20908 if (has_mbyte)
20909 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020910 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020911
20912 (void)msg_outtrans_len_attr(p, i, echo_attr);
20913 p += i - 1;
20914 }
20915 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020917 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20918 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020920 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020922 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923 arg = skipwhite(arg);
20924 }
20925 eap->nextcmd = check_nextcmd(arg);
20926
20927 if (eap->skip)
20928 --emsg_skip;
20929 else
20930 {
20931 /* remove text that may still be there from the command */
20932 if (needclr)
20933 msg_clr_eos();
20934 if (eap->cmdidx == CMD_echo)
20935 msg_end();
20936 }
20937}
20938
20939/*
20940 * ":echohl {name}".
20941 */
20942 void
20943ex_echohl(eap)
20944 exarg_T *eap;
20945{
20946 int id;
20947
20948 id = syn_name2id(eap->arg);
20949 if (id == 0)
20950 echo_attr = 0;
20951 else
20952 echo_attr = syn_id2attr(id);
20953}
20954
20955/*
20956 * ":execute expr1 ..." execute the result of an expression.
20957 * ":echomsg expr1 ..." Print a message
20958 * ":echoerr expr1 ..." Print an error
20959 * Each gets spaces around each argument and a newline at the end for
20960 * echo commands
20961 */
20962 void
20963ex_execute(eap)
20964 exarg_T *eap;
20965{
20966 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020967 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968 int ret = OK;
20969 char_u *p;
20970 garray_T ga;
20971 int len;
20972 int save_did_emsg;
20973
20974 ga_init2(&ga, 1, 80);
20975
20976 if (eap->skip)
20977 ++emsg_skip;
20978 while (*arg != NUL && *arg != '|' && *arg != '\n')
20979 {
20980 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020981 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020982 {
20983 /*
20984 * Report the invalid expression unless the expression evaluation
20985 * has been cancelled due to an aborting error, an interrupt, or an
20986 * exception.
20987 */
20988 if (!aborting())
20989 EMSG2(_(e_invexpr2), p);
20990 ret = FAIL;
20991 break;
20992 }
20993
20994 if (!eap->skip)
20995 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020996 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997 len = (int)STRLEN(p);
20998 if (ga_grow(&ga, len + 2) == FAIL)
20999 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021000 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001 ret = FAIL;
21002 break;
21003 }
21004 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021006 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021007 ga.ga_len += len;
21008 }
21009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021010 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011 arg = skipwhite(arg);
21012 }
21013
21014 if (ret != FAIL && ga.ga_data != NULL)
21015 {
21016 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021019 out_flush();
21020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021 else if (eap->cmdidx == CMD_echoerr)
21022 {
21023 /* We don't want to abort following commands, restore did_emsg. */
21024 save_did_emsg = did_emsg;
21025 EMSG((char_u *)ga.ga_data);
21026 if (!force_abort)
21027 did_emsg = save_did_emsg;
21028 }
21029 else if (eap->cmdidx == CMD_execute)
21030 do_cmdline((char_u *)ga.ga_data,
21031 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21032 }
21033
21034 ga_clear(&ga);
21035
21036 if (eap->skip)
21037 --emsg_skip;
21038
21039 eap->nextcmd = check_nextcmd(arg);
21040}
21041
21042/*
21043 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21044 * "arg" points to the "&" or '+' when called, to "option" when returning.
21045 * Returns NULL when no option name found. Otherwise pointer to the char
21046 * after the option name.
21047 */
21048 static char_u *
21049find_option_end(arg, opt_flags)
21050 char_u **arg;
21051 int *opt_flags;
21052{
21053 char_u *p = *arg;
21054
21055 ++p;
21056 if (*p == 'g' && p[1] == ':')
21057 {
21058 *opt_flags = OPT_GLOBAL;
21059 p += 2;
21060 }
21061 else if (*p == 'l' && p[1] == ':')
21062 {
21063 *opt_flags = OPT_LOCAL;
21064 p += 2;
21065 }
21066 else
21067 *opt_flags = 0;
21068
21069 if (!ASCII_ISALPHA(*p))
21070 return NULL;
21071 *arg = p;
21072
21073 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21074 p += 4; /* termcap option */
21075 else
21076 while (ASCII_ISALPHA(*p))
21077 ++p;
21078 return p;
21079}
21080
21081/*
21082 * ":function"
21083 */
21084 void
21085ex_function(eap)
21086 exarg_T *eap;
21087{
21088 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021089 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 int j;
21091 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 char_u *name = NULL;
21094 char_u *p;
21095 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021096 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021097 garray_T newargs;
21098 garray_T newlines;
21099 int varargs = FALSE;
21100 int mustend = FALSE;
21101 int flags = 0;
21102 ufunc_T *fp;
21103 int indent;
21104 int nesting;
21105 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021106 dictitem_T *v;
21107 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021108 static int func_nr = 0; /* number for nameless function */
21109 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021110 hashtab_T *ht;
21111 int todo;
21112 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021113 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114
21115 /*
21116 * ":function" without argument: list functions.
21117 */
21118 if (ends_excmd(*eap->arg))
21119 {
21120 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021121 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021122 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021123 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021124 {
21125 if (!HASHITEM_EMPTY(hi))
21126 {
21127 --todo;
21128 fp = HI2UF(hi);
21129 if (!isdigit(*fp->uf_name))
21130 list_func_head(fp, FALSE);
21131 }
21132 }
21133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 eap->nextcmd = check_nextcmd(eap->arg);
21135 return;
21136 }
21137
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021138 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021139 * ":function /pat": list functions matching pattern.
21140 */
21141 if (*eap->arg == '/')
21142 {
21143 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21144 if (!eap->skip)
21145 {
21146 regmatch_T regmatch;
21147
21148 c = *p;
21149 *p = NUL;
21150 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21151 *p = c;
21152 if (regmatch.regprog != NULL)
21153 {
21154 regmatch.rm_ic = p_ic;
21155
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021156 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021157 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21158 {
21159 if (!HASHITEM_EMPTY(hi))
21160 {
21161 --todo;
21162 fp = HI2UF(hi);
21163 if (!isdigit(*fp->uf_name)
21164 && vim_regexec(&regmatch, fp->uf_name, 0))
21165 list_func_head(fp, FALSE);
21166 }
21167 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021168 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021169 }
21170 }
21171 if (*p == '/')
21172 ++p;
21173 eap->nextcmd = check_nextcmd(p);
21174 return;
21175 }
21176
21177 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021178 * Get the function name. There are these situations:
21179 * func normal function name
21180 * "name" == func, "fudi.fd_dict" == NULL
21181 * dict.func new dictionary entry
21182 * "name" == NULL, "fudi.fd_dict" set,
21183 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21184 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021185 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021186 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21187 * dict.func existing dict entry that's not a Funcref
21188 * "name" == NULL, "fudi.fd_dict" set,
21189 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21190 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021191 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021192 name = trans_function_name(&p, eap->skip, 0, &fudi);
21193 paren = (vim_strchr(p, '(') != NULL);
21194 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 {
21196 /*
21197 * Return on an invalid expression in braces, unless the expression
21198 * evaluation has been cancelled due to an aborting error, an
21199 * interrupt, or an exception.
21200 */
21201 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021202 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021203 if (!eap->skip && fudi.fd_newkey != NULL)
21204 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021205 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208 else
21209 eap->skip = TRUE;
21210 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021211
Bram Moolenaar071d4272004-06-13 20:20:40 +000021212 /* An error in a function call during evaluation of an expression in magic
21213 * braces should not cause the function not to be defined. */
21214 saved_did_emsg = did_emsg;
21215 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216
21217 /*
21218 * ":function func" with only function name: list function.
21219 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021220 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 {
21222 if (!ends_excmd(*skipwhite(p)))
21223 {
21224 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021225 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 }
21227 eap->nextcmd = check_nextcmd(p);
21228 if (eap->nextcmd != NULL)
21229 *p = NUL;
21230 if (!eap->skip && !got_int)
21231 {
21232 fp = find_func(name);
21233 if (fp != NULL)
21234 {
21235 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021236 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021237 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021238 if (FUNCLINE(fp, j) == NULL)
21239 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021240 msg_putchar('\n');
21241 msg_outnum((long)(j + 1));
21242 if (j < 9)
21243 msg_putchar(' ');
21244 if (j < 99)
21245 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021246 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021247 out_flush(); /* show a line at a time */
21248 ui_breakcheck();
21249 }
21250 if (!got_int)
21251 {
21252 msg_putchar('\n');
21253 msg_puts((char_u *)" endfunction");
21254 }
21255 }
21256 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021257 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021259 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260 }
21261
21262 /*
21263 * ":function name(arg1, arg2)" Define function.
21264 */
21265 p = skipwhite(p);
21266 if (*p != '(')
21267 {
21268 if (!eap->skip)
21269 {
21270 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021271 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021272 }
21273 /* attempt to continue by skipping some text */
21274 if (vim_strchr(p, '(') != NULL)
21275 p = vim_strchr(p, '(');
21276 }
21277 p = skipwhite(p + 1);
21278
21279 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21280 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21281
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021282 if (!eap->skip)
21283 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021284 /* Check the name of the function. Unless it's a dictionary function
21285 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021286 if (name != NULL)
21287 arg = name;
21288 else
21289 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021290 if (arg != NULL && (fudi.fd_di == NULL
21291 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021292 {
21293 if (*arg == K_SPECIAL)
21294 j = 3;
21295 else
21296 j = 0;
21297 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21298 : eval_isnamec(arg[j])))
21299 ++j;
21300 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021301 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021302 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021303 /* Disallow using the g: dict. */
21304 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21305 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021306 }
21307
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308 /*
21309 * Isolate the arguments: "arg1, arg2, ...)"
21310 */
21311 while (*p != ')')
21312 {
21313 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21314 {
21315 varargs = TRUE;
21316 p += 3;
21317 mustend = TRUE;
21318 }
21319 else
21320 {
21321 arg = p;
21322 while (ASCII_ISALNUM(*p) || *p == '_')
21323 ++p;
21324 if (arg == p || isdigit(*arg)
21325 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21326 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21327 {
21328 if (!eap->skip)
21329 EMSG2(_("E125: Illegal argument: %s"), arg);
21330 break;
21331 }
21332 if (ga_grow(&newargs, 1) == FAIL)
21333 goto erret;
21334 c = *p;
21335 *p = NUL;
21336 arg = vim_strsave(arg);
21337 if (arg == NULL)
21338 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021339
21340 /* Check for duplicate argument name. */
21341 for (i = 0; i < newargs.ga_len; ++i)
21342 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21343 {
21344 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21345 goto erret;
21346 }
21347
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21349 *p = c;
21350 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351 if (*p == ',')
21352 ++p;
21353 else
21354 mustend = TRUE;
21355 }
21356 p = skipwhite(p);
21357 if (mustend && *p != ')')
21358 {
21359 if (!eap->skip)
21360 EMSG2(_(e_invarg2), eap->arg);
21361 break;
21362 }
21363 }
21364 ++p; /* skip the ')' */
21365
Bram Moolenaare9a41262005-01-15 22:18:47 +000021366 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 for (;;)
21368 {
21369 p = skipwhite(p);
21370 if (STRNCMP(p, "range", 5) == 0)
21371 {
21372 flags |= FC_RANGE;
21373 p += 5;
21374 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021375 else if (STRNCMP(p, "dict", 4) == 0)
21376 {
21377 flags |= FC_DICT;
21378 p += 4;
21379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380 else if (STRNCMP(p, "abort", 5) == 0)
21381 {
21382 flags |= FC_ABORT;
21383 p += 5;
21384 }
21385 else
21386 break;
21387 }
21388
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021389 /* When there is a line break use what follows for the function body.
21390 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21391 if (*p == '\n')
21392 line_arg = p + 1;
21393 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 EMSG(_(e_trailing));
21395
21396 /*
21397 * Read the body of the function, until ":endfunction" is found.
21398 */
21399 if (KeyTyped)
21400 {
21401 /* Check if the function already exists, don't let the user type the
21402 * whole function before telling him it doesn't work! For a script we
21403 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021404 if (!eap->skip && !eap->forceit)
21405 {
21406 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21407 EMSG(_(e_funcdict));
21408 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021409 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021411
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021412 if (!eap->skip && did_emsg)
21413 goto erret;
21414
Bram Moolenaar071d4272004-06-13 20:20:40 +000021415 msg_putchar('\n'); /* don't overwrite the function name */
21416 cmdline_row = msg_row;
21417 }
21418
21419 indent = 2;
21420 nesting = 0;
21421 for (;;)
21422 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021423 if (KeyTyped)
21424 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021426 sourcing_lnum_off = sourcing_lnum;
21427
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021428 if (line_arg != NULL)
21429 {
21430 /* Use eap->arg, split up in parts by line breaks. */
21431 theline = line_arg;
21432 p = vim_strchr(theline, '\n');
21433 if (p == NULL)
21434 line_arg += STRLEN(line_arg);
21435 else
21436 {
21437 *p = NUL;
21438 line_arg = p + 1;
21439 }
21440 }
21441 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442 theline = getcmdline(':', 0L, indent);
21443 else
21444 theline = eap->getline(':', eap->cookie, indent);
21445 if (KeyTyped)
21446 lines_left = Rows - 1;
21447 if (theline == NULL)
21448 {
21449 EMSG(_("E126: Missing :endfunction"));
21450 goto erret;
21451 }
21452
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021453 /* Detect line continuation: sourcing_lnum increased more than one. */
21454 if (sourcing_lnum > sourcing_lnum_off + 1)
21455 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21456 else
21457 sourcing_lnum_off = 0;
21458
Bram Moolenaar071d4272004-06-13 20:20:40 +000021459 if (skip_until != NULL)
21460 {
21461 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21462 * don't check for ":endfunc". */
21463 if (STRCMP(theline, skip_until) == 0)
21464 {
21465 vim_free(skip_until);
21466 skip_until = NULL;
21467 }
21468 }
21469 else
21470 {
21471 /* skip ':' and blanks*/
21472 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21473 ;
21474
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021475 /* Check for "endfunction". */
21476 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021478 if (line_arg == NULL)
21479 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480 break;
21481 }
21482
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021483 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021484 * at "end". */
21485 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21486 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021487 else if (STRNCMP(p, "if", 2) == 0
21488 || STRNCMP(p, "wh", 2) == 0
21489 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021490 || STRNCMP(p, "try", 3) == 0)
21491 indent += 2;
21492
21493 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021494 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021496 if (*p == '!')
21497 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 p += eval_fname_script(p);
21499 if (ASCII_ISALPHA(*p))
21500 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021501 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 if (*skipwhite(p) == '(')
21503 {
21504 ++nesting;
21505 indent += 2;
21506 }
21507 }
21508 }
21509
21510 /* Check for ":append" or ":insert". */
21511 p = skip_range(p, NULL);
21512 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21513 || (p[0] == 'i'
21514 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21515 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21516 skip_until = vim_strsave((char_u *)".");
21517
21518 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21519 arg = skipwhite(skiptowhite(p));
21520 if (arg[0] == '<' && arg[1] =='<'
21521 && ((p[0] == 'p' && p[1] == 'y'
21522 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21523 || (p[0] == 'p' && p[1] == 'e'
21524 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21525 || (p[0] == 't' && p[1] == 'c'
21526 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021527 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21528 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21530 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021531 || (p[0] == 'm' && p[1] == 'z'
21532 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 ))
21534 {
21535 /* ":python <<" continues until a dot, like ":append" */
21536 p = skipwhite(arg + 2);
21537 if (*p == NUL)
21538 skip_until = vim_strsave((char_u *)".");
21539 else
21540 skip_until = vim_strsave(p);
21541 }
21542 }
21543
21544 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021545 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021546 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021547 if (line_arg == NULL)
21548 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021549 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021550 }
21551
21552 /* Copy the line to newly allocated memory. get_one_sourceline()
21553 * allocates 250 bytes per line, this saves 80% on average. The cost
21554 * is an extra alloc/free. */
21555 p = vim_strsave(theline);
21556 if (p != NULL)
21557 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021558 if (line_arg == NULL)
21559 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021560 theline = p;
21561 }
21562
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021563 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21564
21565 /* Add NULL lines for continuation lines, so that the line count is
21566 * equal to the index in the growarray. */
21567 while (sourcing_lnum_off-- > 0)
21568 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021569
21570 /* Check for end of eap->arg. */
21571 if (line_arg != NULL && *line_arg == NUL)
21572 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573 }
21574
21575 /* Don't define the function when skipping commands or when an error was
21576 * detected. */
21577 if (eap->skip || did_emsg)
21578 goto erret;
21579
21580 /*
21581 * If there are no errors, add the function
21582 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021583 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021584 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021585 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021586 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021587 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021588 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021589 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021590 goto erret;
21591 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021592
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021593 fp = find_func(name);
21594 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021595 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021596 if (!eap->forceit)
21597 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021598 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021599 goto erret;
21600 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021601 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021602 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021603 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021604 name);
21605 goto erret;
21606 }
21607 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021608 ga_clear_strings(&(fp->uf_args));
21609 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021610 vim_free(name);
21611 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 }
21614 else
21615 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021616 char numbuf[20];
21617
21618 fp = NULL;
21619 if (fudi.fd_newkey == NULL && !eap->forceit)
21620 {
21621 EMSG(_(e_funcdict));
21622 goto erret;
21623 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021624 if (fudi.fd_di == NULL)
21625 {
21626 /* Can't add a function to a locked dictionary */
21627 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21628 goto erret;
21629 }
21630 /* Can't change an existing function if it is locked */
21631 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21632 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021633
21634 /* Give the function a sequential number. Can only be used with a
21635 * Funcref! */
21636 vim_free(name);
21637 sprintf(numbuf, "%d", ++func_nr);
21638 name = vim_strsave((char_u *)numbuf);
21639 if (name == NULL)
21640 goto erret;
21641 }
21642
21643 if (fp == NULL)
21644 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021645 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021646 {
21647 int slen, plen;
21648 char_u *scriptname;
21649
21650 /* Check that the autoload name matches the script name. */
21651 j = FAIL;
21652 if (sourcing_name != NULL)
21653 {
21654 scriptname = autoload_name(name);
21655 if (scriptname != NULL)
21656 {
21657 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021658 plen = (int)STRLEN(p);
21659 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021660 if (slen > plen && fnamecmp(p,
21661 sourcing_name + slen - plen) == 0)
21662 j = OK;
21663 vim_free(scriptname);
21664 }
21665 }
21666 if (j == FAIL)
21667 {
21668 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21669 goto erret;
21670 }
21671 }
21672
21673 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 if (fp == NULL)
21675 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021676
21677 if (fudi.fd_dict != NULL)
21678 {
21679 if (fudi.fd_di == NULL)
21680 {
21681 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021682 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021683 if (fudi.fd_di == NULL)
21684 {
21685 vim_free(fp);
21686 goto erret;
21687 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021688 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21689 {
21690 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021691 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021692 goto erret;
21693 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021694 }
21695 else
21696 /* overwrite existing dict entry */
21697 clear_tv(&fudi.fd_di->di_tv);
21698 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021699 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021700 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021701 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021702
21703 /* behave like "dict" was used */
21704 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021705 }
21706
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021708 STRCPY(fp->uf_name, name);
21709 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021710 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021711 fp->uf_args = newargs;
21712 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021713#ifdef FEAT_PROFILE
21714 fp->uf_tml_count = NULL;
21715 fp->uf_tml_total = NULL;
21716 fp->uf_tml_self = NULL;
21717 fp->uf_profiling = FALSE;
21718 if (prof_def_func())
21719 func_do_profile(fp);
21720#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021721 fp->uf_varargs = varargs;
21722 fp->uf_flags = flags;
21723 fp->uf_calls = 0;
21724 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021725 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726
21727erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728 ga_clear_strings(&newargs);
21729 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021730ret_free:
21731 vim_free(skip_until);
21732 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021733 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021735}
21736
21737/*
21738 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021739 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021741 * flags:
21742 * TFN_INT: internal function name OK
21743 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744 * Advances "pp" to just after the function name (if no error).
21745 */
21746 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021747trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748 char_u **pp;
21749 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021750 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021751 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021753 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021754 char_u *start;
21755 char_u *end;
21756 int lead;
21757 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021759 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021760
21761 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021762 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021764
21765 /* Check for hard coded <SNR>: already translated function ID (from a user
21766 * command). */
21767 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21768 && (*pp)[2] == (int)KE_SNR)
21769 {
21770 *pp += 3;
21771 len = get_id_len(pp) + 3;
21772 return vim_strnsave(start, len);
21773 }
21774
21775 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21776 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021778 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021779 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021780
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021781 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21782 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021783 if (end == start)
21784 {
21785 if (!skip)
21786 EMSG(_("E129: Function name required"));
21787 goto theend;
21788 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021789 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021790 {
21791 /*
21792 * Report an invalid expression in braces, unless the expression
21793 * evaluation has been cancelled due to an aborting error, an
21794 * interrupt, or an exception.
21795 */
21796 if (!aborting())
21797 {
21798 if (end != NULL)
21799 EMSG2(_(e_invarg2), start);
21800 }
21801 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021802 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021803 goto theend;
21804 }
21805
21806 if (lv.ll_tv != NULL)
21807 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021808 if (fdp != NULL)
21809 {
21810 fdp->fd_dict = lv.ll_dict;
21811 fdp->fd_newkey = lv.ll_newkey;
21812 lv.ll_newkey = NULL;
21813 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021814 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021815 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21816 {
21817 name = vim_strsave(lv.ll_tv->vval.v_string);
21818 *pp = end;
21819 }
21820 else
21821 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021822 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21823 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021824 EMSG(_(e_funcref));
21825 else
21826 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021827 name = NULL;
21828 }
21829 goto theend;
21830 }
21831
21832 if (lv.ll_name == NULL)
21833 {
21834 /* Error found, but continue after the function name. */
21835 *pp = end;
21836 goto theend;
21837 }
21838
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021839 /* Check if the name is a Funcref. If so, use the value. */
21840 if (lv.ll_exp_name != NULL)
21841 {
21842 len = (int)STRLEN(lv.ll_exp_name);
21843 name = deref_func_name(lv.ll_exp_name, &len);
21844 if (name == lv.ll_exp_name)
21845 name = NULL;
21846 }
21847 else
21848 {
21849 len = (int)(end - *pp);
21850 name = deref_func_name(*pp, &len);
21851 if (name == *pp)
21852 name = NULL;
21853 }
21854 if (name != NULL)
21855 {
21856 name = vim_strsave(name);
21857 *pp = end;
21858 goto theend;
21859 }
21860
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021861 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021862 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021863 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021864 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21865 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21866 {
21867 /* When there was "s:" already or the name expanded to get a
21868 * leading "s:" then remove it. */
21869 lv.ll_name += 2;
21870 len -= 2;
21871 lead = 2;
21872 }
21873 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021874 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021875 {
21876 if (lead == 2) /* skip over "s:" */
21877 lv.ll_name += 2;
21878 len = (int)(end - lv.ll_name);
21879 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021880
21881 /*
21882 * Copy the function name to allocated memory.
21883 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21884 * Accept <SNR>123_name() outside a script.
21885 */
21886 if (skip)
21887 lead = 0; /* do nothing */
21888 else if (lead > 0)
21889 {
21890 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021891 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21892 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021893 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021894 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021895 if (current_SID <= 0)
21896 {
21897 EMSG(_(e_usingsid));
21898 goto theend;
21899 }
21900 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21901 lead += (int)STRLEN(sid_buf);
21902 }
21903 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021904 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021905 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021906 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021907 goto theend;
21908 }
21909 name = alloc((unsigned)(len + lead + 1));
21910 if (name != NULL)
21911 {
21912 if (lead > 0)
21913 {
21914 name[0] = K_SPECIAL;
21915 name[1] = KS_EXTRA;
21916 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021917 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021918 STRCPY(name + 3, sid_buf);
21919 }
21920 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21921 name[len + lead] = NUL;
21922 }
21923 *pp = end;
21924
21925theend:
21926 clear_lval(&lv);
21927 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928}
21929
21930/*
21931 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21932 * Return 2 if "p" starts with "s:".
21933 * Return 0 otherwise.
21934 */
21935 static int
21936eval_fname_script(p)
21937 char_u *p;
21938{
21939 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21940 || STRNICMP(p + 1, "SNR>", 4) == 0))
21941 return 5;
21942 if (p[0] == 's' && p[1] == ':')
21943 return 2;
21944 return 0;
21945}
21946
21947/*
21948 * Return TRUE if "p" starts with "<SID>" or "s:".
21949 * Only works if eval_fname_script() returned non-zero for "p"!
21950 */
21951 static int
21952eval_fname_sid(p)
21953 char_u *p;
21954{
21955 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21956}
21957
21958/*
21959 * List the head of the function: "name(arg1, arg2)".
21960 */
21961 static void
21962list_func_head(fp, indent)
21963 ufunc_T *fp;
21964 int indent;
21965{
21966 int j;
21967
21968 msg_start();
21969 if (indent)
21970 MSG_PUTS(" ");
21971 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021972 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973 {
21974 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021975 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976 }
21977 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021978 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021979 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021980 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981 {
21982 if (j)
21983 MSG_PUTS(", ");
21984 msg_puts(FUNCARG(fp, j));
21985 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021986 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 {
21988 if (j)
21989 MSG_PUTS(", ");
21990 MSG_PUTS("...");
21991 }
21992 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020021993 if (fp->uf_flags & FC_ABORT)
21994 MSG_PUTS(" abort");
21995 if (fp->uf_flags & FC_RANGE)
21996 MSG_PUTS(" range");
21997 if (fp->uf_flags & FC_DICT)
21998 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000021999 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022000 if (p_verbose > 0)
22001 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022002}
22003
22004/*
22005 * Find a function by name, return pointer to it in ufuncs.
22006 * Return NULL for unknown function.
22007 */
22008 static ufunc_T *
22009find_func(name)
22010 char_u *name;
22011{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022012 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022014 hi = hash_find(&func_hashtab, name);
22015 if (!HASHITEM_EMPTY(hi))
22016 return HI2UF(hi);
22017 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022018}
22019
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022020#if defined(EXITFREE) || defined(PROTO)
22021 void
22022free_all_functions()
22023{
22024 hashitem_T *hi;
22025
22026 /* Need to start all over every time, because func_free() may change the
22027 * hash table. */
22028 while (func_hashtab.ht_used > 0)
22029 for (hi = func_hashtab.ht_array; ; ++hi)
22030 if (!HASHITEM_EMPTY(hi))
22031 {
22032 func_free(HI2UF(hi));
22033 break;
22034 }
22035}
22036#endif
22037
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022038 int
22039translated_function_exists(name)
22040 char_u *name;
22041{
22042 if (builtin_function(name))
22043 return find_internal_func(name) >= 0;
22044 return find_func(name) != NULL;
22045}
22046
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022047/*
22048 * Return TRUE if a function "name" exists.
22049 */
22050 static int
22051function_exists(name)
22052 char_u *name;
22053{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022054 char_u *nm = name;
22055 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022056 int n = FALSE;
22057
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022058 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022059 nm = skipwhite(nm);
22060
22061 /* Only accept "funcname", "funcname ", "funcname (..." and
22062 * "funcname(...", not "funcname!...". */
22063 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022064 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022065 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022066 return n;
22067}
22068
Bram Moolenaara1544c02013-05-30 12:35:52 +020022069 char_u *
22070get_expanded_name(name, check)
22071 char_u *name;
22072 int check;
22073{
22074 char_u *nm = name;
22075 char_u *p;
22076
22077 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22078
22079 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022080 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022081 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022082
Bram Moolenaara1544c02013-05-30 12:35:52 +020022083 vim_free(p);
22084 return NULL;
22085}
22086
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022087/*
22088 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022089 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022090 */
22091 static int
22092builtin_function(name)
22093 char_u *name;
22094{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022095 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22096 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022097}
22098
Bram Moolenaar05159a02005-02-26 23:04:13 +000022099#if defined(FEAT_PROFILE) || defined(PROTO)
22100/*
22101 * Start profiling function "fp".
22102 */
22103 static void
22104func_do_profile(fp)
22105 ufunc_T *fp;
22106{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022107 int len = fp->uf_lines.ga_len;
22108
22109 if (len == 0)
22110 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022111 fp->uf_tm_count = 0;
22112 profile_zero(&fp->uf_tm_self);
22113 profile_zero(&fp->uf_tm_total);
22114 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022115 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022116 if (fp->uf_tml_total == NULL)
22117 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022118 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022119 if (fp->uf_tml_self == NULL)
22120 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022121 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022122 fp->uf_tml_idx = -1;
22123 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22124 || fp->uf_tml_self == NULL)
22125 return; /* out of memory */
22126
22127 fp->uf_profiling = TRUE;
22128}
22129
22130/*
22131 * Dump the profiling results for all functions in file "fd".
22132 */
22133 void
22134func_dump_profile(fd)
22135 FILE *fd;
22136{
22137 hashitem_T *hi;
22138 int todo;
22139 ufunc_T *fp;
22140 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022141 ufunc_T **sorttab;
22142 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022143
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022144 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022145 if (todo == 0)
22146 return; /* nothing to dump */
22147
Bram Moolenaar73830342005-02-28 22:48:19 +000022148 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22149
Bram Moolenaar05159a02005-02-26 23:04:13 +000022150 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22151 {
22152 if (!HASHITEM_EMPTY(hi))
22153 {
22154 --todo;
22155 fp = HI2UF(hi);
22156 if (fp->uf_profiling)
22157 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022158 if (sorttab != NULL)
22159 sorttab[st_len++] = fp;
22160
Bram Moolenaar05159a02005-02-26 23:04:13 +000022161 if (fp->uf_name[0] == K_SPECIAL)
22162 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22163 else
22164 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22165 if (fp->uf_tm_count == 1)
22166 fprintf(fd, "Called 1 time\n");
22167 else
22168 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22169 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22170 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22171 fprintf(fd, "\n");
22172 fprintf(fd, "count total (s) self (s)\n");
22173
22174 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22175 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022176 if (FUNCLINE(fp, i) == NULL)
22177 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022178 prof_func_line(fd, fp->uf_tml_count[i],
22179 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022180 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22181 }
22182 fprintf(fd, "\n");
22183 }
22184 }
22185 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022186
22187 if (sorttab != NULL && st_len > 0)
22188 {
22189 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22190 prof_total_cmp);
22191 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22192 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22193 prof_self_cmp);
22194 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22195 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022196
22197 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022198}
Bram Moolenaar73830342005-02-28 22:48:19 +000022199
22200 static void
22201prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22202 FILE *fd;
22203 ufunc_T **sorttab;
22204 int st_len;
22205 char *title;
22206 int prefer_self; /* when equal print only self time */
22207{
22208 int i;
22209 ufunc_T *fp;
22210
22211 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22212 fprintf(fd, "count total (s) self (s) function\n");
22213 for (i = 0; i < 20 && i < st_len; ++i)
22214 {
22215 fp = sorttab[i];
22216 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22217 prefer_self);
22218 if (fp->uf_name[0] == K_SPECIAL)
22219 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22220 else
22221 fprintf(fd, " %s()\n", fp->uf_name);
22222 }
22223 fprintf(fd, "\n");
22224}
22225
22226/*
22227 * Print the count and times for one function or function line.
22228 */
22229 static void
22230prof_func_line(fd, count, total, self, prefer_self)
22231 FILE *fd;
22232 int count;
22233 proftime_T *total;
22234 proftime_T *self;
22235 int prefer_self; /* when equal print only self time */
22236{
22237 if (count > 0)
22238 {
22239 fprintf(fd, "%5d ", count);
22240 if (prefer_self && profile_equal(total, self))
22241 fprintf(fd, " ");
22242 else
22243 fprintf(fd, "%s ", profile_msg(total));
22244 if (!prefer_self && profile_equal(total, self))
22245 fprintf(fd, " ");
22246 else
22247 fprintf(fd, "%s ", profile_msg(self));
22248 }
22249 else
22250 fprintf(fd, " ");
22251}
22252
22253/*
22254 * Compare function for total time sorting.
22255 */
22256 static int
22257#ifdef __BORLANDC__
22258_RTLENTRYF
22259#endif
22260prof_total_cmp(s1, s2)
22261 const void *s1;
22262 const void *s2;
22263{
22264 ufunc_T *p1, *p2;
22265
22266 p1 = *(ufunc_T **)s1;
22267 p2 = *(ufunc_T **)s2;
22268 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22269}
22270
22271/*
22272 * Compare function for self time sorting.
22273 */
22274 static int
22275#ifdef __BORLANDC__
22276_RTLENTRYF
22277#endif
22278prof_self_cmp(s1, s2)
22279 const void *s1;
22280 const void *s2;
22281{
22282 ufunc_T *p1, *p2;
22283
22284 p1 = *(ufunc_T **)s1;
22285 p2 = *(ufunc_T **)s2;
22286 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22287}
22288
Bram Moolenaar05159a02005-02-26 23:04:13 +000022289#endif
22290
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022291/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022292 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022293 * Return TRUE if a package was loaded.
22294 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022295 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022296script_autoload(name, reload)
22297 char_u *name;
22298 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022299{
22300 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022301 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022302 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022303 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022304
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022305 /* Return quickly when autoload disabled. */
22306 if (no_autoload)
22307 return FALSE;
22308
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022309 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022310 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022311 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022312 return FALSE;
22313
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022314 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022315
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022316 /* Find the name in the list of previously loaded package names. Skip
22317 * "autoload/", it's always the same. */
22318 for (i = 0; i < ga_loaded.ga_len; ++i)
22319 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22320 break;
22321 if (!reload && i < ga_loaded.ga_len)
22322 ret = FALSE; /* was loaded already */
22323 else
22324 {
22325 /* Remember the name if it wasn't loaded already. */
22326 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22327 {
22328 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22329 tofree = NULL;
22330 }
22331
22332 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022333 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022334 ret = TRUE;
22335 }
22336
22337 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022338 return ret;
22339}
22340
22341/*
22342 * Return the autoload script name for a function or variable name.
22343 * Returns NULL when out of memory.
22344 */
22345 static char_u *
22346autoload_name(name)
22347 char_u *name;
22348{
22349 char_u *p;
22350 char_u *scriptname;
22351
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022352 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022353 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22354 if (scriptname == NULL)
22355 return FALSE;
22356 STRCPY(scriptname, "autoload/");
22357 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022358 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022359 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022360 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022361 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022362 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022363}
22364
Bram Moolenaar071d4272004-06-13 20:20:40 +000022365#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22366
22367/*
22368 * Function given to ExpandGeneric() to obtain the list of user defined
22369 * function names.
22370 */
22371 char_u *
22372get_user_func_name(xp, idx)
22373 expand_T *xp;
22374 int idx;
22375{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022376 static long_u done;
22377 static hashitem_T *hi;
22378 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379
22380 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022381 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022382 done = 0;
22383 hi = func_hashtab.ht_array;
22384 }
22385 if (done < func_hashtab.ht_used)
22386 {
22387 if (done++ > 0)
22388 ++hi;
22389 while (HASHITEM_EMPTY(hi))
22390 ++hi;
22391 fp = HI2UF(hi);
22392
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022393 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022394 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022395
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022396 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22397 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398
22399 cat_func_name(IObuff, fp);
22400 if (xp->xp_context != EXPAND_USER_FUNC)
22401 {
22402 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022403 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022404 STRCAT(IObuff, ")");
22405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 return IObuff;
22407 }
22408 return NULL;
22409}
22410
22411#endif /* FEAT_CMDL_COMPL */
22412
22413/*
22414 * Copy the function name of "fp" to buffer "buf".
22415 * "buf" must be able to hold the function name plus three bytes.
22416 * Takes care of script-local function names.
22417 */
22418 static void
22419cat_func_name(buf, fp)
22420 char_u *buf;
22421 ufunc_T *fp;
22422{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022423 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424 {
22425 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022426 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427 }
22428 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022429 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430}
22431
22432/*
22433 * ":delfunction {name}"
22434 */
22435 void
22436ex_delfunction(eap)
22437 exarg_T *eap;
22438{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022439 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022440 char_u *p;
22441 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022442 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443
22444 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022445 name = trans_function_name(&p, eap->skip, 0, &fudi);
22446 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022448 {
22449 if (fudi.fd_dict != NULL && !eap->skip)
22450 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022451 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022453 if (!ends_excmd(*skipwhite(p)))
22454 {
22455 vim_free(name);
22456 EMSG(_(e_trailing));
22457 return;
22458 }
22459 eap->nextcmd = check_nextcmd(p);
22460 if (eap->nextcmd != NULL)
22461 *p = NUL;
22462
22463 if (!eap->skip)
22464 fp = find_func(name);
22465 vim_free(name);
22466
22467 if (!eap->skip)
22468 {
22469 if (fp == NULL)
22470 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022471 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472 return;
22473 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022474 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022475 {
22476 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22477 return;
22478 }
22479
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022480 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022482 /* Delete the dict item that refers to the function, it will
22483 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022484 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022486 else
22487 func_free(fp);
22488 }
22489}
22490
22491/*
22492 * Free a function and remove it from the list of functions.
22493 */
22494 static void
22495func_free(fp)
22496 ufunc_T *fp;
22497{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022498 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022499
22500 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022501 ga_clear_strings(&(fp->uf_args));
22502 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022503#ifdef FEAT_PROFILE
22504 vim_free(fp->uf_tml_count);
22505 vim_free(fp->uf_tml_total);
22506 vim_free(fp->uf_tml_self);
22507#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022508
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022509 /* remove the function from the function hashtable */
22510 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22511 if (HASHITEM_EMPTY(hi))
22512 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022513 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022514 hash_remove(&func_hashtab, hi);
22515
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022516 vim_free(fp);
22517}
22518
22519/*
22520 * Unreference a Function: decrement the reference count and free it when it
22521 * becomes zero. Only for numbered functions.
22522 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022523 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022524func_unref(name)
22525 char_u *name;
22526{
22527 ufunc_T *fp;
22528
22529 if (name != NULL && isdigit(*name))
22530 {
22531 fp = find_func(name);
22532 if (fp == NULL)
22533 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022534 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022535 {
22536 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022537 * when "uf_calls" becomes zero. */
22538 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022539 func_free(fp);
22540 }
22541 }
22542}
22543
22544/*
22545 * Count a reference to a Function.
22546 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022547 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022548func_ref(name)
22549 char_u *name;
22550{
22551 ufunc_T *fp;
22552
22553 if (name != NULL && isdigit(*name))
22554 {
22555 fp = find_func(name);
22556 if (fp == NULL)
22557 EMSG2(_(e_intern2), "func_ref()");
22558 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022559 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022560 }
22561}
22562
22563/*
22564 * Call a user function.
22565 */
22566 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022567call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 ufunc_T *fp; /* pointer to function */
22569 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022570 typval_T *argvars; /* arguments */
22571 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022572 linenr_T firstline; /* first line of range */
22573 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022574 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022575{
Bram Moolenaar33570922005-01-25 22:26:29 +000022576 char_u *save_sourcing_name;
22577 linenr_T save_sourcing_lnum;
22578 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022579 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022580 int save_did_emsg;
22581 static int depth = 0;
22582 dictitem_T *v;
22583 int fixvar_idx = 0; /* index in fixvar[] */
22584 int i;
22585 int ai;
22586 char_u numbuf[NUMBUFLEN];
22587 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022588#ifdef FEAT_PROFILE
22589 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022590 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022592
22593 /* If depth of calling is getting too high, don't execute the function */
22594 if (depth >= p_mfd)
22595 {
22596 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022597 rettv->v_type = VAR_NUMBER;
22598 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599 return;
22600 }
22601 ++depth;
22602
22603 line_breakcheck(); /* check for CTRL-C hit */
22604
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022605 fc = (funccall_T *)alloc(sizeof(funccall_T));
22606 fc->caller = current_funccal;
22607 current_funccal = fc;
22608 fc->func = fp;
22609 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022610 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022611 fc->linenr = 0;
22612 fc->returned = FALSE;
22613 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022614 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022615 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22616 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617
Bram Moolenaar33570922005-01-25 22:26:29 +000022618 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022619 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022620 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22621 * each argument variable and saves a lot of time.
22622 */
22623 /*
22624 * Init l: variables.
22625 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022626 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022627 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022628 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022629 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22630 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022631 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022632 name = v->di_key;
22633 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022634 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022635 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022636 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022637 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022638 v->di_tv.vval.v_dict = selfdict;
22639 ++selfdict->dv_refcount;
22640 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022641
Bram Moolenaar33570922005-01-25 22:26:29 +000022642 /*
22643 * Init a: variables.
22644 * Set a:0 to "argcount".
22645 * Set a:000 to a list with room for the "..." arguments.
22646 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022647 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022648 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022649 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022650 /* Use "name" to avoid a warning from some compiler that checks the
22651 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022652 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022653 name = v->di_key;
22654 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022655 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022656 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022657 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022658 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022659 v->di_tv.vval.v_list = &fc->l_varlist;
22660 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22661 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22662 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022663
22664 /*
22665 * Set a:firstline to "firstline" and a:lastline to "lastline".
22666 * Set a:name to named arguments.
22667 * Set a:N to the "..." arguments.
22668 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022669 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022670 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022671 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022672 (varnumber_T)lastline);
22673 for (i = 0; i < argcount; ++i)
22674 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022675 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022676 if (ai < 0)
22677 /* named argument a:name */
22678 name = FUNCARG(fp, i);
22679 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022680 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022681 /* "..." argument a:1, a:2, etc. */
22682 sprintf((char *)numbuf, "%d", ai + 1);
22683 name = numbuf;
22684 }
22685 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22686 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022687 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022688 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22689 }
22690 else
22691 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022692 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22693 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022694 if (v == NULL)
22695 break;
22696 v->di_flags = DI_FLAGS_RO;
22697 }
22698 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022699 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022700
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022701 /* Note: the values are copied directly to avoid alloc/free.
22702 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022703 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022704 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022705
22706 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22707 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022708 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22709 fc->l_listitems[ai].li_tv = argvars[i];
22710 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022711 }
22712 }
22713
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 /* Don't redraw while executing the function. */
22715 ++RedrawingDisabled;
22716 save_sourcing_name = sourcing_name;
22717 save_sourcing_lnum = sourcing_lnum;
22718 sourcing_lnum = 1;
22719 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022720 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 if (sourcing_name != NULL)
22722 {
22723 if (save_sourcing_name != NULL
22724 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22725 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22726 else
22727 STRCPY(sourcing_name, "function ");
22728 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22729
22730 if (p_verbose >= 12)
22731 {
22732 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022733 verbose_enter_scroll();
22734
Bram Moolenaar555b2802005-05-19 21:08:39 +000022735 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736 if (p_verbose >= 14)
22737 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022739 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022740 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022741 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742
22743 msg_puts((char_u *)"(");
22744 for (i = 0; i < argcount; ++i)
22745 {
22746 if (i > 0)
22747 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022748 if (argvars[i].v_type == VAR_NUMBER)
22749 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 else
22751 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022752 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22753 if (s != NULL)
22754 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022755 if (vim_strsize(s) > MSG_BUF_CLEN)
22756 {
22757 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22758 s = buf;
22759 }
22760 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022761 vim_free(tofree);
22762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022763 }
22764 }
22765 msg_puts((char_u *)")");
22766 }
22767 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022768
22769 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770 --no_wait_return;
22771 }
22772 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022773#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022774 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022775 {
22776 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22777 func_do_profile(fp);
22778 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022779 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022780 {
22781 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022782 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022783 profile_zero(&fp->uf_tm_children);
22784 }
22785 script_prof_save(&wait_start);
22786 }
22787#endif
22788
Bram Moolenaar071d4272004-06-13 20:20:40 +000022789 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022790 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791 save_did_emsg = did_emsg;
22792 did_emsg = FALSE;
22793
22794 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022795 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22797
22798 --RedrawingDisabled;
22799
22800 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022801 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022802 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022803 clear_tv(rettv);
22804 rettv->v_type = VAR_NUMBER;
22805 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022806 }
22807
Bram Moolenaar05159a02005-02-26 23:04:13 +000022808#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022809 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022810 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022811 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022812 profile_end(&call_start);
22813 profile_sub_wait(&wait_start, &call_start);
22814 profile_add(&fp->uf_tm_total, &call_start);
22815 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022816 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022817 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022818 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22819 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022820 }
22821 }
22822#endif
22823
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824 /* when being verbose, mention the return value */
22825 if (p_verbose >= 12)
22826 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022827 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022828 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022831 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022832 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022833 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022834 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022835 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022836 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022837 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022838 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022839 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022840 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022841
Bram Moolenaar555b2802005-05-19 21:08:39 +000022842 /* The value may be very long. Skip the middle part, so that we
22843 * have some idea how it starts and ends. smsg() would always
22844 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022845 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022846 if (s != NULL)
22847 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022848 if (vim_strsize(s) > MSG_BUF_CLEN)
22849 {
22850 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22851 s = buf;
22852 }
22853 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022854 vim_free(tofree);
22855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856 }
22857 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022858
22859 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022860 --no_wait_return;
22861 }
22862
22863 vim_free(sourcing_name);
22864 sourcing_name = save_sourcing_name;
22865 sourcing_lnum = save_sourcing_lnum;
22866 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022867#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022868 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022869 script_prof_restore(&wait_start);
22870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871
22872 if (p_verbose >= 12 && sourcing_name != NULL)
22873 {
22874 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022875 verbose_enter_scroll();
22876
Bram Moolenaar555b2802005-05-19 21:08:39 +000022877 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022879
22880 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022881 --no_wait_return;
22882 }
22883
22884 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022885 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022886 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022887
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022888 /* If the a:000 list and the l: and a: dicts are not referenced we can
22889 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022890 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22891 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22892 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22893 {
22894 free_funccal(fc, FALSE);
22895 }
22896 else
22897 {
22898 hashitem_T *hi;
22899 listitem_T *li;
22900 int todo;
22901
22902 /* "fc" is still in use. This can happen when returning "a:000" or
22903 * assigning "l:" to a global variable.
22904 * Link "fc" in the list for garbage collection later. */
22905 fc->caller = previous_funccal;
22906 previous_funccal = fc;
22907
22908 /* Make a copy of the a: variables, since we didn't do that above. */
22909 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22910 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22911 {
22912 if (!HASHITEM_EMPTY(hi))
22913 {
22914 --todo;
22915 v = HI2DI(hi);
22916 copy_tv(&v->di_tv, &v->di_tv);
22917 }
22918 }
22919
22920 /* Make a copy of the a:000 items, since we didn't do that above. */
22921 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22922 copy_tv(&li->li_tv, &li->li_tv);
22923 }
22924}
22925
22926/*
22927 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022928 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022929 */
22930 static int
22931can_free_funccal(fc, copyID)
22932 funccall_T *fc;
22933 int copyID;
22934{
22935 return (fc->l_varlist.lv_copyID != copyID
22936 && fc->l_vars.dv_copyID != copyID
22937 && fc->l_avars.dv_copyID != copyID);
22938}
22939
22940/*
22941 * Free "fc" and what it contains.
22942 */
22943 static void
22944free_funccal(fc, free_val)
22945 funccall_T *fc;
22946 int free_val; /* a: vars were allocated */
22947{
22948 listitem_T *li;
22949
22950 /* The a: variables typevals may not have been allocated, only free the
22951 * allocated variables. */
22952 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22953
22954 /* free all l: variables */
22955 vars_clear(&fc->l_vars.dv_hashtab);
22956
22957 /* Free the a:000 variables if they were allocated. */
22958 if (free_val)
22959 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22960 clear_tv(&li->li_tv);
22961
22962 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022963}
22964
22965/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022966 * Add a number variable "name" to dict "dp" with value "nr".
22967 */
22968 static void
22969add_nr_var(dp, v, name, nr)
22970 dict_T *dp;
22971 dictitem_T *v;
22972 char *name;
22973 varnumber_T nr;
22974{
22975 STRCPY(v->di_key, name);
22976 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22977 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22978 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022979 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022980 v->di_tv.vval.v_number = nr;
22981}
22982
22983/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022984 * ":return [expr]"
22985 */
22986 void
22987ex_return(eap)
22988 exarg_T *eap;
22989{
22990 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022991 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022992 int returning = FALSE;
22993
22994 if (current_funccal == NULL)
22995 {
22996 EMSG(_("E133: :return not inside a function"));
22997 return;
22998 }
22999
23000 if (eap->skip)
23001 ++emsg_skip;
23002
23003 eap->nextcmd = NULL;
23004 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023005 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 {
23007 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023008 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023009 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023010 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023011 }
23012 /* It's safer to return also on error. */
23013 else if (!eap->skip)
23014 {
23015 /*
23016 * Return unless the expression evaluation has been cancelled due to an
23017 * aborting error, an interrupt, or an exception.
23018 */
23019 if (!aborting())
23020 returning = do_return(eap, FALSE, TRUE, NULL);
23021 }
23022
23023 /* When skipping or the return gets pending, advance to the next command
23024 * in this line (!returning). Otherwise, ignore the rest of the line.
23025 * Following lines will be ignored by get_func_line(). */
23026 if (returning)
23027 eap->nextcmd = NULL;
23028 else if (eap->nextcmd == NULL) /* no argument */
23029 eap->nextcmd = check_nextcmd(arg);
23030
23031 if (eap->skip)
23032 --emsg_skip;
23033}
23034
23035/*
23036 * Return from a function. Possibly makes the return pending. Also called
23037 * for a pending return at the ":endtry" or after returning from an extra
23038 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023039 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023040 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023041 * FALSE when the return gets pending.
23042 */
23043 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023044do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023045 exarg_T *eap;
23046 int reanimate;
23047 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023048 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049{
23050 int idx;
23051 struct condstack *cstack = eap->cstack;
23052
23053 if (reanimate)
23054 /* Undo the return. */
23055 current_funccal->returned = FALSE;
23056
23057 /*
23058 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23059 * not in its finally clause (which then is to be executed next) is found.
23060 * In this case, make the ":return" pending for execution at the ":endtry".
23061 * Otherwise, return normally.
23062 */
23063 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23064 if (idx >= 0)
23065 {
23066 cstack->cs_pending[idx] = CSTP_RETURN;
23067
23068 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023069 /* A pending return again gets pending. "rettv" points to an
23070 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023072 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023073 else
23074 {
23075 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023076 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023077 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023078 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023080 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 {
23082 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023083 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023084 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085 else
23086 EMSG(_(e_outofmem));
23087 }
23088 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023089 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090
23091 if (reanimate)
23092 {
23093 /* The pending return value could be overwritten by a ":return"
23094 * without argument in a finally clause; reset the default
23095 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023096 current_funccal->rettv->v_type = VAR_NUMBER;
23097 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023098 }
23099 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023100 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023101 }
23102 else
23103 {
23104 current_funccal->returned = TRUE;
23105
23106 /* If the return is carried out now, store the return value. For
23107 * a return immediately after reanimation, the value is already
23108 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023109 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023111 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023112 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023114 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115 }
23116 }
23117
23118 return idx < 0;
23119}
23120
23121/*
23122 * Free the variable with a pending return value.
23123 */
23124 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023125discard_pending_return(rettv)
23126 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023127{
Bram Moolenaar33570922005-01-25 22:26:29 +000023128 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023129}
23130
23131/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023132 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023133 * is an allocated string. Used by report_pending() for verbose messages.
23134 */
23135 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023136get_return_cmd(rettv)
23137 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023139 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023140 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023141 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023143 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023144 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023145 if (s == NULL)
23146 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023147
23148 STRCPY(IObuff, ":return ");
23149 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23150 if (STRLEN(s) + 8 >= IOSIZE)
23151 STRCPY(IObuff + IOSIZE - 4, "...");
23152 vim_free(tofree);
23153 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154}
23155
23156/*
23157 * Get next function line.
23158 * Called by do_cmdline() to get the next line.
23159 * Returns allocated string, or NULL for end of function.
23160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161 char_u *
23162get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023163 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023165 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166{
Bram Moolenaar33570922005-01-25 22:26:29 +000023167 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023168 ufunc_T *fp = fcp->func;
23169 char_u *retval;
23170 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171
23172 /* If breakpoints have been added/deleted need to check for it. */
23173 if (fcp->dbg_tick != debug_tick)
23174 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023175 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176 sourcing_lnum);
23177 fcp->dbg_tick = debug_tick;
23178 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023179#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023180 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023181 func_line_end(cookie);
23182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183
Bram Moolenaar05159a02005-02-26 23:04:13 +000023184 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023185 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23186 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 retval = NULL;
23188 else
23189 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023190 /* Skip NULL lines (continuation lines). */
23191 while (fcp->linenr < gap->ga_len
23192 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23193 ++fcp->linenr;
23194 if (fcp->linenr >= gap->ga_len)
23195 retval = NULL;
23196 else
23197 {
23198 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23199 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023200#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023201 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023202 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023203#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205 }
23206
23207 /* Did we encounter a breakpoint? */
23208 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23209 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023210 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023212 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023213 sourcing_lnum);
23214 fcp->dbg_tick = debug_tick;
23215 }
23216
23217 return retval;
23218}
23219
Bram Moolenaar05159a02005-02-26 23:04:13 +000023220#if defined(FEAT_PROFILE) || defined(PROTO)
23221/*
23222 * Called when starting to read a function line.
23223 * "sourcing_lnum" must be correct!
23224 * When skipping lines it may not actually be executed, but we won't find out
23225 * until later and we need to store the time now.
23226 */
23227 void
23228func_line_start(cookie)
23229 void *cookie;
23230{
23231 funccall_T *fcp = (funccall_T *)cookie;
23232 ufunc_T *fp = fcp->func;
23233
23234 if (fp->uf_profiling && sourcing_lnum >= 1
23235 && sourcing_lnum <= fp->uf_lines.ga_len)
23236 {
23237 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023238 /* Skip continuation lines. */
23239 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23240 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023241 fp->uf_tml_execed = FALSE;
23242 profile_start(&fp->uf_tml_start);
23243 profile_zero(&fp->uf_tml_children);
23244 profile_get_wait(&fp->uf_tml_wait);
23245 }
23246}
23247
23248/*
23249 * Called when actually executing a function line.
23250 */
23251 void
23252func_line_exec(cookie)
23253 void *cookie;
23254{
23255 funccall_T *fcp = (funccall_T *)cookie;
23256 ufunc_T *fp = fcp->func;
23257
23258 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23259 fp->uf_tml_execed = TRUE;
23260}
23261
23262/*
23263 * Called when done with a function line.
23264 */
23265 void
23266func_line_end(cookie)
23267 void *cookie;
23268{
23269 funccall_T *fcp = (funccall_T *)cookie;
23270 ufunc_T *fp = fcp->func;
23271
23272 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23273 {
23274 if (fp->uf_tml_execed)
23275 {
23276 ++fp->uf_tml_count[fp->uf_tml_idx];
23277 profile_end(&fp->uf_tml_start);
23278 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023279 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023280 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23281 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023282 }
23283 fp->uf_tml_idx = -1;
23284 }
23285}
23286#endif
23287
Bram Moolenaar071d4272004-06-13 20:20:40 +000023288/*
23289 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023290 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023291 */
23292 int
23293func_has_ended(cookie)
23294 void *cookie;
23295{
Bram Moolenaar33570922005-01-25 22:26:29 +000023296 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023297
23298 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23299 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023300 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301 || fcp->returned);
23302}
23303
23304/*
23305 * return TRUE if cookie indicates a function which "abort"s on errors.
23306 */
23307 int
23308func_has_abort(cookie)
23309 void *cookie;
23310{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023311 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312}
23313
23314#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23315typedef enum
23316{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023317 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23318 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23319 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023320} var_flavour_T;
23321
23322static var_flavour_T var_flavour __ARGS((char_u *varname));
23323
23324 static var_flavour_T
23325var_flavour(varname)
23326 char_u *varname;
23327{
23328 char_u *p = varname;
23329
23330 if (ASCII_ISUPPER(*p))
23331 {
23332 while (*(++p))
23333 if (ASCII_ISLOWER(*p))
23334 return VAR_FLAVOUR_SESSION;
23335 return VAR_FLAVOUR_VIMINFO;
23336 }
23337 else
23338 return VAR_FLAVOUR_DEFAULT;
23339}
23340#endif
23341
23342#if defined(FEAT_VIMINFO) || defined(PROTO)
23343/*
23344 * Restore global vars that start with a capital from the viminfo file
23345 */
23346 int
23347read_viminfo_varlist(virp, writing)
23348 vir_T *virp;
23349 int writing;
23350{
23351 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023352 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023353 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354
23355 if (!writing && (find_viminfo_parameter('!') != NULL))
23356 {
23357 tab = vim_strchr(virp->vir_line + 1, '\t');
23358 if (tab != NULL)
23359 {
23360 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023361 switch (*tab)
23362 {
23363 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023364#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023365 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023366#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023367 case 'D': type = VAR_DICT; break;
23368 case 'L': type = VAR_LIST; break;
23369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370
23371 tab = vim_strchr(tab, '\t');
23372 if (tab != NULL)
23373 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023374 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023375 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023376 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023377 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023378#ifdef FEAT_FLOAT
23379 else if (type == VAR_FLOAT)
23380 (void)string2float(tab + 1, &tv.vval.v_float);
23381#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023382 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023383 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023384 if (type == VAR_DICT || type == VAR_LIST)
23385 {
23386 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23387
23388 if (etv == NULL)
23389 /* Failed to parse back the dict or list, use it as a
23390 * string. */
23391 tv.v_type = VAR_STRING;
23392 else
23393 {
23394 vim_free(tv.vval.v_string);
23395 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023396 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023397 }
23398 }
23399
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023400 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023401
23402 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023403 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023404 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23405 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406 }
23407 }
23408 }
23409
23410 return viminfo_readline(virp);
23411}
23412
23413/*
23414 * Write global vars that start with a capital to the viminfo file
23415 */
23416 void
23417write_viminfo_varlist(fp)
23418 FILE *fp;
23419{
Bram Moolenaar33570922005-01-25 22:26:29 +000023420 hashitem_T *hi;
23421 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023422 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023423 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023424 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023425 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023426 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023427
23428 if (find_viminfo_parameter('!') == NULL)
23429 return;
23430
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023431 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023432
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023433 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023434 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023435 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023436 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023438 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023439 this_var = HI2DI(hi);
23440 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023441 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023442 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023443 {
23444 case VAR_STRING: s = "STR"; break;
23445 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023446#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023447 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023448#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023449 case VAR_DICT: s = "DIC"; break;
23450 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023451 default: continue;
23452 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023453 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023454 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023455 if (p != NULL)
23456 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023457 vim_free(tofree);
23458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023459 }
23460 }
23461}
23462#endif
23463
23464#if defined(FEAT_SESSION) || defined(PROTO)
23465 int
23466store_session_globals(fd)
23467 FILE *fd;
23468{
Bram Moolenaar33570922005-01-25 22:26:29 +000023469 hashitem_T *hi;
23470 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023471 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023472 char_u *p, *t;
23473
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023474 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023475 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023477 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023479 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023480 this_var = HI2DI(hi);
23481 if ((this_var->di_tv.v_type == VAR_NUMBER
23482 || this_var->di_tv.v_type == VAR_STRING)
23483 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023484 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023485 /* Escape special characters with a backslash. Turn a LF and
23486 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023487 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023488 (char_u *)"\\\"\n\r");
23489 if (p == NULL) /* out of memory */
23490 break;
23491 for (t = p; *t != NUL; ++t)
23492 if (*t == '\n')
23493 *t = 'n';
23494 else if (*t == '\r')
23495 *t = 'r';
23496 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023497 this_var->di_key,
23498 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23499 : ' ',
23500 p,
23501 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23502 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023503 || put_eol(fd) == FAIL)
23504 {
23505 vim_free(p);
23506 return FAIL;
23507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023508 vim_free(p);
23509 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023510#ifdef FEAT_FLOAT
23511 else if (this_var->di_tv.v_type == VAR_FLOAT
23512 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23513 {
23514 float_T f = this_var->di_tv.vval.v_float;
23515 int sign = ' ';
23516
23517 if (f < 0)
23518 {
23519 f = -f;
23520 sign = '-';
23521 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023522 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023523 this_var->di_key, sign, f) < 0)
23524 || put_eol(fd) == FAIL)
23525 return FAIL;
23526 }
23527#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023528 }
23529 }
23530 return OK;
23531}
23532#endif
23533
Bram Moolenaar661b1822005-07-28 22:36:45 +000023534/*
23535 * Display script name where an item was last set.
23536 * Should only be invoked when 'verbose' is non-zero.
23537 */
23538 void
23539last_set_msg(scriptID)
23540 scid_T scriptID;
23541{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023542 char_u *p;
23543
Bram Moolenaar661b1822005-07-28 22:36:45 +000023544 if (scriptID != 0)
23545 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023546 p = home_replace_save(NULL, get_scriptname(scriptID));
23547 if (p != NULL)
23548 {
23549 verbose_enter();
23550 MSG_PUTS(_("\n\tLast set from "));
23551 MSG_PUTS(p);
23552 vim_free(p);
23553 verbose_leave();
23554 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023555 }
23556}
23557
Bram Moolenaard812df62008-11-09 12:46:09 +000023558/*
23559 * List v:oldfiles in a nice way.
23560 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023561 void
23562ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023563 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023564{
23565 list_T *l = vimvars[VV_OLDFILES].vv_list;
23566 listitem_T *li;
23567 int nr = 0;
23568
23569 if (l == NULL)
23570 msg((char_u *)_("No old files"));
23571 else
23572 {
23573 msg_start();
23574 msg_scroll = TRUE;
23575 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23576 {
23577 msg_outnum((long)++nr);
23578 MSG_PUTS(": ");
23579 msg_outtrans(get_tv_string(&li->li_tv));
23580 msg_putchar('\n');
23581 out_flush(); /* output one line at a time */
23582 ui_breakcheck();
23583 }
23584 /* Assume "got_int" was set to truncate the listing. */
23585 got_int = FALSE;
23586
23587#ifdef FEAT_BROWSE_CMD
23588 if (cmdmod.browse)
23589 {
23590 quit_more = FALSE;
23591 nr = prompt_for_number(FALSE);
23592 msg_starthere();
23593 if (nr > 0)
23594 {
23595 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23596 (long)nr);
23597
23598 if (p != NULL)
23599 {
23600 p = expand_env_save(p);
23601 eap->arg = p;
23602 eap->cmdidx = CMD_edit;
23603 cmdmod.browse = FALSE;
23604 do_exedit(eap, NULL);
23605 vim_free(p);
23606 }
23607 }
23608 }
23609#endif
23610 }
23611}
23612
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613#endif /* FEAT_EVAL */
23614
Bram Moolenaar071d4272004-06-13 20:20:40 +000023615
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023616#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617
23618#ifdef WIN3264
23619/*
23620 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23621 */
23622static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23623static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23624static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23625
23626/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023627 * Get the short path (8.3) for the filename in "fnamep".
23628 * Only works for a valid file name.
23629 * When the path gets longer "fnamep" is changed and the allocated buffer
23630 * is put in "bufp".
23631 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23632 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633 */
23634 static int
23635get_short_pathname(fnamep, bufp, fnamelen)
23636 char_u **fnamep;
23637 char_u **bufp;
23638 int *fnamelen;
23639{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023640 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023641 char_u *newbuf;
23642
23643 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023644 l = GetShortPathName(*fnamep, *fnamep, len);
23645 if (l > len - 1)
23646 {
23647 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023648 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 newbuf = vim_strnsave(*fnamep, l);
23650 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023651 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652
23653 vim_free(*bufp);
23654 *fnamep = *bufp = newbuf;
23655
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023656 /* Really should always succeed, as the buffer is big enough. */
23657 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 }
23659
23660 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023661 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023662}
23663
23664/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023665 * Get the short path (8.3) for the filename in "fname". The converted
23666 * path is returned in "bufp".
23667 *
23668 * Some of the directories specified in "fname" may not exist. This function
23669 * will shorten the existing directories at the beginning of the path and then
23670 * append the remaining non-existing path.
23671 *
23672 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023673 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023674 * bufp - Pointer to an allocated buffer for the filename.
23675 * fnamelen - Length of the filename pointed to by fname
23676 *
23677 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023678 */
23679 static int
23680shortpath_for_invalid_fname(fname, bufp, fnamelen)
23681 char_u **fname;
23682 char_u **bufp;
23683 int *fnamelen;
23684{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023685 char_u *short_fname, *save_fname, *pbuf_unused;
23686 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023687 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023688 int old_len, len;
23689 int new_len, sfx_len;
23690 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023691
23692 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023693 old_len = *fnamelen;
23694 save_fname = vim_strnsave(*fname, old_len);
23695 pbuf_unused = NULL;
23696 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023697
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023698 endp = save_fname + old_len - 1; /* Find the end of the copy */
23699 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023700
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023701 /*
23702 * Try shortening the supplied path till it succeeds by removing one
23703 * directory at a time from the tail of the path.
23704 */
23705 len = 0;
23706 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023708 /* go back one path-separator */
23709 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23710 --endp;
23711 if (endp <= save_fname)
23712 break; /* processed the complete path */
23713
23714 /*
23715 * Replace the path separator with a NUL and try to shorten the
23716 * resulting path.
23717 */
23718 ch = *endp;
23719 *endp = 0;
23720 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023721 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023722 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23723 {
23724 retval = FAIL;
23725 goto theend;
23726 }
23727 *endp = ch; /* preserve the string */
23728
23729 if (len > 0)
23730 break; /* successfully shortened the path */
23731
23732 /* failed to shorten the path. Skip the path separator */
23733 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734 }
23735
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023736 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023737 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023738 /*
23739 * Succeeded in shortening the path. Now concatenate the shortened
23740 * path with the remaining path at the tail.
23741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023742
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023743 /* Compute the length of the new path. */
23744 sfx_len = (int)(save_endp - endp) + 1;
23745 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023746
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023747 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023748 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023749 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023750 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023751 /* There is not enough space in the currently allocated string,
23752 * copy it to a buffer big enough. */
23753 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023754 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023755 {
23756 retval = FAIL;
23757 goto theend;
23758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759 }
23760 else
23761 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023762 /* Transfer short_fname to the main buffer (it's big enough),
23763 * unless get_short_pathname() did its work in-place. */
23764 *fname = *bufp = save_fname;
23765 if (short_fname != save_fname)
23766 vim_strncpy(save_fname, short_fname, len);
23767 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023768 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023769
23770 /* concat the not-shortened part of the path */
23771 vim_strncpy(*fname + len, endp, sfx_len);
23772 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023774
23775theend:
23776 vim_free(pbuf_unused);
23777 vim_free(save_fname);
23778
23779 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023780}
23781
23782/*
23783 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023784 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785 */
23786 static int
23787shortpath_for_partial(fnamep, bufp, fnamelen)
23788 char_u **fnamep;
23789 char_u **bufp;
23790 int *fnamelen;
23791{
23792 int sepcount, len, tflen;
23793 char_u *p;
23794 char_u *pbuf, *tfname;
23795 int hasTilde;
23796
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023797 /* Count up the path separators from the RHS.. so we know which part
23798 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023799 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023800 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023801 if (vim_ispathsep(*p))
23802 ++sepcount;
23803
23804 /* Need full path first (use expand_env() to remove a "~/") */
23805 hasTilde = (**fnamep == '~');
23806 if (hasTilde)
23807 pbuf = tfname = expand_env_save(*fnamep);
23808 else
23809 pbuf = tfname = FullName_save(*fnamep, FALSE);
23810
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023811 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023812
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023813 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23814 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023815
23816 if (len == 0)
23817 {
23818 /* Don't have a valid filename, so shorten the rest of the
23819 * path if we can. This CAN give us invalid 8.3 filenames, but
23820 * there's not a lot of point in guessing what it might be.
23821 */
23822 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023823 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23824 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023825 }
23826
23827 /* Count the paths backward to find the beginning of the desired string. */
23828 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023829 {
23830#ifdef FEAT_MBYTE
23831 if (has_mbyte)
23832 p -= mb_head_off(tfname, p);
23833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834 if (vim_ispathsep(*p))
23835 {
23836 if (sepcount == 0 || (hasTilde && sepcount == 1))
23837 break;
23838 else
23839 sepcount --;
23840 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 if (hasTilde)
23843 {
23844 --p;
23845 if (p >= tfname)
23846 *p = '~';
23847 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023848 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849 }
23850 else
23851 ++p;
23852
23853 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23854 vim_free(*bufp);
23855 *fnamelen = (int)STRLEN(p);
23856 *bufp = pbuf;
23857 *fnamep = p;
23858
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023859 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860}
23861#endif /* WIN3264 */
23862
23863/*
23864 * Adjust a filename, according to a string of modifiers.
23865 * *fnamep must be NUL terminated when called. When returning, the length is
23866 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023867 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023868 * When there is an error, *fnamep is set to NULL.
23869 */
23870 int
23871modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23872 char_u *src; /* string with modifiers */
23873 int *usedlen; /* characters after src that are used */
23874 char_u **fnamep; /* file name so far */
23875 char_u **bufp; /* buffer for allocated file name or NULL */
23876 int *fnamelen; /* length of fnamep */
23877{
23878 int valid = 0;
23879 char_u *tail;
23880 char_u *s, *p, *pbuf;
23881 char_u dirname[MAXPATHL];
23882 int c;
23883 int has_fullname = 0;
23884#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023885 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 int has_shortname = 0;
23887#endif
23888
23889repeat:
23890 /* ":p" - full path/file_name */
23891 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23892 {
23893 has_fullname = 1;
23894
23895 valid |= VALID_PATH;
23896 *usedlen += 2;
23897
23898 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23899 if ((*fnamep)[0] == '~'
23900#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23901 && ((*fnamep)[1] == '/'
23902# ifdef BACKSLASH_IN_FILENAME
23903 || (*fnamep)[1] == '\\'
23904# endif
23905 || (*fnamep)[1] == NUL)
23906
23907#endif
23908 )
23909 {
23910 *fnamep = expand_env_save(*fnamep);
23911 vim_free(*bufp); /* free any allocated file name */
23912 *bufp = *fnamep;
23913 if (*fnamep == NULL)
23914 return -1;
23915 }
23916
23917 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023918 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023919 {
23920 if (vim_ispathsep(*p)
23921 && p[1] == '.'
23922 && (p[2] == NUL
23923 || vim_ispathsep(p[2])
23924 || (p[2] == '.'
23925 && (p[3] == NUL || vim_ispathsep(p[3])))))
23926 break;
23927 }
23928
23929 /* FullName_save() is slow, don't use it when not needed. */
23930 if (*p != NUL || !vim_isAbsName(*fnamep))
23931 {
23932 *fnamep = FullName_save(*fnamep, *p != NUL);
23933 vim_free(*bufp); /* free any allocated file name */
23934 *bufp = *fnamep;
23935 if (*fnamep == NULL)
23936 return -1;
23937 }
23938
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023939#ifdef WIN3264
23940# if _WIN32_WINNT >= 0x0500
23941 if (vim_strchr(*fnamep, '~') != NULL)
23942 {
23943 /* Expand 8.3 filename to full path. Needed to make sure the same
23944 * file does not have two different names.
23945 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23946 p = alloc(_MAX_PATH + 1);
23947 if (p != NULL)
23948 {
23949 if (GetLongPathName(*fnamep, p, MAXPATHL))
23950 {
23951 vim_free(*bufp);
23952 *bufp = *fnamep = p;
23953 }
23954 else
23955 vim_free(p);
23956 }
23957 }
23958# endif
23959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023960 /* Append a path separator to a directory. */
23961 if (mch_isdir(*fnamep))
23962 {
23963 /* Make room for one or two extra characters. */
23964 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23965 vim_free(*bufp); /* free any allocated file name */
23966 *bufp = *fnamep;
23967 if (*fnamep == NULL)
23968 return -1;
23969 add_pathsep(*fnamep);
23970 }
23971 }
23972
23973 /* ":." - path relative to the current directory */
23974 /* ":~" - path relative to the home directory */
23975 /* ":8" - shortname path - postponed till after */
23976 while (src[*usedlen] == ':'
23977 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23978 {
23979 *usedlen += 2;
23980 if (c == '8')
23981 {
23982#ifdef WIN3264
23983 has_shortname = 1; /* Postpone this. */
23984#endif
23985 continue;
23986 }
23987 pbuf = NULL;
23988 /* Need full path first (use expand_env() to remove a "~/") */
23989 if (!has_fullname)
23990 {
23991 if (c == '.' && **fnamep == '~')
23992 p = pbuf = expand_env_save(*fnamep);
23993 else
23994 p = pbuf = FullName_save(*fnamep, FALSE);
23995 }
23996 else
23997 p = *fnamep;
23998
23999 has_fullname = 0;
24000
24001 if (p != NULL)
24002 {
24003 if (c == '.')
24004 {
24005 mch_dirname(dirname, MAXPATHL);
24006 s = shorten_fname(p, dirname);
24007 if (s != NULL)
24008 {
24009 *fnamep = s;
24010 if (pbuf != NULL)
24011 {
24012 vim_free(*bufp); /* free any allocated file name */
24013 *bufp = pbuf;
24014 pbuf = NULL;
24015 }
24016 }
24017 }
24018 else
24019 {
24020 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24021 /* Only replace it when it starts with '~' */
24022 if (*dirname == '~')
24023 {
24024 s = vim_strsave(dirname);
24025 if (s != NULL)
24026 {
24027 *fnamep = s;
24028 vim_free(*bufp);
24029 *bufp = s;
24030 }
24031 }
24032 }
24033 vim_free(pbuf);
24034 }
24035 }
24036
24037 tail = gettail(*fnamep);
24038 *fnamelen = (int)STRLEN(*fnamep);
24039
24040 /* ":h" - head, remove "/file_name", can be repeated */
24041 /* Don't remove the first "/" or "c:\" */
24042 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24043 {
24044 valid |= VALID_HEAD;
24045 *usedlen += 2;
24046 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024047 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024048 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049 *fnamelen = (int)(tail - *fnamep);
24050#ifdef VMS
24051 if (*fnamelen > 0)
24052 *fnamelen += 1; /* the path separator is part of the path */
24053#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024054 if (*fnamelen == 0)
24055 {
24056 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24057 p = vim_strsave((char_u *)".");
24058 if (p == NULL)
24059 return -1;
24060 vim_free(*bufp);
24061 *bufp = *fnamep = tail = p;
24062 *fnamelen = 1;
24063 }
24064 else
24065 {
24066 while (tail > s && !after_pathsep(s, tail))
24067 mb_ptr_back(*fnamep, tail);
24068 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024069 }
24070
24071 /* ":8" - shortname */
24072 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24073 {
24074 *usedlen += 2;
24075#ifdef WIN3264
24076 has_shortname = 1;
24077#endif
24078 }
24079
24080#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024081 /*
24082 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024083 */
24084 if (has_shortname)
24085 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024086 /* Copy the string if it is shortened by :h and when it wasn't copied
24087 * yet, because we are going to change it in place. Avoids changing
24088 * the buffer name for "%:8". */
24089 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 {
24091 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024092 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024093 return -1;
24094 vim_free(*bufp);
24095 *bufp = *fnamep = p;
24096 }
24097
24098 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024099 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024100 if (!has_fullname && !vim_isAbsName(*fnamep))
24101 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024102 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024103 return -1;
24104 }
24105 else
24106 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024107 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108
Bram Moolenaardc935552011-08-17 15:23:23 +020024109 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024110 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024111 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024112 return -1;
24113
24114 if (l == 0)
24115 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024116 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024117 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024118 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024119 return -1;
24120 }
24121 *fnamelen = l;
24122 }
24123 }
24124#endif /* WIN3264 */
24125
24126 /* ":t" - tail, just the basename */
24127 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24128 {
24129 *usedlen += 2;
24130 *fnamelen -= (int)(tail - *fnamep);
24131 *fnamep = tail;
24132 }
24133
24134 /* ":e" - extension, can be repeated */
24135 /* ":r" - root, without extension, can be repeated */
24136 while (src[*usedlen] == ':'
24137 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24138 {
24139 /* find a '.' in the tail:
24140 * - for second :e: before the current fname
24141 * - otherwise: The last '.'
24142 */
24143 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24144 s = *fnamep - 2;
24145 else
24146 s = *fnamep + *fnamelen - 1;
24147 for ( ; s > tail; --s)
24148 if (s[0] == '.')
24149 break;
24150 if (src[*usedlen + 1] == 'e') /* :e */
24151 {
24152 if (s > tail)
24153 {
24154 *fnamelen += (int)(*fnamep - (s + 1));
24155 *fnamep = s + 1;
24156#ifdef VMS
24157 /* cut version from the extension */
24158 s = *fnamep + *fnamelen - 1;
24159 for ( ; s > *fnamep; --s)
24160 if (s[0] == ';')
24161 break;
24162 if (s > *fnamep)
24163 *fnamelen = s - *fnamep;
24164#endif
24165 }
24166 else if (*fnamep <= tail)
24167 *fnamelen = 0;
24168 }
24169 else /* :r */
24170 {
24171 if (s > tail) /* remove one extension */
24172 *fnamelen = (int)(s - *fnamep);
24173 }
24174 *usedlen += 2;
24175 }
24176
24177 /* ":s?pat?foo?" - substitute */
24178 /* ":gs?pat?foo?" - global substitute */
24179 if (src[*usedlen] == ':'
24180 && (src[*usedlen + 1] == 's'
24181 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24182 {
24183 char_u *str;
24184 char_u *pat;
24185 char_u *sub;
24186 int sep;
24187 char_u *flags;
24188 int didit = FALSE;
24189
24190 flags = (char_u *)"";
24191 s = src + *usedlen + 2;
24192 if (src[*usedlen + 1] == 'g')
24193 {
24194 flags = (char_u *)"g";
24195 ++s;
24196 }
24197
24198 sep = *s++;
24199 if (sep)
24200 {
24201 /* find end of pattern */
24202 p = vim_strchr(s, sep);
24203 if (p != NULL)
24204 {
24205 pat = vim_strnsave(s, (int)(p - s));
24206 if (pat != NULL)
24207 {
24208 s = p + 1;
24209 /* find end of substitution */
24210 p = vim_strchr(s, sep);
24211 if (p != NULL)
24212 {
24213 sub = vim_strnsave(s, (int)(p - s));
24214 str = vim_strnsave(*fnamep, *fnamelen);
24215 if (sub != NULL && str != NULL)
24216 {
24217 *usedlen = (int)(p + 1 - src);
24218 s = do_string_sub(str, pat, sub, flags);
24219 if (s != NULL)
24220 {
24221 *fnamep = s;
24222 *fnamelen = (int)STRLEN(s);
24223 vim_free(*bufp);
24224 *bufp = s;
24225 didit = TRUE;
24226 }
24227 }
24228 vim_free(sub);
24229 vim_free(str);
24230 }
24231 vim_free(pat);
24232 }
24233 }
24234 /* after using ":s", repeat all the modifiers */
24235 if (didit)
24236 goto repeat;
24237 }
24238 }
24239
24240 return valid;
24241}
24242
24243/*
24244 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24245 * "flags" can be "g" to do a global substitute.
24246 * Returns an allocated string, NULL for error.
24247 */
24248 char_u *
24249do_string_sub(str, pat, sub, flags)
24250 char_u *str;
24251 char_u *pat;
24252 char_u *sub;
24253 char_u *flags;
24254{
24255 int sublen;
24256 regmatch_T regmatch;
24257 int i;
24258 int do_all;
24259 char_u *tail;
24260 garray_T ga;
24261 char_u *ret;
24262 char_u *save_cpo;
24263
24264 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24265 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024266 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024267
24268 ga_init2(&ga, 1, 200);
24269
24270 do_all = (flags[0] == 'g');
24271
24272 regmatch.rm_ic = p_ic;
24273 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24274 if (regmatch.regprog != NULL)
24275 {
24276 tail = str;
24277 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24278 {
24279 /*
24280 * Get some space for a temporary buffer to do the substitution
24281 * into. It will contain:
24282 * - The text up to where the match is.
24283 * - The substituted text.
24284 * - The text after the match.
24285 */
24286 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24287 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24288 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24289 {
24290 ga_clear(&ga);
24291 break;
24292 }
24293
24294 /* copy the text up to where the match is */
24295 i = (int)(regmatch.startp[0] - tail);
24296 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24297 /* add the substituted text */
24298 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24299 + ga.ga_len + i, TRUE, TRUE, FALSE);
24300 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024301 /* avoid getting stuck on a match with an empty string */
24302 if (tail == regmatch.endp[0])
24303 {
24304 if (*tail == NUL)
24305 break;
24306 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24307 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024308 }
24309 else
24310 {
24311 tail = regmatch.endp[0];
24312 if (*tail == NUL)
24313 break;
24314 }
24315 if (!do_all)
24316 break;
24317 }
24318
24319 if (ga.ga_data != NULL)
24320 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24321
Bram Moolenaar473de612013-06-08 18:19:48 +020024322 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024323 }
24324
24325 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24326 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024327 if (p_cpo == empty_option)
24328 p_cpo = save_cpo;
24329 else
24330 /* Darn, evaluating {sub} expression changed the value. */
24331 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024332
24333 return ret;
24334}
24335
24336#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */