blob: 14ba4d8b2d80d17bfb4c697a77bc0abdf18b5da4 [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));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100477static void byteidx __ARGS((typval_T *argvars, typval_T *rettv, int comp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000478static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +0100479static void f_byteidxcomp __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000480static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#ifdef FEAT_FLOAT
482static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
483#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000484static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000485static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
486static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000487static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000488static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000489#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000490static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000491static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
492static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
493#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000494static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
495static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000496#ifdef FEAT_FLOAT
497static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200498static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000500static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
501static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
503static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
512static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
513static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200514#ifdef FEAT_FLOAT
515static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
516#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000517static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
518static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000519static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000520static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
523static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
524static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000525#ifdef FEAT_FLOAT
526static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
527static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200528static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000530static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000531static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
537static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
538static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000539static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000541static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000542static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
545static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
546static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000547static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000548static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
553static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
554static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000555static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000556static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000557static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000558static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000559static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
560static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200561static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000562static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000563static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
568static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
569static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000570static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000571static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
582static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
583static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000584static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000585static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
587static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
588static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100589static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000591static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000592static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
601static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
602static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000603#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200604static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000605static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
606#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200607#ifdef FEAT_LUA
608static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
609#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000610static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
612static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
613static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000614static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000615static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000616static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000617static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000618static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000619static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
620static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
621static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000622#ifdef vim_mkdir
623static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
624#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000625static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100626#ifdef FEAT_MZSCHEME
627static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
628#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000629static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
630static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100631static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000632static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000633#ifdef FEAT_FLOAT
634static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
635#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000636static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000637static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000638static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200639#ifdef FEAT_PYTHON3
640static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
641#endif
642#ifdef FEAT_PYTHON
643static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000645static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000646static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000647static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000649static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
657static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
658static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000659#ifdef FEAT_FLOAT
660static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
661#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200662static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100664static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
665static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000667static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000668static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000669static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000671static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
674static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
675static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000676static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000677static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000678static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000679static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000680static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200681static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000682static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000683static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100684#ifdef FEAT_CRYPT
685static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
686#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000687static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200688static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000689static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000690#ifdef FEAT_FLOAT
691static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200692static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000693#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000694static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000695static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000696static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
697static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000698static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000699#ifdef FEAT_FLOAT
700static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
701static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
702#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000703static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200704static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000705#ifdef HAVE_STRFTIME
706static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
707#endif
708static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
712static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
713static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200714static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200715static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000716static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
719static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
720static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000721static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200722static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000723static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000724static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000725static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000726static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000727static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000728static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000729static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000730static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200731#ifdef FEAT_FLOAT
732static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
733static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
734#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000735static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
736static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
737static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000738#ifdef FEAT_FLOAT
739static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
740#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000741static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200742static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200743static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000744static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
745static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
746static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100747static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000748static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
752static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000754static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
755static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000756static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000757static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100758static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000759
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000760static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000761static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000762static int get_env_len __ARGS((char_u **arg));
763static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000764static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000765static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
766#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
767#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
768 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000769static 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 +0000770static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000771static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000772static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
773static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static typval_T *alloc_tv __ARGS((void));
775static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000776static void init_tv __ARGS((typval_T *varp));
777static long get_tv_number __ARGS((typval_T *varp));
778static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000779static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000780static char_u *get_tv_string __ARGS((typval_T *varp));
781static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000782static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200784static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000785static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
786static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
787static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000788static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
789static 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 +0000790static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
791static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000792static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200793static int var_check_func_name __ARGS((char_u *name, int new_var));
794static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000795static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000796static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000797static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
798static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
799static int eval_fname_script __ARGS((char_u *p));
800static int eval_fname_sid __ARGS((char_u *p));
801static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000802static ufunc_T *find_func __ARGS((char_u *name));
803static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000804static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000805#ifdef FEAT_PROFILE
806static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000807static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
808static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
809static int
810# ifdef __BORLANDC__
811 _RTLENTRYF
812# endif
813 prof_total_cmp __ARGS((const void *s1, const void *s2));
814static int
815# ifdef __BORLANDC__
816 _RTLENTRYF
817# endif
818 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000819#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200820static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000821static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000822static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000823static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000824static 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 +0000825static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
826static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000827static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000828static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
829static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000830static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000831static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000832static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000833
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200834
835#ifdef EBCDIC
836static int compare_func_name __ARGS((const void *s1, const void *s2));
837static void sortFunctions __ARGS(());
838#endif
839
Bram Moolenaar33570922005-01-25 22:26:29 +0000840/*
841 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000842 */
843 void
844eval_init()
845{
Bram Moolenaar33570922005-01-25 22:26:29 +0000846 int i;
847 struct vimvar *p;
848
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200849 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
850 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200851 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000852 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000853 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000854
855 for (i = 0; i < VV_LEN; ++i)
856 {
857 p = &vimvars[i];
858 STRCPY(p->vv_di.di_key, p->vv_name);
859 if (p->vv_flags & VV_RO)
860 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
861 else if (p->vv_flags & VV_RO_SBX)
862 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
863 else
864 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000865
866 /* add to v: scope dict, unless the value is not always available */
867 if (p->vv_type != VAR_UNKNOWN)
868 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000869 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000870 /* add to compat scope dict */
871 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000872 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000873 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200874 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200875
876#ifdef EBCDIC
877 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100878 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200879 */
880 sortFunctions();
881#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000882}
883
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000884#if defined(EXITFREE) || defined(PROTO)
885 void
886eval_clear()
887{
888 int i;
889 struct vimvar *p;
890
891 for (i = 0; i < VV_LEN; ++i)
892 {
893 p = &vimvars[i];
894 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000895 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000896 vim_free(p->vv_str);
897 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000898 }
899 else if (p->vv_di.di_tv.v_type == VAR_LIST)
900 {
901 list_unref(p->vv_list);
902 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000903 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000904 }
905 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000906 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000907 hash_clear(&compat_hashtab);
908
Bram Moolenaard9fba312005-06-26 22:34:35 +0000909 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100910# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200911 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100912# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000913
914 /* global variables */
915 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000916
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000917 /* autoloaded script names */
918 ga_clear_strings(&ga_loaded);
919
Bram Moolenaarcca74132013-09-25 21:00:28 +0200920 /* Script-local variables. First clear all the variables and in a second
921 * loop free the scriptvar_T, because a variable in one script might hold
922 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200923 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200924 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200925 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200926 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200927 ga_clear(&ga_scripts);
928
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000929 /* unreferenced lists and dicts */
930 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000931
932 /* functions */
933 free_all_functions();
934 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000935}
936#endif
937
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000938/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 * Return the name of the executed function.
940 */
941 char_u *
942func_name(cookie)
943 void *cookie;
944{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000945 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946}
947
948/*
949 * Return the address holding the next breakpoint line for a funccall cookie.
950 */
951 linenr_T *
952func_breakpoint(cookie)
953 void *cookie;
954{
Bram Moolenaar33570922005-01-25 22:26:29 +0000955 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956}
957
958/*
959 * Return the address holding the debug tick for a funccall cookie.
960 */
961 int *
962func_dbg_tick(cookie)
963 void *cookie;
964{
Bram Moolenaar33570922005-01-25 22:26:29 +0000965 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966}
967
968/*
969 * Return the nesting level for a funccall cookie.
970 */
971 int
972func_level(cookie)
973 void *cookie;
974{
Bram Moolenaar33570922005-01-25 22:26:29 +0000975 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976}
977
978/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000979funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000981/* pointer to list of previously used funccal, still around because some
982 * item in it is still being used. */
983funccall_T *previous_funccal = NULL;
984
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985/*
986 * Return TRUE when a function was ended by a ":return" command.
987 */
988 int
989current_func_returned()
990{
991 return current_funccal->returned;
992}
993
994
995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 * Set an internal variable to a string value. Creates the variable if it does
997 * not already exist.
998 */
999 void
1000set_internal_string_var(name, value)
1001 char_u *name;
1002 char_u *value;
1003{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001004 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001005 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006
1007 val = vim_strsave(value);
1008 if (val != NULL)
1009 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001010 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001011 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001013 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001014 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 }
1016 }
1017}
1018
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001020static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001021static char_u *redir_endp = NULL;
1022static char_u *redir_varname = NULL;
1023
1024/*
1025 * Start recording command output to a variable
1026 * Returns OK if successfully completed the setup. FAIL otherwise.
1027 */
1028 int
1029var_redir_start(name, append)
1030 char_u *name;
1031 int append; /* append to an existing variable */
1032{
1033 int save_emsg;
1034 int err;
1035 typval_T tv;
1036
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001037 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001038 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001039 {
1040 EMSG(_(e_invarg));
1041 return FAIL;
1042 }
1043
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001044 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001045 redir_varname = vim_strsave(name);
1046 if (redir_varname == NULL)
1047 return FAIL;
1048
1049 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1050 if (redir_lval == NULL)
1051 {
1052 var_redir_stop();
1053 return FAIL;
1054 }
1055
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001056 /* The output is stored in growarray "redir_ga" until redirection ends. */
1057 ga_init2(&redir_ga, (int)sizeof(char), 500);
1058
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001060 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1061 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1063 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001064 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001065 if (redir_endp != NULL && *redir_endp != NUL)
1066 /* Trailing characters are present after the variable name */
1067 EMSG(_(e_trailing));
1068 else
1069 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001070 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001071 var_redir_stop();
1072 return FAIL;
1073 }
1074
1075 /* check if we can write to the variable: set it to or append an empty
1076 * string */
1077 save_emsg = did_emsg;
1078 did_emsg = FALSE;
1079 tv.v_type = VAR_STRING;
1080 tv.vval.v_string = (char_u *)"";
1081 if (append)
1082 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1083 else
1084 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001085 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001087 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 if (err)
1089 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001090 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091 var_redir_stop();
1092 return FAIL;
1093 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001094
1095 return OK;
1096}
1097
1098/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099 * Append "value[value_len]" to the variable set by var_redir_start().
1100 * The actual appending is postponed until redirection ends, because the value
1101 * appended may in fact be the string we write to, changing it may cause freed
1102 * memory to be used:
1103 * :redir => foo
1104 * :let foo
1105 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 */
1107 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001110 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001112 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113
1114 if (redir_lval == NULL)
1115 return;
1116
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001117 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001118 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 {
1124 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001125 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001126 }
1127 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001129}
1130
1131/*
1132 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001133 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 */
1135 void
1136var_redir_stop()
1137{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001138 typval_T tv;
1139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_lval != NULL)
1141 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001142 /* If there was no error: assign the text to the variable. */
1143 if (redir_endp != NULL)
1144 {
1145 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1146 tv.v_type = VAR_STRING;
1147 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001148 /* Call get_lval() again, if it's inside a Dict or List it may
1149 * have changed. */
1150 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1151 FALSE, FALSE, FALSE, FNE_CHECK_START);
1152 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1153 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1154 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001156
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001157 /* free the collected output */
1158 vim_free(redir_ga.ga_data);
1159 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001160
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 vim_free(redir_lval);
1162 redir_lval = NULL;
1163 }
1164 vim_free(redir_varname);
1165 redir_varname = NULL;
1166}
1167
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168# if defined(FEAT_MBYTE) || defined(PROTO)
1169 int
1170eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1171 char_u *enc_from;
1172 char_u *enc_to;
1173 char_u *fname_from;
1174 char_u *fname_to;
1175{
1176 int err = FALSE;
1177
1178 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1179 set_vim_var_string(VV_CC_TO, enc_to, -1);
1180 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1181 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1182 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1183 err = TRUE;
1184 set_vim_var_string(VV_CC_FROM, NULL, -1);
1185 set_vim_var_string(VV_CC_TO, NULL, -1);
1186 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1187 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1188
1189 if (err)
1190 return FAIL;
1191 return OK;
1192}
1193# endif
1194
1195# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1196 int
1197eval_printexpr(fname, args)
1198 char_u *fname;
1199 char_u *args;
1200{
1201 int err = FALSE;
1202
1203 set_vim_var_string(VV_FNAME_IN, fname, -1);
1204 set_vim_var_string(VV_CMDARG, args, -1);
1205 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1206 err = TRUE;
1207 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1208 set_vim_var_string(VV_CMDARG, NULL, -1);
1209
1210 if (err)
1211 {
1212 mch_remove(fname);
1213 return FAIL;
1214 }
1215 return OK;
1216}
1217# endif
1218
1219# if defined(FEAT_DIFF) || defined(PROTO)
1220 void
1221eval_diff(origfile, newfile, outfile)
1222 char_u *origfile;
1223 char_u *newfile;
1224 char_u *outfile;
1225{
1226 int err = FALSE;
1227
1228 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1229 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1230 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1231 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1232 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1233 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1234 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1235}
1236
1237 void
1238eval_patch(origfile, difffile, outfile)
1239 char_u *origfile;
1240 char_u *difffile;
1241 char_u *outfile;
1242{
1243 int err;
1244
1245 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1246 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1247 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1248 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1249 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1250 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1251 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1252}
1253# endif
1254
1255/*
1256 * Top level evaluation function, returning a boolean.
1257 * Sets "error" to TRUE if there was an error.
1258 * Return TRUE or FALSE.
1259 */
1260 int
1261eval_to_bool(arg, error, nextcmd, skip)
1262 char_u *arg;
1263 int *error;
1264 char_u **nextcmd;
1265 int skip; /* only parse, don't execute */
1266{
Bram Moolenaar33570922005-01-25 22:26:29 +00001267 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 int retval = FALSE;
1269
1270 if (skip)
1271 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001272 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 else
1275 {
1276 *error = FALSE;
1277 if (!skip)
1278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001279 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001280 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 }
1282 }
1283 if (skip)
1284 --emsg_skip;
1285
1286 return retval;
1287}
1288
1289/*
1290 * Top level evaluation function, returning a string. If "skip" is TRUE,
1291 * only parsing to "nextcmd" is done, without reporting errors. Return
1292 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1293 */
1294 char_u *
1295eval_to_string_skip(arg, nextcmd, skip)
1296 char_u *arg;
1297 char_u **nextcmd;
1298 int skip; /* only parse, don't execute */
1299{
Bram Moolenaar33570922005-01-25 22:26:29 +00001300 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301 char_u *retval;
1302
1303 if (skip)
1304 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001305 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 retval = NULL;
1307 else
1308 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001309 retval = vim_strsave(get_tv_string(&tv));
1310 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 }
1312 if (skip)
1313 --emsg_skip;
1314
1315 return retval;
1316}
1317
1318/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001319 * Skip over an expression at "*pp".
1320 * Return FAIL for an error, OK otherwise.
1321 */
1322 int
1323skip_expr(pp)
1324 char_u **pp;
1325{
Bram Moolenaar33570922005-01-25 22:26:29 +00001326 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327
1328 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001329 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001330}
1331
1332/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001334 * When "convert" is TRUE convert a List into a sequence of lines and convert
1335 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336 * Return pointer to allocated memory, or NULL for failure.
1337 */
1338 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 char_u *arg;
1341 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001342 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001346 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001347#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001348 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 retval = NULL;
1353 else
1354 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001355 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001356 {
1357 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001358 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001359 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001360 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001361 if (tv.vval.v_list->lv_len > 0)
1362 ga_append(&ga, NL);
1363 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001364 ga_append(&ga, NUL);
1365 retval = (char_u *)ga.ga_data;
1366 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001367#ifdef FEAT_FLOAT
1368 else if (convert && tv.v_type == VAR_FLOAT)
1369 {
1370 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1371 retval = vim_strsave(numbuf);
1372 }
1373#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001374 else
1375 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 }
1378
1379 return retval;
1380}
1381
1382/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001383 * Call eval_to_string() without using current local variables and using
1384 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 */
1386 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 char_u *arg;
1389 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001390 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391{
1392 char_u *retval;
1393 void *save_funccalp;
1394
1395 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001396 if (use_sandbox)
1397 ++sandbox;
1398 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001399 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001400 if (use_sandbox)
1401 --sandbox;
1402 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 restore_funccal(save_funccalp);
1404 return retval;
1405}
1406
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407/*
1408 * Top level evaluation function, returning a number.
1409 * Evaluates "expr" silently.
1410 * Returns -1 for an error.
1411 */
1412 int
1413eval_to_number(expr)
1414 char_u *expr;
1415{
Bram Moolenaar33570922005-01-25 22:26:29 +00001416 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001418 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419
1420 ++emsg_off;
1421
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001422 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 retval = -1;
1424 else
1425 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001426 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001427 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 }
1429 --emsg_off;
1430
1431 return retval;
1432}
1433
Bram Moolenaara40058a2005-07-11 22:42:07 +00001434/*
1435 * Prepare v: variable "idx" to be used.
1436 * Save the current typeval in "save_tv".
1437 * When not used yet add the variable to the v: hashtable.
1438 */
1439 static void
1440prepare_vimvar(idx, save_tv)
1441 int idx;
1442 typval_T *save_tv;
1443{
1444 *save_tv = vimvars[idx].vv_tv;
1445 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1446 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1447}
1448
1449/*
1450 * Restore v: variable "idx" to typeval "save_tv".
1451 * When no longer defined, remove the variable from the v: hashtable.
1452 */
1453 static void
1454restore_vimvar(idx, save_tv)
1455 int idx;
1456 typval_T *save_tv;
1457{
1458 hashitem_T *hi;
1459
Bram Moolenaara40058a2005-07-11 22:42:07 +00001460 vimvars[idx].vv_tv = *save_tv;
1461 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1462 {
1463 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1464 if (HASHITEM_EMPTY(hi))
1465 EMSG2(_(e_intern2), "restore_vimvar()");
1466 else
1467 hash_remove(&vimvarht, hi);
1468 }
1469}
1470
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001471#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001472/*
1473 * Evaluate an expression to a list with suggestions.
1474 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001475 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001476 */
1477 list_T *
1478eval_spell_expr(badword, expr)
1479 char_u *badword;
1480 char_u *expr;
1481{
1482 typval_T save_val;
1483 typval_T rettv;
1484 list_T *list = NULL;
1485 char_u *p = skipwhite(expr);
1486
1487 /* Set "v:val" to the bad word. */
1488 prepare_vimvar(VV_VAL, &save_val);
1489 vimvars[VV_VAL].vv_type = VAR_STRING;
1490 vimvars[VV_VAL].vv_str = badword;
1491 if (p_verbose == 0)
1492 ++emsg_off;
1493
1494 if (eval1(&p, &rettv, TRUE) == OK)
1495 {
1496 if (rettv.v_type != VAR_LIST)
1497 clear_tv(&rettv);
1498 else
1499 list = rettv.vval.v_list;
1500 }
1501
1502 if (p_verbose == 0)
1503 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001504 restore_vimvar(VV_VAL, &save_val);
1505
1506 return list;
1507}
1508
1509/*
1510 * "list" is supposed to contain two items: a word and a number. Return the
1511 * word in "pp" and the number as the return value.
1512 * Return -1 if anything isn't right.
1513 * Used to get the good word and score from the eval_spell_expr() result.
1514 */
1515 int
1516get_spellword(list, pp)
1517 list_T *list;
1518 char_u **pp;
1519{
1520 listitem_T *li;
1521
1522 li = list->lv_first;
1523 if (li == NULL)
1524 return -1;
1525 *pp = get_tv_string(&li->li_tv);
1526
1527 li = li->li_next;
1528 if (li == NULL)
1529 return -1;
1530 return get_tv_number(&li->li_tv);
1531}
1532#endif
1533
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001534/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001535 * Top level evaluation function.
1536 * Returns an allocated typval_T with the result.
1537 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001538 */
1539 typval_T *
1540eval_expr(arg, nextcmd)
1541 char_u *arg;
1542 char_u **nextcmd;
1543{
1544 typval_T *tv;
1545
1546 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001547 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 {
1549 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001550 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001551 }
1552
1553 return tv;
1554}
1555
1556
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001559 * Uses argv[argc] for the function arguments. Only Number and String
1560 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001561 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001563 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001564call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565 char_u *func;
1566 int argc;
1567 char_u **argv;
1568 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001569 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001570 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
Bram Moolenaar33570922005-01-25 22:26:29 +00001572 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 long n;
1574 int len;
1575 int i;
1576 int doesrange;
1577 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001578 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001580 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001582 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
1584 for (i = 0; i < argc; i++)
1585 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001586 /* Pass a NULL or empty argument as an empty string */
1587 if (argv[i] == NULL || *argv[i] == NUL)
1588 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001589 argvars[i].v_type = VAR_STRING;
1590 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001591 continue;
1592 }
1593
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001594 if (str_arg_only)
1595 len = 0;
1596 else
1597 /* Recognize a number argument, the others must be strings. */
1598 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 if (len != 0 && len == (int)STRLEN(argv[i]))
1600 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001601 argvars[i].v_type = VAR_NUMBER;
1602 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001603 }
1604 else
1605 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001606 argvars[i].v_type = VAR_STRING;
1607 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001608 }
1609 }
1610
1611 if (safe)
1612 {
1613 save_funccalp = save_funccal();
1614 ++sandbox;
1615 }
1616
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1618 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 if (safe)
1622 {
1623 --sandbox;
1624 restore_funccal(save_funccalp);
1625 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626 vim_free(argvars);
1627
1628 if (ret == FAIL)
1629 clear_tv(rettv);
1630
1631 return ret;
1632}
1633
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001634/*
1635 * Call vimL function "func" and return the result as a number.
1636 * Returns -1 when calling the function fails.
1637 * Uses argv[argc] for the function arguments.
1638 */
1639 long
1640call_func_retnr(func, argc, argv, safe)
1641 char_u *func;
1642 int argc;
1643 char_u **argv;
1644 int safe; /* use the sandbox */
1645{
1646 typval_T rettv;
1647 long retval;
1648
1649 /* All arguments are passed as strings, no conversion to number. */
1650 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1651 return -1;
1652
1653 retval = get_tv_number_chk(&rettv, NULL);
1654 clear_tv(&rettv);
1655 return retval;
1656}
1657
1658#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1659 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1660
Bram Moolenaar4f688582007-07-24 12:34:30 +00001661# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001662/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001663 * Call vimL function "func" and return the result as a string.
1664 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001665 * Uses argv[argc] for the function arguments.
1666 */
1667 void *
1668call_func_retstr(func, argc, argv, safe)
1669 char_u *func;
1670 int argc;
1671 char_u **argv;
1672 int safe; /* use the sandbox */
1673{
1674 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001675 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001677 /* All arguments are passed as strings, no conversion to number. */
1678 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 return NULL;
1680
1681 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 return retval;
1684}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001685# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001687/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001688 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001690 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001691 */
1692 void *
1693call_func_retlist(func, argc, argv, safe)
1694 char_u *func;
1695 int argc;
1696 char_u **argv;
1697 int safe; /* use the sandbox */
1698{
1699 typval_T rettv;
1700
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001701 /* All arguments are passed as strings, no conversion to number. */
1702 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001703 return NULL;
1704
1705 if (rettv.v_type != VAR_LIST)
1706 {
1707 clear_tv(&rettv);
1708 return NULL;
1709 }
1710
1711 return rettv.vval.v_list;
1712}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713#endif
1714
1715/*
1716 * Save the current function call pointer, and set it to NULL.
1717 * Used when executing autocommands and for ":source".
1718 */
1719 void *
1720save_funccal()
1721{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001722 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724 current_funccal = NULL;
1725 return (void *)fc;
1726}
1727
1728 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729restore_funccal(vfc)
1730 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001732 funccall_T *fc = (funccall_T *)vfc;
1733
1734 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001735}
1736
Bram Moolenaar05159a02005-02-26 23:04:13 +00001737#if defined(FEAT_PROFILE) || defined(PROTO)
1738/*
1739 * Prepare profiling for entering a child or something else that is not
1740 * counted for the script/function itself.
1741 * Should always be called in pair with prof_child_exit().
1742 */
1743 void
1744prof_child_enter(tm)
1745 proftime_T *tm; /* place to store waittime */
1746{
1747 funccall_T *fc = current_funccal;
1748
1749 if (fc != NULL && fc->func->uf_profiling)
1750 profile_start(&fc->prof_child);
1751 script_prof_save(tm);
1752}
1753
1754/*
1755 * Take care of time spent in a child.
1756 * Should always be called after prof_child_enter().
1757 */
1758 void
1759prof_child_exit(tm)
1760 proftime_T *tm; /* where waittime was stored */
1761{
1762 funccall_T *fc = current_funccal;
1763
1764 if (fc != NULL && fc->func->uf_profiling)
1765 {
1766 profile_end(&fc->prof_child);
1767 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1768 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1769 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1770 }
1771 script_prof_restore(tm);
1772}
1773#endif
1774
1775
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776#ifdef FEAT_FOLDING
1777/*
1778 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1779 * it in "*cp". Doesn't give error messages.
1780 */
1781 int
1782eval_foldexpr(arg, cp)
1783 char_u *arg;
1784 int *cp;
1785{
Bram Moolenaar33570922005-01-25 22:26:29 +00001786 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787 int retval;
1788 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001789 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1790 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
1792 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001793 if (use_sandbox)
1794 ++sandbox;
1795 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001797 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 retval = 0;
1799 else
1800 {
1801 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001802 if (tv.v_type == VAR_NUMBER)
1803 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001804 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001805 retval = 0;
1806 else
1807 {
1808 /* If the result is a string, check if there is a non-digit before
1809 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001810 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (!VIM_ISDIGIT(*s) && *s != '-')
1812 *cp = *s++;
1813 retval = atol((char *)s);
1814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001815 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 }
1817 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001818 if (use_sandbox)
1819 --sandbox;
1820 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821
1822 return retval;
1823}
1824#endif
1825
Bram Moolenaar071d4272004-06-13 20:20:40 +00001826/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001827 * ":let" list all variable values
1828 * ":let var1 var2" list variable values
1829 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001830 * ":let var += expr" assignment command.
1831 * ":let var -= expr" assignment command.
1832 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001833 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 */
1835 void
1836ex_let(eap)
1837 exarg_T *eap;
1838{
1839 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001841 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001843 int var_count = 0;
1844 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001845 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001847 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848
Bram Moolenaardb552d602006-03-23 22:59:57 +00001849 argend = skip_var_list(arg, &var_count, &semicolon);
1850 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001851 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001852 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1853 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001854 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001855 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001857 /*
1858 * ":let" without "=": list variables
1859 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001860 if (*arg == '[')
1861 EMSG(_(e_invarg));
1862 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001864 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001866 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001867 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001868 list_glob_vars(&first);
1869 list_buf_vars(&first);
1870 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001871#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001873#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001874 list_script_vars(&first);
1875 list_func_vars(&first);
1876 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 eap->nextcmd = check_nextcmd(arg);
1879 }
1880 else
1881 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882 op[0] = '=';
1883 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001884 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001885 {
1886 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1887 op[0] = expr[-1]; /* +=, -= or .= */
1888 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001889 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (eap->skip)
1892 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001893 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 if (eap->skip)
1895 {
1896 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 --emsg_skip;
1899 }
1900 else if (i != FAIL)
1901 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001902 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906 }
1907}
1908
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909/*
1910 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1911 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001912 * When "nextchars" is not NULL it points to a string with characters that
1913 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1914 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001915 * Returns OK or FAIL;
1916 */
1917 static int
1918ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1919 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001920 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001921 int copy; /* copy values from "tv", don't move */
1922 int semicolon; /* from skip_var_list() */
1923 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001924 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925{
1926 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001927 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001929 listitem_T *item;
1930 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001931
1932 if (*arg != '[')
1933 {
1934 /*
1935 * ":let var = expr" or ":for var in list"
1936 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001937 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001938 return FAIL;
1939 return OK;
1940 }
1941
1942 /*
1943 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1944 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001945 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001946 {
1947 EMSG(_(e_listreq));
1948 return FAIL;
1949 }
1950
1951 i = list_len(l);
1952 if (semicolon == 0 && var_count < i)
1953 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001954 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001955 return FAIL;
1956 }
1957 if (var_count - semicolon > i)
1958 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001959 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 return FAIL;
1961 }
1962
1963 item = l->lv_first;
1964 while (*arg != ']')
1965 {
1966 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 item = item->li_next;
1969 if (arg == NULL)
1970 return FAIL;
1971
1972 arg = skipwhite(arg);
1973 if (*arg == ';')
1974 {
1975 /* Put the rest of the list (may be empty) in the var after ';'.
1976 * Create a new list for this. */
1977 l = list_alloc();
1978 if (l == NULL)
1979 return FAIL;
1980 while (item != NULL)
1981 {
1982 list_append_tv(l, &item->li_tv);
1983 item = item->li_next;
1984 }
1985
1986 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001987 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001988 ltv.vval.v_list = l;
1989 l->lv_refcount = 1;
1990
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001991 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1992 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993 clear_tv(&ltv);
1994 if (arg == NULL)
1995 return FAIL;
1996 break;
1997 }
1998 else if (*arg != ',' && *arg != ']')
1999 {
2000 EMSG2(_(e_intern2), "ex_let_vars()");
2001 return FAIL;
2002 }
2003 }
2004
2005 return OK;
2006}
2007
2008/*
2009 * Skip over assignable variable "var" or list of variables "[var, var]".
2010 * Used for ":let varvar = expr" and ":for varvar in expr".
2011 * For "[var, var]" increment "*var_count" for each variable.
2012 * for "[var, var; var]" set "semicolon".
2013 * Return NULL for an error.
2014 */
2015 static char_u *
2016skip_var_list(arg, var_count, semicolon)
2017 char_u *arg;
2018 int *var_count;
2019 int *semicolon;
2020{
2021 char_u *p, *s;
2022
2023 if (*arg == '[')
2024 {
2025 /* "[var, var]": find the matching ']'. */
2026 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002027 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002028 {
2029 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2030 s = skip_var_one(p);
2031 if (s == p)
2032 {
2033 EMSG2(_(e_invarg2), p);
2034 return NULL;
2035 }
2036 ++*var_count;
2037
2038 p = skipwhite(s);
2039 if (*p == ']')
2040 break;
2041 else if (*p == ';')
2042 {
2043 if (*semicolon == 1)
2044 {
2045 EMSG(_("Double ; in list of variables"));
2046 return NULL;
2047 }
2048 *semicolon = 1;
2049 }
2050 else if (*p != ',')
2051 {
2052 EMSG2(_(e_invarg2), p);
2053 return NULL;
2054 }
2055 }
2056 return p + 1;
2057 }
2058 else
2059 return skip_var_one(arg);
2060}
2061
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002063 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002064 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002065 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002066 static char_u *
2067skip_var_one(arg)
2068 char_u *arg;
2069{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002070 if (*arg == '@' && arg[1] != NUL)
2071 return arg + 2;
2072 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2073 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002074}
2075
Bram Moolenaara7043832005-01-21 11:56:39 +00002076/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002077 * List variables for hashtab "ht" with prefix "prefix".
2078 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002079 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002080 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002081list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002082 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002084 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002085 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002086{
Bram Moolenaar33570922005-01-25 22:26:29 +00002087 hashitem_T *hi;
2088 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 int todo;
2090
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002091 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002092 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2093 {
2094 if (!HASHITEM_EMPTY(hi))
2095 {
2096 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002097 di = HI2DI(hi);
2098 if (empty || di->di_tv.v_type != VAR_STRING
2099 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002100 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002101 }
2102 }
2103}
2104
2105/*
2106 * List global variables.
2107 */
2108 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109list_glob_vars(first)
2110 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002111{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002112 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002113}
2114
2115/*
2116 * List buffer variables.
2117 */
2118 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002119list_buf_vars(first)
2120 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002121{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002122 char_u numbuf[NUMBUFLEN];
2123
Bram Moolenaar429fa852013-04-15 12:27:36 +02002124 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126
2127 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002128 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2129 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002130}
2131
2132/*
2133 * List window variables.
2134 */
2135 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002136list_win_vars(first)
2137 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002138{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002139 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002140 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002141}
2142
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002143#ifdef FEAT_WINDOWS
2144/*
2145 * List tab page variables.
2146 */
2147 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002148list_tab_vars(first)
2149 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002150{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002151 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002152 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002153}
2154#endif
2155
Bram Moolenaara7043832005-01-21 11:56:39 +00002156/*
2157 * List Vim variables.
2158 */
2159 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160list_vim_vars(first)
2161 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002164}
2165
2166/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002167 * List script-local variables, if there is a script.
2168 */
2169 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170list_script_vars(first)
2171 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002172{
2173 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2175 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002176}
2177
2178/*
2179 * List function variables, if there is a function.
2180 */
2181 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002182list_func_vars(first)
2183 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002184{
2185 if (current_funccal != NULL)
2186 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002188}
2189
2190/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002191 * List variables in "arg".
2192 */
2193 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195 exarg_T *eap;
2196 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198{
2199 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002201 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002202 char_u *name_start;
2203 char_u *arg_subsc;
2204 char_u *tofree;
2205 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002206
2207 while (!ends_excmd(*arg) && !got_int)
2208 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002210 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002211 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002212 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2213 {
2214 emsg_severe = TRUE;
2215 EMSG(_(e_trailing));
2216 break;
2217 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002221 /* get_name_len() takes care of expanding curly braces */
2222 name_start = name = arg;
2223 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2224 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002225 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002226 /* This is mainly to keep test 49 working: when expanding
2227 * curly braces fails overrule the exception error message. */
2228 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002230 emsg_severe = TRUE;
2231 EMSG2(_(e_invarg2), arg);
2232 break;
2233 }
2234 error = TRUE;
2235 }
2236 else
2237 {
2238 if (tofree != NULL)
2239 name = tofree;
2240 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002242 else
2243 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002244 /* handle d.key, l[idx], f(expr) */
2245 arg_subsc = arg;
2246 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002253 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002254 case 'g': list_glob_vars(first); break;
2255 case 'b': list_buf_vars(first); break;
2256 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002257#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002258 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002259#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002260 case 'v': list_vim_vars(first); break;
2261 case 's': list_script_vars(first); break;
2262 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 default:
2264 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002265 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002266 }
2267 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 {
2269 char_u numbuf[NUMBUFLEN];
2270 char_u *tf;
2271 int c;
2272 char_u *s;
2273
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002274 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275 c = *arg;
2276 *arg = NUL;
2277 list_one_var_a((char_u *)"",
2278 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002279 tv.v_type,
2280 s == NULL ? (char_u *)"" : s,
2281 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002282 *arg = c;
2283 vim_free(tf);
2284 }
2285 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002286 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002287 }
2288 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289
2290 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002292
2293 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 }
2295
2296 return arg;
2297}
2298
2299/*
2300 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2301 * Returns a pointer to the char just after the var name.
2302 * Returns NULL if there is an error.
2303 */
2304 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002305ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002307 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002309 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002310 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002311{
2312 int c1;
2313 char_u *name;
2314 char_u *p;
2315 char_u *arg_end = NULL;
2316 int len;
2317 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002318 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002319
2320 /*
2321 * ":let $VAR = expr": Set environment variable.
2322 */
2323 if (*arg == '$')
2324 {
2325 /* Find the end of the name. */
2326 ++arg;
2327 name = arg;
2328 len = get_env_len(&arg);
2329 if (len == 0)
2330 EMSG2(_(e_invarg2), name - 1);
2331 else
2332 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002333 if (op != NULL && (*op == '+' || *op == '-'))
2334 EMSG2(_(e_letwrong), op);
2335 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002336 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002338 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002339 {
2340 c1 = name[len];
2341 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002342 p = get_tv_string_chk(tv);
2343 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002344 {
2345 int mustfree = FALSE;
2346 char_u *s = vim_getenv(name, &mustfree);
2347
2348 if (s != NULL)
2349 {
2350 p = tofree = concat_str(s, p);
2351 if (mustfree)
2352 vim_free(s);
2353 }
2354 }
2355 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002356 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002357 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002358 if (STRICMP(name, "HOME") == 0)
2359 init_homedir();
2360 else if (didset_vim && STRICMP(name, "VIM") == 0)
2361 didset_vim = FALSE;
2362 else if (didset_vimruntime
2363 && STRICMP(name, "VIMRUNTIME") == 0)
2364 didset_vimruntime = FALSE;
2365 arg_end = arg;
2366 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002368 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369 }
2370 }
2371 }
2372
2373 /*
2374 * ":let &option = expr": Set option value.
2375 * ":let &l:option = expr": Set local option value.
2376 * ":let &g:option = expr": Set global option value.
2377 */
2378 else if (*arg == '&')
2379 {
2380 /* Find the end of the name. */
2381 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002382 if (p == NULL || (endchars != NULL
2383 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 EMSG(_(e_letunexp));
2385 else
2386 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 long n;
2388 int opt_type;
2389 long numval;
2390 char_u *stringval = NULL;
2391 char_u *s;
2392
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 c1 = *p;
2394 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002395
2396 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002397 s = get_tv_string_chk(tv); /* != NULL if number or string */
2398 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399 {
2400 opt_type = get_option_value(arg, &numval,
2401 &stringval, opt_flags);
2402 if ((opt_type == 1 && *op == '.')
2403 || (opt_type == 0 && *op != '.'))
2404 EMSG2(_(e_letwrong), op);
2405 else
2406 {
2407 if (opt_type == 1) /* number */
2408 {
2409 if (*op == '+')
2410 n = numval + n;
2411 else
2412 n = numval - n;
2413 }
2414 else if (opt_type == 0 && stringval != NULL) /* string */
2415 {
2416 s = concat_str(stringval, s);
2417 vim_free(stringval);
2418 stringval = s;
2419 }
2420 }
2421 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002422 if (s != NULL)
2423 {
2424 set_option_value(arg, n, s, opt_flags);
2425 arg_end = p;
2426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 }
2430 }
2431
2432 /*
2433 * ":let @r = expr": Set register contents.
2434 */
2435 else if (*arg == '@')
2436 {
2437 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002438 if (op != NULL && (*op == '+' || *op == '-'))
2439 EMSG2(_(e_letwrong), op);
2440 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002441 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002442 EMSG(_(e_letunexp));
2443 else
2444 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002445 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 char_u *s;
2447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002448 p = get_tv_string_chk(tv);
2449 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002451 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 if (s != NULL)
2453 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002454 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455 vim_free(s);
2456 }
2457 }
2458 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002459 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002460 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002461 arg_end = arg + 1;
2462 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002463 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002464 }
2465 }
2466
2467 /*
2468 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002469 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002471 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002473 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002475 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002478 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2479 EMSG(_(e_letunexp));
2480 else
2481 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 arg_end = p;
2484 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002486 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 }
2488
2489 else
2490 EMSG2(_(e_invarg2), arg);
2491
2492 return arg_end;
2493}
2494
2495/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002496 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2497 */
2498 static int
2499check_changedtick(arg)
2500 char_u *arg;
2501{
2502 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2503 {
2504 EMSG2(_(e_readonlyvar), arg);
2505 return TRUE;
2506 }
2507 return FALSE;
2508}
2509
2510/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002511 * Get an lval: variable, Dict item or List item that can be assigned a value
2512 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2513 * "name.key", "name.key[expr]" etc.
2514 * Indexing only works if "name" is an existing List or Dictionary.
2515 * "name" points to the start of the name.
2516 * If "rettv" is not NULL it points to the value to be assigned.
2517 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2518 * wrong; must end in space or cmd separator.
2519 *
2520 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002521 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 */
2524 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002525get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002527 typval_T *rettv;
2528 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 int unlet;
2530 int skip;
2531 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002532 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002533{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002535 char_u *expr_start, *expr_end;
2536 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002537 dictitem_T *v;
2538 typval_T var1;
2539 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002542 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002545
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002547 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002548
2549 if (skip)
2550 {
2551 /* When skipping just find the end of the name. */
2552 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002553 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002554 }
2555
2556 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002557 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002558 if (expr_start != NULL)
2559 {
2560 /* Don't expand the name when we already know there is an error. */
2561 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2562 && *p != '[' && *p != '.')
2563 {
2564 EMSG(_(e_trailing));
2565 return NULL;
2566 }
2567
2568 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2569 if (lp->ll_exp_name == NULL)
2570 {
2571 /* Report an invalid expression in braces, unless the
2572 * expression evaluation has been cancelled due to an
2573 * aborting error, an interrupt, or an exception. */
2574 if (!aborting() && !quiet)
2575 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002576 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 EMSG2(_(e_invarg2), name);
2578 return NULL;
2579 }
2580 }
2581 lp->ll_name = lp->ll_exp_name;
2582 }
2583 else
2584 lp->ll_name = name;
2585
2586 /* Without [idx] or .key we are done. */
2587 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2588 return p;
2589
2590 cc = *p;
2591 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002592 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 if (v == NULL && !quiet)
2594 EMSG2(_(e_undefvar), lp->ll_name);
2595 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596 if (v == NULL)
2597 return NULL;
2598
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599 /*
2600 * Loop until no more [idx] or .key is following.
2601 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002602 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2606 && !(lp->ll_tv->v_type == VAR_DICT
2607 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (!quiet)
2610 EMSG(_("E689: Can only index a List or Dictionary"));
2611 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002614 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (!quiet)
2616 EMSG(_("E708: [:] must come last"));
2617 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002618 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002619
Bram Moolenaar8c711452005-01-14 21:53:12 +00002620 len = -1;
2621 if (*p == '.')
2622 {
2623 key = p + 1;
2624 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2625 ;
2626 if (len == 0)
2627 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 if (!quiet)
2629 EMSG(_(e_emptykey));
2630 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002631 }
2632 p = key + len;
2633 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 else
2635 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002638 if (*p == ':')
2639 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002640 else
2641 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002642 empty1 = FALSE;
2643 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002645 if (get_tv_string_chk(&var1) == NULL)
2646 {
2647 /* not a number or string */
2648 clear_tv(&var1);
2649 return NULL;
2650 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002651 }
2652
2653 /* Optionally get the second index [ :expr]. */
2654 if (*p == ':')
2655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002657 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002659 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660 if (!empty1)
2661 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002663 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (rettv != NULL && (rettv->v_type != VAR_LIST
2665 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
2668 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 if (!empty1)
2670 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002672 }
2673 p = skipwhite(p + 1);
2674 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 else
2677 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002678 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2680 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 if (!empty1)
2682 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002683 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002685 if (get_tv_string_chk(&var2) == NULL)
2686 {
2687 /* not a number or string */
2688 if (!empty1)
2689 clear_tv(&var1);
2690 clear_tv(&var2);
2691 return NULL;
2692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002697 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002700 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 if (!quiet)
2702 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 if (!empty1)
2704 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
2709
2710 /* Skip to past ']'. */
2711 ++p;
2712 }
2713
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 {
2716 if (len == -1)
2717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002719 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (*key == NUL)
2721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (!quiet)
2723 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
2727 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002728 lp->ll_list = NULL;
2729 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002730 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002731
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002732 /* When assigning to a scope dictionary check that a function and
2733 * variable name is valid (only variable name unless it is l: or
2734 * g: dictionary). Disallow overwriting a builtin function. */
2735 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002736 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002737 int prevval;
2738 int wrong;
2739
2740 if (len != -1)
2741 {
2742 prevval = key[len];
2743 key[len] = NUL;
2744 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002745 else
2746 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002747 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2748 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002749 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002750 || !valid_varname(key);
2751 if (len != -1)
2752 key[len] = prevval;
2753 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002754 return NULL;
2755 }
2756
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002757 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002759 /* Can't add "v:" variable. */
2760 if (lp->ll_dict == &vimvardict)
2761 {
2762 EMSG2(_(e_illvar), name);
2763 return NULL;
2764 }
2765
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002766 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002770 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 if (len == -1)
2772 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 }
2775 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 if (len == -1)
2780 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 p = NULL;
2783 break;
2784 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002785 /* existing variable, need to check if it can be changed */
2786 else if (var_check_ro(lp->ll_di->di_flags, name))
2787 return NULL;
2788
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 if (len == -1)
2790 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002791 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002792 }
2793 else
2794 {
2795 /*
2796 * Get the number and item for the only or first index of the List.
2797 */
2798 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002799 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 else
2801 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002802 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002803 clear_tv(&var1);
2804 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002805 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002806 lp->ll_list = lp->ll_tv->vval.v_list;
2807 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2808 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002810 if (lp->ll_n1 < 0)
2811 {
2812 lp->ll_n1 = 0;
2813 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2814 }
2815 }
2816 if (lp->ll_li == NULL)
2817 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002820 if (!quiet)
2821 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002822 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002823 }
2824
2825 /*
2826 * May need to find the item or absolute index for the second
2827 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 * When no index given: "lp->ll_empty2" is TRUE.
2829 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002833 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002839 {
2840 if (!quiet)
2841 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002843 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002845 }
2846
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002847 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2848 if (lp->ll_n1 < 0)
2849 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2850 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002851 {
2852 if (!quiet)
2853 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002854 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002855 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002856 }
2857
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002860 }
2861
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002862 return p;
2863}
2864
2865/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002866 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002867 */
2868 static void
2869clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002870 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002871{
2872 vim_free(lp->ll_exp_name);
2873 vim_free(lp->ll_newkey);
2874}
2875
2876/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002877 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002879 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 */
2881 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002882set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002885 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002887 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888{
2889 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002890 listitem_T *ri;
2891 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892
2893 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002894 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002896 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002897 cc = *endp;
2898 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002899 if (op != NULL && *op != '=')
2900 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002901 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002902
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002903 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002904 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002905 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002906 {
2907 if (tv_op(&tv, rettv, op) == OK)
2908 set_var(lp->ll_name, &tv, FALSE);
2909 clear_tv(&tv);
2910 }
2911 }
2912 else
2913 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002915 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002917 else if (tv_check_lock(lp->ll_newkey == NULL
2918 ? lp->ll_tv->v_lock
2919 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2920 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 else if (lp->ll_range)
2922 {
2923 /*
2924 * Assign the List values to the list items.
2925 */
2926 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002927 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002928 if (op != NULL && *op != '=')
2929 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2930 else
2931 {
2932 clear_tv(&lp->ll_li->li_tv);
2933 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2934 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 ri = ri->li_next;
2936 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2937 break;
2938 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002941 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002942 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 ri = NULL;
2944 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002945 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002946 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 lp->ll_li = lp->ll_li->li_next;
2948 ++lp->ll_n1;
2949 }
2950 if (ri != NULL)
2951 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952 else if (lp->ll_empty2
2953 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 : lp->ll_n1 != lp->ll_n2)
2955 EMSG(_("E711: List value has not enough items"));
2956 }
2957 else
2958 {
2959 /*
2960 * Assign to a List or Dictionary item.
2961 */
2962 if (lp->ll_newkey != NULL)
2963 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002964 if (op != NULL && *op != '=')
2965 {
2966 EMSG2(_(e_letwrong), op);
2967 return;
2968 }
2969
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002971 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 if (di == NULL)
2973 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002974 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2975 {
2976 vim_free(di);
2977 return;
2978 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002980 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002981 else if (op != NULL && *op != '=')
2982 {
2983 tv_op(lp->ll_tv, rettv, op);
2984 return;
2985 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002986 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002988
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 /*
2990 * Assign the value to the variable or list item.
2991 */
2992 if (copy)
2993 copy_tv(rettv, lp->ll_tv);
2994 else
2995 {
2996 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002997 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002999 }
3000 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003001}
3002
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003003/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003004 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3005 * Returns OK or FAIL.
3006 */
3007 static int
3008tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003009 typval_T *tv1;
3010 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003011 char_u *op;
3012{
3013 long n;
3014 char_u numbuf[NUMBUFLEN];
3015 char_u *s;
3016
3017 /* Can't do anything with a Funcref or a Dict on the right. */
3018 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3019 {
3020 switch (tv1->v_type)
3021 {
3022 case VAR_DICT:
3023 case VAR_FUNC:
3024 break;
3025
3026 case VAR_LIST:
3027 if (*op != '+' || tv2->v_type != VAR_LIST)
3028 break;
3029 /* List += List */
3030 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3031 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3032 return OK;
3033
3034 case VAR_NUMBER:
3035 case VAR_STRING:
3036 if (tv2->v_type == VAR_LIST)
3037 break;
3038 if (*op == '+' || *op == '-')
3039 {
3040 /* nr += nr or nr -= nr*/
3041 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003042#ifdef FEAT_FLOAT
3043 if (tv2->v_type == VAR_FLOAT)
3044 {
3045 float_T f = n;
3046
3047 if (*op == '+')
3048 f += tv2->vval.v_float;
3049 else
3050 f -= tv2->vval.v_float;
3051 clear_tv(tv1);
3052 tv1->v_type = VAR_FLOAT;
3053 tv1->vval.v_float = f;
3054 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003055 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003056#endif
3057 {
3058 if (*op == '+')
3059 n += get_tv_number(tv2);
3060 else
3061 n -= get_tv_number(tv2);
3062 clear_tv(tv1);
3063 tv1->v_type = VAR_NUMBER;
3064 tv1->vval.v_number = n;
3065 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003066 }
3067 else
3068 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003069 if (tv2->v_type == VAR_FLOAT)
3070 break;
3071
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003072 /* str .= str */
3073 s = get_tv_string(tv1);
3074 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3075 clear_tv(tv1);
3076 tv1->v_type = VAR_STRING;
3077 tv1->vval.v_string = s;
3078 }
3079 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003080
3081#ifdef FEAT_FLOAT
3082 case VAR_FLOAT:
3083 {
3084 float_T f;
3085
3086 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3087 && tv2->v_type != VAR_NUMBER
3088 && tv2->v_type != VAR_STRING))
3089 break;
3090 if (tv2->v_type == VAR_FLOAT)
3091 f = tv2->vval.v_float;
3092 else
3093 f = get_tv_number(tv2);
3094 if (*op == '+')
3095 tv1->vval.v_float += f;
3096 else
3097 tv1->vval.v_float -= f;
3098 }
3099 return OK;
3100#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003101 }
3102 }
3103
3104 EMSG2(_(e_letwrong), op);
3105 return FAIL;
3106}
3107
3108/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003109 * Add a watcher to a list.
3110 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003111 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003112list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003113 list_T *l;
3114 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003115{
3116 lw->lw_next = l->lv_watch;
3117 l->lv_watch = lw;
3118}
3119
3120/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003121 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122 * No warning when it isn't found...
3123 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003124 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003125list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003126 list_T *l;
3127 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003128{
Bram Moolenaar33570922005-01-25 22:26:29 +00003129 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003130
3131 lwp = &l->lv_watch;
3132 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3133 {
3134 if (lw == lwrem)
3135 {
3136 *lwp = lw->lw_next;
3137 break;
3138 }
3139 lwp = &lw->lw_next;
3140 }
3141}
3142
3143/*
3144 * Just before removing an item from a list: advance watchers to the next
3145 * item.
3146 */
3147 static void
3148list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003149 list_T *l;
3150 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151{
Bram Moolenaar33570922005-01-25 22:26:29 +00003152 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003153
3154 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3155 if (lw->lw_item == item)
3156 lw->lw_item = item->li_next;
3157}
3158
3159/*
3160 * Evaluate the expression used in a ":for var in expr" command.
3161 * "arg" points to "var".
3162 * Set "*errp" to TRUE for an error, FALSE otherwise;
3163 * Return a pointer that holds the info. Null when there is an error.
3164 */
3165 void *
3166eval_for_line(arg, errp, nextcmdp, skip)
3167 char_u *arg;
3168 int *errp;
3169 char_u **nextcmdp;
3170 int skip;
3171{
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003174 typval_T tv;
3175 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003176
3177 *errp = TRUE; /* default: there is an error */
3178
Bram Moolenaar33570922005-01-25 22:26:29 +00003179 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003180 if (fi == NULL)
3181 return NULL;
3182
3183 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3184 if (expr == NULL)
3185 return fi;
3186
3187 expr = skipwhite(expr);
3188 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3189 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003190 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 return fi;
3192 }
3193
3194 if (skip)
3195 ++emsg_skip;
3196 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3197 {
3198 *errp = FALSE;
3199 if (!skip)
3200 {
3201 l = tv.vval.v_list;
3202 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003203 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003205 clear_tv(&tv);
3206 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003207 else
3208 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003209 /* No need to increment the refcount, it's already set for the
3210 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003211 fi->fi_list = l;
3212 list_add_watch(l, &fi->fi_lw);
3213 fi->fi_lw.lw_item = l->lv_first;
3214 }
3215 }
3216 }
3217 if (skip)
3218 --emsg_skip;
3219
3220 return fi;
3221}
3222
3223/*
3224 * Use the first item in a ":for" list. Advance to the next.
3225 * Assign the values to the variable (list). "arg" points to the first one.
3226 * Return TRUE when a valid item was found, FALSE when at end of list or
3227 * something wrong.
3228 */
3229 int
3230next_for_item(fi_void, arg)
3231 void *fi_void;
3232 char_u *arg;
3233{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003234 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003236 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003237
3238 item = fi->fi_lw.lw_item;
3239 if (item == NULL)
3240 result = FALSE;
3241 else
3242 {
3243 fi->fi_lw.lw_item = item->li_next;
3244 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3245 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3246 }
3247 return result;
3248}
3249
3250/*
3251 * Free the structure used to store info used by ":for".
3252 */
3253 void
3254free_for_info(fi_void)
3255 void *fi_void;
3256{
Bram Moolenaar33570922005-01-25 22:26:29 +00003257 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003259 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003260 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003262 list_unref(fi->fi_list);
3263 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003264 vim_free(fi);
3265}
3266
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3268
3269 void
3270set_context_for_expression(xp, arg, cmdidx)
3271 expand_T *xp;
3272 char_u *arg;
3273 cmdidx_T cmdidx;
3274{
3275 int got_eq = FALSE;
3276 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 if (cmdidx == CMD_let)
3280 {
3281 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003282 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 {
3284 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003285 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 {
3287 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003288 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003289 if (vim_iswhite(*p))
3290 break;
3291 }
3292 return;
3293 }
3294 }
3295 else
3296 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3297 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 while ((xp->xp_pattern = vim_strpbrk(arg,
3299 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3300 {
3301 c = *xp->xp_pattern;
3302 if (c == '&')
3303 {
3304 c = xp->xp_pattern[1];
3305 if (c == '&')
3306 {
3307 ++xp->xp_pattern;
3308 xp->xp_context = cmdidx != CMD_let || got_eq
3309 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3310 }
3311 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003312 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003314 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3315 xp->xp_pattern += 2;
3316
3317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 }
3319 else if (c == '$')
3320 {
3321 /* environment variable */
3322 xp->xp_context = EXPAND_ENV_VARS;
3323 }
3324 else if (c == '=')
3325 {
3326 got_eq = TRUE;
3327 xp->xp_context = EXPAND_EXPRESSION;
3328 }
3329 else if (c == '<'
3330 && xp->xp_context == EXPAND_FUNCTIONS
3331 && vim_strchr(xp->xp_pattern, '(') == NULL)
3332 {
3333 /* Function name can start with "<SNR>" */
3334 break;
3335 }
3336 else if (cmdidx != CMD_let || got_eq)
3337 {
3338 if (c == '"') /* string */
3339 {
3340 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3341 if (c == '\\' && xp->xp_pattern[1] != NUL)
3342 ++xp->xp_pattern;
3343 xp->xp_context = EXPAND_NOTHING;
3344 }
3345 else if (c == '\'') /* literal string */
3346 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003347 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3349 /* skip */ ;
3350 xp->xp_context = EXPAND_NOTHING;
3351 }
3352 else if (c == '|')
3353 {
3354 if (xp->xp_pattern[1] == '|')
3355 {
3356 ++xp->xp_pattern;
3357 xp->xp_context = EXPAND_EXPRESSION;
3358 }
3359 else
3360 xp->xp_context = EXPAND_COMMANDS;
3361 }
3362 else
3363 xp->xp_context = EXPAND_EXPRESSION;
3364 }
3365 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003366 /* Doesn't look like something valid, expand as an expression
3367 * anyway. */
3368 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 arg = xp->xp_pattern;
3370 if (*arg != NUL)
3371 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3372 /* skip */ ;
3373 }
3374 xp->xp_pattern = arg;
3375}
3376
3377#endif /* FEAT_CMDL_COMPL */
3378
3379/*
3380 * ":1,25call func(arg1, arg2)" function call.
3381 */
3382 void
3383ex_call(eap)
3384 exarg_T *eap;
3385{
3386 char_u *arg = eap->arg;
3387 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003389 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003391 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 linenr_T lnum;
3393 int doesrange;
3394 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003395 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003396
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003397 if (eap->skip)
3398 {
3399 /* trans_function_name() doesn't work well when skipping, use eval0()
3400 * instead to skip to any following command, e.g. for:
3401 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003402 ++emsg_skip;
3403 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3404 clear_tv(&rettv);
3405 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003406 return;
3407 }
3408
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003409 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003410 if (fudi.fd_newkey != NULL)
3411 {
3412 /* Still need to give an error message for missing key. */
3413 EMSG2(_(e_dictkey), fudi.fd_newkey);
3414 vim_free(fudi.fd_newkey);
3415 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003416 if (tofree == NULL)
3417 return;
3418
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003419 /* Increase refcount on dictionary, it could get deleted when evaluating
3420 * the arguments. */
3421 if (fudi.fd_dict != NULL)
3422 ++fudi.fd_dict->dv_refcount;
3423
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003424 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003425 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003426 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427
Bram Moolenaar532c7802005-01-27 14:44:31 +00003428 /* Skip white space to allow ":call func ()". Not good, but required for
3429 * backward compatibility. */
3430 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003431 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003432
3433 if (*startarg != '(')
3434 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003435 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 goto end;
3437 }
3438
3439 /*
3440 * When skipping, evaluate the function once, to find the end of the
3441 * arguments.
3442 * When the function takes a range, this is discovered after the first
3443 * call, and the loop is broken.
3444 */
3445 if (eap->skip)
3446 {
3447 ++emsg_skip;
3448 lnum = eap->line2; /* do it once, also with an invalid range */
3449 }
3450 else
3451 lnum = eap->line1;
3452 for ( ; lnum <= eap->line2; ++lnum)
3453 {
3454 if (!eap->skip && eap->addr_count > 0)
3455 {
3456 curwin->w_cursor.lnum = lnum;
3457 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003458#ifdef FEAT_VIRTUALEDIT
3459 curwin->w_cursor.coladd = 0;
3460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 }
3462 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003463 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003464 eap->line1, eap->line2, &doesrange,
3465 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 {
3467 failed = TRUE;
3468 break;
3469 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003470
3471 /* Handle a function returning a Funcref, Dictionary or List. */
3472 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3473 {
3474 failed = TRUE;
3475 break;
3476 }
3477
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003478 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 if (doesrange || eap->skip)
3480 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003483 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003484 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003485 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003486 if (aborting())
3487 break;
3488 }
3489 if (eap->skip)
3490 --emsg_skip;
3491
3492 if (!failed)
3493 {
3494 /* Check for trailing illegal characters and a following command. */
3495 if (!ends_excmd(*arg))
3496 {
3497 emsg_severe = TRUE;
3498 EMSG(_(e_trailing));
3499 }
3500 else
3501 eap->nextcmd = check_nextcmd(arg);
3502 }
3503
3504end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003505 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003506 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507}
3508
3509/*
3510 * ":unlet[!] var1 ... " command.
3511 */
3512 void
3513ex_unlet(eap)
3514 exarg_T *eap;
3515{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003516 ex_unletlock(eap, eap->arg, 0);
3517}
3518
3519/*
3520 * ":lockvar" and ":unlockvar" commands
3521 */
3522 void
3523ex_lockvar(eap)
3524 exarg_T *eap;
3525{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003526 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003527 int deep = 2;
3528
3529 if (eap->forceit)
3530 deep = -1;
3531 else if (vim_isdigit(*arg))
3532 {
3533 deep = getdigits(&arg);
3534 arg = skipwhite(arg);
3535 }
3536
3537 ex_unletlock(eap, arg, deep);
3538}
3539
3540/*
3541 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3542 */
3543 static void
3544ex_unletlock(eap, argstart, deep)
3545 exarg_T *eap;
3546 char_u *argstart;
3547 int deep;
3548{
3549 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003552 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003553
3554 do
3555 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003556 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003557 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3558 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003559 if (lv.ll_name == NULL)
3560 error = TRUE; /* error but continue parsing */
3561 if (name_end == NULL || (!vim_iswhite(*name_end)
3562 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003564 if (name_end != NULL)
3565 {
3566 emsg_severe = TRUE;
3567 EMSG(_(e_trailing));
3568 }
3569 if (!(eap->skip || error))
3570 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 break;
3572 }
3573
3574 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003575 {
3576 if (eap->cmdidx == CMD_unlet)
3577 {
3578 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3579 error = TRUE;
3580 }
3581 else
3582 {
3583 if (do_lock_var(&lv, name_end, deep,
3584 eap->cmdidx == CMD_lockvar) == FAIL)
3585 error = TRUE;
3586 }
3587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003589 if (!eap->skip)
3590 clear_lval(&lv);
3591
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 arg = skipwhite(name_end);
3593 } while (!ends_excmd(*arg));
3594
3595 eap->nextcmd = check_nextcmd(arg);
3596}
3597
Bram Moolenaar8c711452005-01-14 21:53:12 +00003598 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003600 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003602 int forceit;
3603{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003604 int ret = OK;
3605 int cc;
3606
3607 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003608 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003609 cc = *name_end;
3610 *name_end = NUL;
3611
3612 /* Normal name or expanded name. */
3613 if (check_changedtick(lp->ll_name))
3614 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003615 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003616 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003617 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003618 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003619 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3620 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 else if (lp->ll_range)
3622 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003623 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624
3625 /* Delete a range of List items. */
3626 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3627 {
3628 li = lp->ll_li->li_next;
3629 listitem_remove(lp->ll_list, lp->ll_li);
3630 lp->ll_li = li;
3631 ++lp->ll_n1;
3632 }
3633 }
3634 else
3635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 /* unlet a List item. */
3638 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003641 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642 }
3643
3644 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003645}
3646
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647/*
3648 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003649 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 */
3651 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003652do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003654 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655{
Bram Moolenaar33570922005-01-25 22:26:29 +00003656 hashtab_T *ht;
3657 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003658 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003659 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660
Bram Moolenaar33570922005-01-25 22:26:29 +00003661 ht = find_var_ht(name, &varname);
3662 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003664 hi = hash_find(ht, varname);
3665 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003666 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003667 di = HI2DI(hi);
3668 if (var_check_fixed(di->di_flags, name)
3669 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003670 return FAIL;
3671 delete_var(ht, hi);
3672 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003675 if (forceit)
3676 return OK;
3677 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 return FAIL;
3679}
3680
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003681/*
3682 * Lock or unlock variable indicated by "lp".
3683 * "deep" is the levels to go (-1 for unlimited);
3684 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3685 */
3686 static int
3687do_lock_var(lp, name_end, deep, lock)
3688 lval_T *lp;
3689 char_u *name_end;
3690 int deep;
3691 int lock;
3692{
3693 int ret = OK;
3694 int cc;
3695 dictitem_T *di;
3696
3697 if (deep == 0) /* nothing to do */
3698 return OK;
3699
3700 if (lp->ll_tv == NULL)
3701 {
3702 cc = *name_end;
3703 *name_end = NUL;
3704
3705 /* Normal name or expanded name. */
3706 if (check_changedtick(lp->ll_name))
3707 ret = FAIL;
3708 else
3709 {
3710 di = find_var(lp->ll_name, NULL);
3711 if (di == NULL)
3712 ret = FAIL;
3713 else
3714 {
3715 if (lock)
3716 di->di_flags |= DI_FLAGS_LOCK;
3717 else
3718 di->di_flags &= ~DI_FLAGS_LOCK;
3719 item_lock(&di->di_tv, deep, lock);
3720 }
3721 }
3722 *name_end = cc;
3723 }
3724 else if (lp->ll_range)
3725 {
3726 listitem_T *li = lp->ll_li;
3727
3728 /* (un)lock a range of List items. */
3729 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3730 {
3731 item_lock(&li->li_tv, deep, lock);
3732 li = li->li_next;
3733 ++lp->ll_n1;
3734 }
3735 }
3736 else if (lp->ll_list != NULL)
3737 /* (un)lock a List item. */
3738 item_lock(&lp->ll_li->li_tv, deep, lock);
3739 else
3740 /* un(lock) a Dictionary item. */
3741 item_lock(&lp->ll_di->di_tv, deep, lock);
3742
3743 return ret;
3744}
3745
3746/*
3747 * Lock or unlock an item. "deep" is nr of levels to go.
3748 */
3749 static void
3750item_lock(tv, deep, lock)
3751 typval_T *tv;
3752 int deep;
3753 int lock;
3754{
3755 static int recurse = 0;
3756 list_T *l;
3757 listitem_T *li;
3758 dict_T *d;
3759 hashitem_T *hi;
3760 int todo;
3761
3762 if (recurse >= DICT_MAXNEST)
3763 {
3764 EMSG(_("E743: variable nested too deep for (un)lock"));
3765 return;
3766 }
3767 if (deep == 0)
3768 return;
3769 ++recurse;
3770
3771 /* lock/unlock the item itself */
3772 if (lock)
3773 tv->v_lock |= VAR_LOCKED;
3774 else
3775 tv->v_lock &= ~VAR_LOCKED;
3776
3777 switch (tv->v_type)
3778 {
3779 case VAR_LIST:
3780 if ((l = tv->vval.v_list) != NULL)
3781 {
3782 if (lock)
3783 l->lv_lock |= VAR_LOCKED;
3784 else
3785 l->lv_lock &= ~VAR_LOCKED;
3786 if (deep < 0 || deep > 1)
3787 /* recursive: lock/unlock the items the List contains */
3788 for (li = l->lv_first; li != NULL; li = li->li_next)
3789 item_lock(&li->li_tv, deep - 1, lock);
3790 }
3791 break;
3792 case VAR_DICT:
3793 if ((d = tv->vval.v_dict) != NULL)
3794 {
3795 if (lock)
3796 d->dv_lock |= VAR_LOCKED;
3797 else
3798 d->dv_lock &= ~VAR_LOCKED;
3799 if (deep < 0 || deep > 1)
3800 {
3801 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003802 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003803 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3804 {
3805 if (!HASHITEM_EMPTY(hi))
3806 {
3807 --todo;
3808 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3809 }
3810 }
3811 }
3812 }
3813 }
3814 --recurse;
3815}
3816
Bram Moolenaara40058a2005-07-11 22:42:07 +00003817/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003818 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3819 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003820 */
3821 static int
3822tv_islocked(tv)
3823 typval_T *tv;
3824{
3825 return (tv->v_lock & VAR_LOCKED)
3826 || (tv->v_type == VAR_LIST
3827 && tv->vval.v_list != NULL
3828 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3829 || (tv->v_type == VAR_DICT
3830 && tv->vval.v_dict != NULL
3831 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3832}
3833
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3835/*
3836 * Delete all "menutrans_" variables.
3837 */
3838 void
3839del_menutrans_vars()
3840{
Bram Moolenaar33570922005-01-25 22:26:29 +00003841 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003842 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843
Bram Moolenaar33570922005-01-25 22:26:29 +00003844 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003845 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003846 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003847 {
3848 if (!HASHITEM_EMPTY(hi))
3849 {
3850 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003851 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3852 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003853 }
3854 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003855 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856}
3857#endif
3858
3859#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3860
3861/*
3862 * Local string buffer for the next two functions to store a variable name
3863 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3864 * get_user_var_name().
3865 */
3866
3867static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3868
3869static char_u *varnamebuf = NULL;
3870static int varnamebuflen = 0;
3871
3872/*
3873 * Function to concatenate a prefix and a variable name.
3874 */
3875 static char_u *
3876cat_prefix_varname(prefix, name)
3877 int prefix;
3878 char_u *name;
3879{
3880 int len;
3881
3882 len = (int)STRLEN(name) + 3;
3883 if (len > varnamebuflen)
3884 {
3885 vim_free(varnamebuf);
3886 len += 10; /* some additional space */
3887 varnamebuf = alloc(len);
3888 if (varnamebuf == NULL)
3889 {
3890 varnamebuflen = 0;
3891 return NULL;
3892 }
3893 varnamebuflen = len;
3894 }
3895 *varnamebuf = prefix;
3896 varnamebuf[1] = ':';
3897 STRCPY(varnamebuf + 2, name);
3898 return varnamebuf;
3899}
3900
3901/*
3902 * Function given to ExpandGeneric() to obtain the list of user defined
3903 * (global/buffer/window/built-in) variable names.
3904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905 char_u *
3906get_user_var_name(xp, idx)
3907 expand_T *xp;
3908 int idx;
3909{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003910 static long_u gdone;
3911 static long_u bdone;
3912 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003913#ifdef FEAT_WINDOWS
3914 static long_u tdone;
3915#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003916 static int vidx;
3917 static hashitem_T *hi;
3918 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919
3920 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003921 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003922 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003923#ifdef FEAT_WINDOWS
3924 tdone = 0;
3925#endif
3926 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003927
3928 /* Global variables */
3929 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003931 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003932 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003933 else
3934 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003935 while (HASHITEM_EMPTY(hi))
3936 ++hi;
3937 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3938 return cat_prefix_varname('g', hi->hi_key);
3939 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003941
3942 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003943 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003946 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003948 else
3949 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 while (HASHITEM_EMPTY(hi))
3951 ++hi;
3952 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003954 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003956 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957 return (char_u *)"b:changedtick";
3958 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003959
3960 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003961 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003964 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003965 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003966 else
3967 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003968 while (HASHITEM_EMPTY(hi))
3969 ++hi;
3970 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003972
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003973#ifdef FEAT_WINDOWS
3974 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003975 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003976 if (tdone < ht->ht_used)
3977 {
3978 if (tdone++ == 0)
3979 hi = ht->ht_array;
3980 else
3981 ++hi;
3982 while (HASHITEM_EMPTY(hi))
3983 ++hi;
3984 return cat_prefix_varname('t', hi->hi_key);
3985 }
3986#endif
3987
Bram Moolenaar33570922005-01-25 22:26:29 +00003988 /* v: variables */
3989 if (vidx < VV_LEN)
3990 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991
3992 vim_free(varnamebuf);
3993 varnamebuf = NULL;
3994 varnamebuflen = 0;
3995 return NULL;
3996}
3997
3998#endif /* FEAT_CMDL_COMPL */
3999
4000/*
4001 * types for expressions.
4002 */
4003typedef enum
4004{
4005 TYPE_UNKNOWN = 0
4006 , TYPE_EQUAL /* == */
4007 , TYPE_NEQUAL /* != */
4008 , TYPE_GREATER /* > */
4009 , TYPE_GEQUAL /* >= */
4010 , TYPE_SMALLER /* < */
4011 , TYPE_SEQUAL /* <= */
4012 , TYPE_MATCH /* =~ */
4013 , TYPE_NOMATCH /* !~ */
4014} exptype_T;
4015
4016/*
4017 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004018 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4020 */
4021
4022/*
4023 * Handle zero level expression.
4024 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004025 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004026 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 * Return OK or FAIL.
4028 */
4029 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004030eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004032 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033 char_u **nextcmd;
4034 int evaluate;
4035{
4036 int ret;
4037 char_u *p;
4038
4039 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004040 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 if (ret == FAIL || !ends_excmd(*p))
4042 {
4043 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004044 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 /*
4046 * Report the invalid expression unless the expression evaluation has
4047 * been cancelled due to an aborting error, an interrupt, or an
4048 * exception.
4049 */
4050 if (!aborting())
4051 EMSG2(_(e_invexpr2), arg);
4052 ret = FAIL;
4053 }
4054 if (nextcmd != NULL)
4055 *nextcmd = check_nextcmd(p);
4056
4057 return ret;
4058}
4059
4060/*
4061 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004062 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 *
4064 * "arg" must point to the first non-white of the expression.
4065 * "arg" is advanced to the next non-white after the recognized expression.
4066 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004067 * Note: "rettv.v_lock" is not set.
4068 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 * Return OK or FAIL.
4070 */
4071 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004072eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004074 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 int evaluate;
4076{
4077 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004078 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079
4080 /*
4081 * Get the first variable.
4082 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004083 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 return FAIL;
4085
4086 if ((*arg)[0] == '?')
4087 {
4088 result = FALSE;
4089 if (evaluate)
4090 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004091 int error = FALSE;
4092
4093 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004094 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004095 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004096 if (error)
4097 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 }
4099
4100 /*
4101 * Get the second variable.
4102 */
4103 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004104 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 return FAIL;
4106
4107 /*
4108 * Check for the ":".
4109 */
4110 if ((*arg)[0] != ':')
4111 {
4112 EMSG(_("E109: Missing ':' after '?'"));
4113 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 return FAIL;
4116 }
4117
4118 /*
4119 * Get the third variable.
4120 */
4121 *arg = skipwhite(*arg + 1);
4122 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4123 {
4124 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004125 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 return FAIL;
4127 }
4128 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 }
4131
4132 return OK;
4133}
4134
4135/*
4136 * Handle first level expression:
4137 * expr2 || expr2 || expr2 logical OR
4138 *
4139 * "arg" must point to the first non-white of the expression.
4140 * "arg" is advanced to the next non-white after the recognized expression.
4141 *
4142 * Return OK or FAIL.
4143 */
4144 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004145eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004147 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 int evaluate;
4149{
Bram Moolenaar33570922005-01-25 22:26:29 +00004150 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 long result;
4152 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004153 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004154
4155 /*
4156 * Get the first variable.
4157 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004158 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 return FAIL;
4160
4161 /*
4162 * Repeat until there is no following "||".
4163 */
4164 first = TRUE;
4165 result = FALSE;
4166 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4167 {
4168 if (evaluate && first)
4169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004170 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004172 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004173 if (error)
4174 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 first = FALSE;
4176 }
4177
4178 /*
4179 * Get the second variable.
4180 */
4181 *arg = skipwhite(*arg + 2);
4182 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4183 return FAIL;
4184
4185 /*
4186 * Compute the result.
4187 */
4188 if (evaluate && !result)
4189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004192 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004193 if (error)
4194 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 }
4196 if (evaluate)
4197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004198 rettv->v_type = VAR_NUMBER;
4199 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 }
4201 }
4202
4203 return OK;
4204}
4205
4206/*
4207 * Handle second level expression:
4208 * expr3 && expr3 && expr3 logical AND
4209 *
4210 * "arg" must point to the first non-white of the expression.
4211 * "arg" is advanced to the next non-white after the recognized expression.
4212 *
4213 * Return OK or FAIL.
4214 */
4215 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004216eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004218 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 int evaluate;
4220{
Bram Moolenaar33570922005-01-25 22:26:29 +00004221 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 long result;
4223 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004224 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225
4226 /*
4227 * Get the first variable.
4228 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004229 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 return FAIL;
4231
4232 /*
4233 * Repeat until there is no following "&&".
4234 */
4235 first = TRUE;
4236 result = TRUE;
4237 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4238 {
4239 if (evaluate && first)
4240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004243 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004244 if (error)
4245 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 first = FALSE;
4247 }
4248
4249 /*
4250 * Get the second variable.
4251 */
4252 *arg = skipwhite(*arg + 2);
4253 if (eval4(arg, &var2, evaluate && result) == FAIL)
4254 return FAIL;
4255
4256 /*
4257 * Compute the result.
4258 */
4259 if (evaluate && result)
4260 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004264 if (error)
4265 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 }
4267 if (evaluate)
4268 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004269 rettv->v_type = VAR_NUMBER;
4270 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272 }
4273
4274 return OK;
4275}
4276
4277/*
4278 * Handle third level expression:
4279 * var1 == var2
4280 * var1 =~ var2
4281 * var1 != var2
4282 * var1 !~ var2
4283 * var1 > var2
4284 * var1 >= var2
4285 * var1 < var2
4286 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004287 * var1 is var2
4288 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 *
4290 * "arg" must point to the first non-white of the expression.
4291 * "arg" is advanced to the next non-white after the recognized expression.
4292 *
4293 * Return OK or FAIL.
4294 */
4295 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004298 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 int evaluate;
4300{
Bram Moolenaar33570922005-01-25 22:26:29 +00004301 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 char_u *p;
4303 int i;
4304 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004305 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 int len = 2;
4307 long n1, n2;
4308 char_u *s1, *s2;
4309 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4310 regmatch_T regmatch;
4311 int ic;
4312 char_u *save_cpo;
4313
4314 /*
4315 * Get the first variable.
4316 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004317 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 return FAIL;
4319
4320 p = *arg;
4321 switch (p[0])
4322 {
4323 case '=': if (p[1] == '=')
4324 type = TYPE_EQUAL;
4325 else if (p[1] == '~')
4326 type = TYPE_MATCH;
4327 break;
4328 case '!': if (p[1] == '=')
4329 type = TYPE_NEQUAL;
4330 else if (p[1] == '~')
4331 type = TYPE_NOMATCH;
4332 break;
4333 case '>': if (p[1] != '=')
4334 {
4335 type = TYPE_GREATER;
4336 len = 1;
4337 }
4338 else
4339 type = TYPE_GEQUAL;
4340 break;
4341 case '<': if (p[1] != '=')
4342 {
4343 type = TYPE_SMALLER;
4344 len = 1;
4345 }
4346 else
4347 type = TYPE_SEQUAL;
4348 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004349 case 'i': if (p[1] == 's')
4350 {
4351 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4352 len = 5;
4353 if (!vim_isIDc(p[len]))
4354 {
4355 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4356 type_is = TRUE;
4357 }
4358 }
4359 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361
4362 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004363 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 */
4365 if (type != TYPE_UNKNOWN)
4366 {
4367 /* extra question mark appended: ignore case */
4368 if (p[len] == '?')
4369 {
4370 ic = TRUE;
4371 ++len;
4372 }
4373 /* extra '#' appended: match case */
4374 else if (p[len] == '#')
4375 {
4376 ic = FALSE;
4377 ++len;
4378 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004379 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 else
4381 ic = p_ic;
4382
4383 /*
4384 * Get the second variable.
4385 */
4386 *arg = skipwhite(p + len);
4387 if (eval5(arg, &var2, evaluate) == FAIL)
4388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004389 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 return FAIL;
4391 }
4392
4393 if (evaluate)
4394 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004395 if (type_is && rettv->v_type != var2.v_type)
4396 {
4397 /* For "is" a different type always means FALSE, for "notis"
4398 * it means TRUE. */
4399 n1 = (type == TYPE_NEQUAL);
4400 }
4401 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4402 {
4403 if (type_is)
4404 {
4405 n1 = (rettv->v_type == var2.v_type
4406 && rettv->vval.v_list == var2.vval.v_list);
4407 if (type == TYPE_NEQUAL)
4408 n1 = !n1;
4409 }
4410 else if (rettv->v_type != var2.v_type
4411 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4412 {
4413 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004414 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004415 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004416 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004417 clear_tv(rettv);
4418 clear_tv(&var2);
4419 return FAIL;
4420 }
4421 else
4422 {
4423 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004424 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4425 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004426 if (type == TYPE_NEQUAL)
4427 n1 = !n1;
4428 }
4429 }
4430
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004431 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4432 {
4433 if (type_is)
4434 {
4435 n1 = (rettv->v_type == var2.v_type
4436 && rettv->vval.v_dict == var2.vval.v_dict);
4437 if (type == TYPE_NEQUAL)
4438 n1 = !n1;
4439 }
4440 else if (rettv->v_type != var2.v_type
4441 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4442 {
4443 if (rettv->v_type != var2.v_type)
4444 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4445 else
4446 EMSG(_("E736: Invalid operation for Dictionary"));
4447 clear_tv(rettv);
4448 clear_tv(&var2);
4449 return FAIL;
4450 }
4451 else
4452 {
4453 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004454 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4455 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004456 if (type == TYPE_NEQUAL)
4457 n1 = !n1;
4458 }
4459 }
4460
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004461 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4462 {
4463 if (rettv->v_type != var2.v_type
4464 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4465 {
4466 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004467 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004469 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004470 clear_tv(rettv);
4471 clear_tv(&var2);
4472 return FAIL;
4473 }
4474 else
4475 {
4476 /* Compare two Funcrefs for being equal or unequal. */
4477 if (rettv->vval.v_string == NULL
4478 || var2.vval.v_string == NULL)
4479 n1 = FALSE;
4480 else
4481 n1 = STRCMP(rettv->vval.v_string,
4482 var2.vval.v_string) == 0;
4483 if (type == TYPE_NEQUAL)
4484 n1 = !n1;
4485 }
4486 }
4487
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004488#ifdef FEAT_FLOAT
4489 /*
4490 * If one of the two variables is a float, compare as a float.
4491 * When using "=~" or "!~", always compare as string.
4492 */
4493 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4494 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4495 {
4496 float_T f1, f2;
4497
4498 if (rettv->v_type == VAR_FLOAT)
4499 f1 = rettv->vval.v_float;
4500 else
4501 f1 = get_tv_number(rettv);
4502 if (var2.v_type == VAR_FLOAT)
4503 f2 = var2.vval.v_float;
4504 else
4505 f2 = get_tv_number(&var2);
4506 n1 = FALSE;
4507 switch (type)
4508 {
4509 case TYPE_EQUAL: n1 = (f1 == f2); break;
4510 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4511 case TYPE_GREATER: n1 = (f1 > f2); break;
4512 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4513 case TYPE_SMALLER: n1 = (f1 < f2); break;
4514 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4515 case TYPE_UNKNOWN:
4516 case TYPE_MATCH:
4517 case TYPE_NOMATCH: break; /* avoid gcc warning */
4518 }
4519 }
4520#endif
4521
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 /*
4523 * If one of the two variables is a number, compare as a number.
4524 * When using "=~" or "!~", always compare as string.
4525 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004526 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4528 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004529 n1 = get_tv_number(rettv);
4530 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 switch (type)
4532 {
4533 case TYPE_EQUAL: n1 = (n1 == n2); break;
4534 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4535 case TYPE_GREATER: n1 = (n1 > n2); break;
4536 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4537 case TYPE_SMALLER: n1 = (n1 < n2); break;
4538 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4539 case TYPE_UNKNOWN:
4540 case TYPE_MATCH:
4541 case TYPE_NOMATCH: break; /* avoid gcc warning */
4542 }
4543 }
4544 else
4545 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004546 s1 = get_tv_string_buf(rettv, buf1);
4547 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4549 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4550 else
4551 i = 0;
4552 n1 = FALSE;
4553 switch (type)
4554 {
4555 case TYPE_EQUAL: n1 = (i == 0); break;
4556 case TYPE_NEQUAL: n1 = (i != 0); break;
4557 case TYPE_GREATER: n1 = (i > 0); break;
4558 case TYPE_GEQUAL: n1 = (i >= 0); break;
4559 case TYPE_SMALLER: n1 = (i < 0); break;
4560 case TYPE_SEQUAL: n1 = (i <= 0); break;
4561
4562 case TYPE_MATCH:
4563 case TYPE_NOMATCH:
4564 /* avoid 'l' flag in 'cpoptions' */
4565 save_cpo = p_cpo;
4566 p_cpo = (char_u *)"";
4567 regmatch.regprog = vim_regcomp(s2,
4568 RE_MAGIC + RE_STRING);
4569 regmatch.rm_ic = ic;
4570 if (regmatch.regprog != NULL)
4571 {
4572 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004573 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 if (type == TYPE_NOMATCH)
4575 n1 = !n1;
4576 }
4577 p_cpo = save_cpo;
4578 break;
4579
4580 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4581 }
4582 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004583 clear_tv(rettv);
4584 clear_tv(&var2);
4585 rettv->v_type = VAR_NUMBER;
4586 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
4588 }
4589
4590 return OK;
4591}
4592
4593/*
4594 * Handle fourth level expression:
4595 * + number addition
4596 * - number subtraction
4597 * . string concatenation
4598 *
4599 * "arg" must point to the first non-white of the expression.
4600 * "arg" is advanced to the next non-white after the recognized expression.
4601 *
4602 * Return OK or FAIL.
4603 */
4604 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004605eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004607 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 int evaluate;
4609{
Bram Moolenaar33570922005-01-25 22:26:29 +00004610 typval_T var2;
4611 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 int op;
4613 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004614#ifdef FEAT_FLOAT
4615 float_T f1 = 0, f2 = 0;
4616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 char_u *s1, *s2;
4618 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4619 char_u *p;
4620
4621 /*
4622 * Get the first variable.
4623 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004624 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 return FAIL;
4626
4627 /*
4628 * Repeat computing, until no '+', '-' or '.' is following.
4629 */
4630 for (;;)
4631 {
4632 op = **arg;
4633 if (op != '+' && op != '-' && op != '.')
4634 break;
4635
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004636 if ((op != '+' || rettv->v_type != VAR_LIST)
4637#ifdef FEAT_FLOAT
4638 && (op == '.' || rettv->v_type != VAR_FLOAT)
4639#endif
4640 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004641 {
4642 /* For "list + ...", an illegal use of the first operand as
4643 * a number cannot be determined before evaluating the 2nd
4644 * operand: if this is also a list, all is ok.
4645 * For "something . ...", "something - ..." or "non-list + ...",
4646 * we know that the first operand needs to be a string or number
4647 * without evaluating the 2nd operand. So check before to avoid
4648 * side effects after an error. */
4649 if (evaluate && get_tv_string_chk(rettv) == NULL)
4650 {
4651 clear_tv(rettv);
4652 return FAIL;
4653 }
4654 }
4655
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656 /*
4657 * Get the second variable.
4658 */
4659 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004660 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004662 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 return FAIL;
4664 }
4665
4666 if (evaluate)
4667 {
4668 /*
4669 * Compute the result.
4670 */
4671 if (op == '.')
4672 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004673 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4674 s2 = get_tv_string_buf_chk(&var2, buf2);
4675 if (s2 == NULL) /* type error ? */
4676 {
4677 clear_tv(rettv);
4678 clear_tv(&var2);
4679 return FAIL;
4680 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004681 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004682 clear_tv(rettv);
4683 rettv->v_type = VAR_STRING;
4684 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004686 else if (op == '+' && rettv->v_type == VAR_LIST
4687 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004688 {
4689 /* concatenate Lists */
4690 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4691 &var3) == FAIL)
4692 {
4693 clear_tv(rettv);
4694 clear_tv(&var2);
4695 return FAIL;
4696 }
4697 clear_tv(rettv);
4698 *rettv = var3;
4699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 else
4701 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004702 int error = FALSE;
4703
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004704#ifdef FEAT_FLOAT
4705 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004706 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004707 f1 = rettv->vval.v_float;
4708 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004709 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004710 else
4711#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004712 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004713 n1 = get_tv_number_chk(rettv, &error);
4714 if (error)
4715 {
4716 /* This can only happen for "list + non-list". For
4717 * "non-list + ..." or "something - ...", we returned
4718 * before evaluating the 2nd operand. */
4719 clear_tv(rettv);
4720 return FAIL;
4721 }
4722#ifdef FEAT_FLOAT
4723 if (var2.v_type == VAR_FLOAT)
4724 f1 = n1;
4725#endif
4726 }
4727#ifdef FEAT_FLOAT
4728 if (var2.v_type == VAR_FLOAT)
4729 {
4730 f2 = var2.vval.v_float;
4731 n2 = 0;
4732 }
4733 else
4734#endif
4735 {
4736 n2 = get_tv_number_chk(&var2, &error);
4737 if (error)
4738 {
4739 clear_tv(rettv);
4740 clear_tv(&var2);
4741 return FAIL;
4742 }
4743#ifdef FEAT_FLOAT
4744 if (rettv->v_type == VAR_FLOAT)
4745 f2 = n2;
4746#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004747 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004748 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004749
4750#ifdef FEAT_FLOAT
4751 /* If there is a float on either side the result is a float. */
4752 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4753 {
4754 if (op == '+')
4755 f1 = f1 + f2;
4756 else
4757 f1 = f1 - f2;
4758 rettv->v_type = VAR_FLOAT;
4759 rettv->vval.v_float = f1;
4760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004762#endif
4763 {
4764 if (op == '+')
4765 n1 = n1 + n2;
4766 else
4767 n1 = n1 - n2;
4768 rettv->v_type = VAR_NUMBER;
4769 rettv->vval.v_number = n1;
4770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004772 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
4774 }
4775 return OK;
4776}
4777
4778/*
4779 * Handle fifth level expression:
4780 * * number multiplication
4781 * / number division
4782 * % number modulo
4783 *
4784 * "arg" must point to the first non-white of the expression.
4785 * "arg" is advanced to the next non-white after the recognized expression.
4786 *
4787 * Return OK or FAIL.
4788 */
4789 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004790eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004794 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795{
Bram Moolenaar33570922005-01-25 22:26:29 +00004796 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 int op;
4798 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004799#ifdef FEAT_FLOAT
4800 int use_float = FALSE;
4801 float_T f1 = 0, f2;
4802#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004803 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804
4805 /*
4806 * Get the first variable.
4807 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004808 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 return FAIL;
4810
4811 /*
4812 * Repeat computing, until no '*', '/' or '%' is following.
4813 */
4814 for (;;)
4815 {
4816 op = **arg;
4817 if (op != '*' && op != '/' && op != '%')
4818 break;
4819
4820 if (evaluate)
4821 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004822#ifdef FEAT_FLOAT
4823 if (rettv->v_type == VAR_FLOAT)
4824 {
4825 f1 = rettv->vval.v_float;
4826 use_float = TRUE;
4827 n1 = 0;
4828 }
4829 else
4830#endif
4831 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004832 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004833 if (error)
4834 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
4836 else
4837 n1 = 0;
4838
4839 /*
4840 * Get the second variable.
4841 */
4842 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004843 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 return FAIL;
4845
4846 if (evaluate)
4847 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004848#ifdef FEAT_FLOAT
4849 if (var2.v_type == VAR_FLOAT)
4850 {
4851 if (!use_float)
4852 {
4853 f1 = n1;
4854 use_float = TRUE;
4855 }
4856 f2 = var2.vval.v_float;
4857 n2 = 0;
4858 }
4859 else
4860#endif
4861 {
4862 n2 = get_tv_number_chk(&var2, &error);
4863 clear_tv(&var2);
4864 if (error)
4865 return FAIL;
4866#ifdef FEAT_FLOAT
4867 if (use_float)
4868 f2 = n2;
4869#endif
4870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871
4872 /*
4873 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876#ifdef FEAT_FLOAT
4877 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004879 if (op == '*')
4880 f1 = f1 * f2;
4881 else if (op == '/')
4882 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004883# ifdef VMS
4884 /* VMS crashes on divide by zero, work around it */
4885 if (f2 == 0.0)
4886 {
4887 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004888 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004889 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004890 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004891 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004892 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004893 }
4894 else
4895 f1 = f1 / f2;
4896# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004897 /* We rely on the floating point library to handle divide
4898 * by zero to result in "inf" and not a crash. */
4899 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004900# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004904 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004905 return FAIL;
4906 }
4907 rettv->v_type = VAR_FLOAT;
4908 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 }
4910 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004913 if (op == '*')
4914 n1 = n1 * n2;
4915 else if (op == '/')
4916 {
4917 if (n2 == 0) /* give an error message? */
4918 {
4919 if (n1 == 0)
4920 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4921 else if (n1 < 0)
4922 n1 = -0x7fffffffL;
4923 else
4924 n1 = 0x7fffffffL;
4925 }
4926 else
4927 n1 = n1 / n2;
4928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930 {
4931 if (n2 == 0) /* give an error message? */
4932 n1 = 0;
4933 else
4934 n1 = n1 % n2;
4935 }
4936 rettv->v_type = VAR_NUMBER;
4937 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940 }
4941
4942 return OK;
4943}
4944
4945/*
4946 * Handle sixth level expression:
4947 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004948 * "string" string constant
4949 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 * &option-name option value
4951 * @r register contents
4952 * identifier variable value
4953 * function() function call
4954 * $VAR environment variable
4955 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004956 * [expr, expr] List
4957 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 *
4959 * Also handle:
4960 * ! in front logical NOT
4961 * - in front unary minus
4962 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004963 * trailing [] subscript in String or List
4964 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 *
4966 * "arg" must point to the first non-white of the expression.
4967 * "arg" is advanced to the next non-white after the recognized expression.
4968 *
4969 * Return OK or FAIL.
4970 */
4971 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004972eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004974 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004976 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 long n;
4979 int len;
4980 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 char_u *start_leader, *end_leader;
4982 int ret = OK;
4983 char_u *alias;
4984
4985 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004986 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004987 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004989 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990
4991 /*
4992 * Skip '!' and '-' characters. They are handled later.
4993 */
4994 start_leader = *arg;
4995 while (**arg == '!' || **arg == '-' || **arg == '+')
4996 *arg = skipwhite(*arg + 1);
4997 end_leader = *arg;
4998
4999 switch (**arg)
5000 {
5001 /*
5002 * Number constant.
5003 */
5004 case '0':
5005 case '1':
5006 case '2':
5007 case '3':
5008 case '4':
5009 case '5':
5010 case '6':
5011 case '7':
5012 case '8':
5013 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005014 {
5015#ifdef FEAT_FLOAT
5016 char_u *p = skipdigits(*arg + 1);
5017 int get_float = FALSE;
5018
5019 /* We accept a float when the format matches
5020 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005021 * strict to avoid backwards compatibility problems.
5022 * Don't look for a float after the "." operator, so that
5023 * ":let vers = 1.2.3" doesn't fail. */
5024 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005026 get_float = TRUE;
5027 p = skipdigits(p + 2);
5028 if (*p == 'e' || *p == 'E')
5029 {
5030 ++p;
5031 if (*p == '-' || *p == '+')
5032 ++p;
5033 if (!vim_isdigit(*p))
5034 get_float = FALSE;
5035 else
5036 p = skipdigits(p + 1);
5037 }
5038 if (ASCII_ISALPHA(*p) || *p == '.')
5039 get_float = FALSE;
5040 }
5041 if (get_float)
5042 {
5043 float_T f;
5044
5045 *arg += string2float(*arg, &f);
5046 if (evaluate)
5047 {
5048 rettv->v_type = VAR_FLOAT;
5049 rettv->vval.v_float = f;
5050 }
5051 }
5052 else
5053#endif
5054 {
5055 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5056 *arg += len;
5057 if (evaluate)
5058 {
5059 rettv->v_type = VAR_NUMBER;
5060 rettv->vval.v_number = n;
5061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 }
5063 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065
5066 /*
5067 * String constant: "string".
5068 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005069 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 break;
5071
5072 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005073 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005075 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005076 break;
5077
5078 /*
5079 * List: [expr, expr]
5080 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005081 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 break;
5083
5084 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005085 * Dictionary: {key: val, key: val}
5086 */
5087 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5088 break;
5089
5090 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005091 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005093 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 break;
5095
5096 /*
5097 * Environment variable: $VAR.
5098 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005099 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 break;
5101
5102 /*
5103 * Register contents: @r.
5104 */
5105 case '@': ++*arg;
5106 if (evaluate)
5107 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005108 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005109 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 }
5111 if (**arg != NUL)
5112 ++*arg;
5113 break;
5114
5115 /*
5116 * nested expression: (expression).
5117 */
5118 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005119 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 if (**arg == ')')
5121 ++*arg;
5122 else if (ret == OK)
5123 {
5124 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005125 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 ret = FAIL;
5127 }
5128 break;
5129
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 break;
5132 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005133
5134 if (ret == NOTDONE)
5135 {
5136 /*
5137 * Must be a variable or function name.
5138 * Can also be a curly-braces kind of name: {expr}.
5139 */
5140 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005141 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005142 if (alias != NULL)
5143 s = alias;
5144
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005145 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005146 ret = FAIL;
5147 else
5148 {
5149 if (**arg == '(') /* recursive! */
5150 {
5151 /* If "s" is the name of a variable of type VAR_FUNC
5152 * use its contents. */
5153 s = deref_func_name(s, &len);
5154
5155 /* Invoke the function. */
5156 ret = get_func_tv(s, len, rettv, arg,
5157 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005158 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005159
5160 /* If evaluate is FALSE rettv->v_type was not set in
5161 * get_func_tv, but it's needed in handle_subscript() to parse
5162 * what follows. So set it here. */
5163 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5164 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005165 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005166 rettv->v_type = VAR_FUNC;
5167 }
5168
Bram Moolenaar8c711452005-01-14 21:53:12 +00005169 /* Stop the expression evaluation when immediately
5170 * aborting on error, or when an interrupt occurred or
5171 * an exception was thrown but not caught. */
5172 if (aborting())
5173 {
5174 if (ret == OK)
5175 clear_tv(rettv);
5176 ret = FAIL;
5177 }
5178 }
5179 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005180 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005181 else
5182 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005184 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005185 }
5186
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 *arg = skipwhite(*arg);
5188
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005189 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5190 * expr(expr). */
5191 if (ret == OK)
5192 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193
5194 /*
5195 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5196 */
5197 if (ret == OK && evaluate && end_leader > start_leader)
5198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005199 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005200 int val = 0;
5201#ifdef FEAT_FLOAT
5202 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005203
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005204 if (rettv->v_type == VAR_FLOAT)
5205 f = rettv->vval.v_float;
5206 else
5207#endif
5208 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005209 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211 clear_tv(rettv);
5212 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005214 else
5215 {
5216 while (end_leader > start_leader)
5217 {
5218 --end_leader;
5219 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005220 {
5221#ifdef FEAT_FLOAT
5222 if (rettv->v_type == VAR_FLOAT)
5223 f = !f;
5224 else
5225#endif
5226 val = !val;
5227 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005228 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005229 {
5230#ifdef FEAT_FLOAT
5231 if (rettv->v_type == VAR_FLOAT)
5232 f = -f;
5233 else
5234#endif
5235 val = -val;
5236 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005237 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005238#ifdef FEAT_FLOAT
5239 if (rettv->v_type == VAR_FLOAT)
5240 {
5241 clear_tv(rettv);
5242 rettv->vval.v_float = f;
5243 }
5244 else
5245#endif
5246 {
5247 clear_tv(rettv);
5248 rettv->v_type = VAR_NUMBER;
5249 rettv->vval.v_number = val;
5250 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005252 }
5253
5254 return ret;
5255}
5256
5257/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005258 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5259 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005260 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5261 */
5262 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005263eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005265 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005267 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268{
5269 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005270 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 long len = -1;
5273 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005275 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005277 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005279 if (verbose)
5280 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005281 return FAIL;
5282 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005283#ifdef FEAT_FLOAT
5284 else if (rettv->v_type == VAR_FLOAT)
5285 {
5286 if (verbose)
5287 EMSG(_(e_float_as_string));
5288 return FAIL;
5289 }
5290#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005293 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005294 /*
5295 * dict.name
5296 */
5297 key = *arg + 1;
5298 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5299 ;
5300 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005302 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 }
5304 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005305 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005306 /*
5307 * something[idx]
5308 *
5309 * Get the (first) variable from inside the [].
5310 */
5311 *arg = skipwhite(*arg + 1);
5312 if (**arg == ':')
5313 empty1 = TRUE;
5314 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5315 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005316 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5317 {
5318 /* not a number or string */
5319 clear_tv(&var1);
5320 return FAIL;
5321 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005322
5323 /*
5324 * Get the second variable from inside the [:].
5325 */
5326 if (**arg == ':')
5327 {
5328 range = TRUE;
5329 *arg = skipwhite(*arg + 1);
5330 if (**arg == ']')
5331 empty2 = TRUE;
5332 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334 if (!empty1)
5335 clear_tv(&var1);
5336 return FAIL;
5337 }
5338 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5339 {
5340 /* not a number or string */
5341 if (!empty1)
5342 clear_tv(&var1);
5343 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005344 return FAIL;
5345 }
5346 }
5347
5348 /* Check for the ']'. */
5349 if (**arg != ']')
5350 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005351 if (verbose)
5352 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005353 clear_tv(&var1);
5354 if (range)
5355 clear_tv(&var2);
5356 return FAIL;
5357 }
5358 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 }
5360
5361 if (evaluate)
5362 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 n1 = 0;
5364 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005366 n1 = get_tv_number(&var1);
5367 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005368 }
5369 if (range)
5370 {
5371 if (empty2)
5372 n2 = -1;
5373 else
5374 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005375 n2 = get_tv_number(&var2);
5376 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005377 }
5378 }
5379
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005380 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 {
5382 case VAR_NUMBER:
5383 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005384 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 len = (long)STRLEN(s);
5386 if (range)
5387 {
5388 /* The resulting variable is a substring. If the indexes
5389 * are out of range the result is empty. */
5390 if (n1 < 0)
5391 {
5392 n1 = len + n1;
5393 if (n1 < 0)
5394 n1 = 0;
5395 }
5396 if (n2 < 0)
5397 n2 = len + n2;
5398 else if (n2 >= len)
5399 n2 = len;
5400 if (n1 >= len || n2 < 0 || n1 > n2)
5401 s = NULL;
5402 else
5403 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5404 }
5405 else
5406 {
5407 /* The resulting variable is a string of a single
5408 * character. If the index is too big or negative the
5409 * result is empty. */
5410 if (n1 >= len || n1 < 0)
5411 s = NULL;
5412 else
5413 s = vim_strnsave(s + n1, 1);
5414 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005415 clear_tv(rettv);
5416 rettv->v_type = VAR_STRING;
5417 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 break;
5419
5420 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005421 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005422 if (n1 < 0)
5423 n1 = len + n1;
5424 if (!empty1 && (n1 < 0 || n1 >= len))
5425 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005426 /* For a range we allow invalid values and return an empty
5427 * list. A list index out of range is an error. */
5428 if (!range)
5429 {
5430 if (verbose)
5431 EMSGN(_(e_listidx), n1);
5432 return FAIL;
5433 }
5434 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005435 }
5436 if (range)
5437 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005438 list_T *l;
5439 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005440
5441 if (n2 < 0)
5442 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005443 else if (n2 >= len)
5444 n2 = len - 1;
5445 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005446 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005447 l = list_alloc();
5448 if (l == NULL)
5449 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005450 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005451 n1 <= n2; ++n1)
5452 {
5453 if (list_append_tv(l, &item->li_tv) == FAIL)
5454 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005455 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005456 return FAIL;
5457 }
5458 item = item->li_next;
5459 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005460 clear_tv(rettv);
5461 rettv->v_type = VAR_LIST;
5462 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005463 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005464 }
5465 else
5466 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005467 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005468 clear_tv(rettv);
5469 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005470 }
5471 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005472
5473 case VAR_DICT:
5474 if (range)
5475 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005476 if (verbose)
5477 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478 if (len == -1)
5479 clear_tv(&var1);
5480 return FAIL;
5481 }
5482 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005483 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005484
5485 if (len == -1)
5486 {
5487 key = get_tv_string(&var1);
5488 if (*key == NUL)
5489 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005490 if (verbose)
5491 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005492 clear_tv(&var1);
5493 return FAIL;
5494 }
5495 }
5496
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005497 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005499 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005500 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005501 if (len == -1)
5502 clear_tv(&var1);
5503 if (item == NULL)
5504 return FAIL;
5505
5506 copy_tv(&item->di_tv, &var1);
5507 clear_tv(rettv);
5508 *rettv = var1;
5509 }
5510 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511 }
5512 }
5513
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005514 return OK;
5515}
5516
5517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 * Get an option value.
5519 * "arg" points to the '&' or '+' before the option name.
5520 * "arg" is advanced to character after the option name.
5521 * Return OK or FAIL.
5522 */
5523 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005524get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005526 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527 int evaluate;
5528{
5529 char_u *option_end;
5530 long numval;
5531 char_u *stringval;
5532 int opt_type;
5533 int c;
5534 int working = (**arg == '+'); /* has("+option") */
5535 int ret = OK;
5536 int opt_flags;
5537
5538 /*
5539 * Isolate the option name and find its value.
5540 */
5541 option_end = find_option_end(arg, &opt_flags);
5542 if (option_end == NULL)
5543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005544 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 EMSG2(_("E112: Option name missing: %s"), *arg);
5546 return FAIL;
5547 }
5548
5549 if (!evaluate)
5550 {
5551 *arg = option_end;
5552 return OK;
5553 }
5554
5555 c = *option_end;
5556 *option_end = NUL;
5557 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559
5560 if (opt_type == -3) /* invalid name */
5561 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 EMSG2(_("E113: Unknown option: %s"), *arg);
5564 ret = FAIL;
5565 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005566 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567 {
5568 if (opt_type == -2) /* hidden string option */
5569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005570 rettv->v_type = VAR_STRING;
5571 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 }
5573 else if (opt_type == -1) /* hidden number option */
5574 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005575 rettv->v_type = VAR_NUMBER;
5576 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577 }
5578 else if (opt_type == 1) /* number option */
5579 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005580 rettv->v_type = VAR_NUMBER;
5581 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 }
5583 else /* string option */
5584 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005585 rettv->v_type = VAR_STRING;
5586 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
5588 }
5589 else if (working && (opt_type == -2 || opt_type == -1))
5590 ret = FAIL;
5591
5592 *option_end = c; /* put back for error messages */
5593 *arg = option_end;
5594
5595 return ret;
5596}
5597
5598/*
5599 * Allocate a variable for a string constant.
5600 * Return OK or FAIL.
5601 */
5602 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005603get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 int evaluate;
5607{
5608 char_u *p;
5609 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 int extra = 0;
5611
5612 /*
5613 * Find the end of the string, skipping backslashed characters.
5614 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005615 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 {
5617 if (*p == '\\' && p[1] != NUL)
5618 {
5619 ++p;
5620 /* A "\<x>" form occupies at least 4 characters, and produces up
5621 * to 6 characters: reserve space for 2 extra */
5622 if (*p == '<')
5623 extra += 2;
5624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 }
5626
5627 if (*p != '"')
5628 {
5629 EMSG2(_("E114: Missing quote: %s"), *arg);
5630 return FAIL;
5631 }
5632
5633 /* If only parsing, set *arg and return here */
5634 if (!evaluate)
5635 {
5636 *arg = p + 1;
5637 return OK;
5638 }
5639
5640 /*
5641 * Copy the string into allocated memory, handling backslashed
5642 * characters.
5643 */
5644 name = alloc((unsigned)(p - *arg + extra));
5645 if (name == NULL)
5646 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 rettv->v_type = VAR_STRING;
5648 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649
Bram Moolenaar8c711452005-01-14 21:53:12 +00005650 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005651 {
5652 if (*p == '\\')
5653 {
5654 switch (*++p)
5655 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005656 case 'b': *name++ = BS; ++p; break;
5657 case 'e': *name++ = ESC; ++p; break;
5658 case 'f': *name++ = FF; ++p; break;
5659 case 'n': *name++ = NL; ++p; break;
5660 case 'r': *name++ = CAR; ++p; break;
5661 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662
5663 case 'X': /* hex: "\x1", "\x12" */
5664 case 'x':
5665 case 'u': /* Unicode: "\u0023" */
5666 case 'U':
5667 if (vim_isxdigit(p[1]))
5668 {
5669 int n, nr;
5670 int c = toupper(*p);
5671
5672 if (c == 'X')
5673 n = 2;
5674 else
5675 n = 4;
5676 nr = 0;
5677 while (--n >= 0 && vim_isxdigit(p[1]))
5678 {
5679 ++p;
5680 nr = (nr << 4) + hex2nr(*p);
5681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005682 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683#ifdef FEAT_MBYTE
5684 /* For "\u" store the number according to
5685 * 'encoding'. */
5686 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005687 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 else
5689#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005690 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692 break;
5693
5694 /* octal: "\1", "\12", "\123" */
5695 case '0':
5696 case '1':
5697 case '2':
5698 case '3':
5699 case '4':
5700 case '5':
5701 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 case '7': *name = *p++ - '0';
5703 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005705 *name = (*name << 3) + *p++ - '0';
5706 if (*p >= '0' && *p <= '7')
5707 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005709 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 break;
5711
5712 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 if (extra != 0)
5715 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005716 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005717 break;
5718 }
5719 /* FALLTHROUGH */
5720
Bram Moolenaar8c711452005-01-14 21:53:12 +00005721 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005722 break;
5723 }
5724 }
5725 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005729 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 *arg = p + 1;
5731
Bram Moolenaar071d4272004-06-13 20:20:40 +00005732 return OK;
5733}
5734
5735/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005736 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 * Return OK or FAIL.
5738 */
5739 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005740get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005743 int evaluate;
5744{
5745 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005746 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005747 int reduce = 0;
5748
5749 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005750 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005751 */
5752 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005757 break;
5758 ++reduce;
5759 ++p;
5760 }
5761 }
5762
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005766 return FAIL;
5767 }
5768
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005770 if (!evaluate)
5771 {
5772 *arg = p + 1;
5773 return OK;
5774 }
5775
5776 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005777 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005778 */
5779 str = alloc((unsigned)((p - *arg) - reduce));
5780 if (str == NULL)
5781 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 rettv->v_type = VAR_STRING;
5783 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005790 break;
5791 ++p;
5792 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005794 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005796 *arg = p + 1;
5797
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005798 return OK;
5799}
5800
5801/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005802 * Allocate a variable for a List and fill it from "*arg".
5803 * Return OK or FAIL.
5804 */
5805 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005806get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005808 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005809 int evaluate;
5810{
Bram Moolenaar33570922005-01-25 22:26:29 +00005811 list_T *l = NULL;
5812 typval_T tv;
5813 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005814
5815 if (evaluate)
5816 {
5817 l = list_alloc();
5818 if (l == NULL)
5819 return FAIL;
5820 }
5821
5822 *arg = skipwhite(*arg + 1);
5823 while (**arg != ']' && **arg != NUL)
5824 {
5825 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5826 goto failret;
5827 if (evaluate)
5828 {
5829 item = listitem_alloc();
5830 if (item != NULL)
5831 {
5832 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005833 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005834 list_append(l, item);
5835 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005836 else
5837 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005838 }
5839
5840 if (**arg == ']')
5841 break;
5842 if (**arg != ',')
5843 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005844 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005845 goto failret;
5846 }
5847 *arg = skipwhite(*arg + 1);
5848 }
5849
5850 if (**arg != ']')
5851 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853failret:
5854 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005855 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005856 return FAIL;
5857 }
5858
5859 *arg = skipwhite(*arg + 1);
5860 if (evaluate)
5861 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005862 rettv->v_type = VAR_LIST;
5863 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005864 ++l->lv_refcount;
5865 }
5866
5867 return OK;
5868}
5869
5870/*
5871 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005872 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005874 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005875list_alloc()
5876{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005877 list_T *l;
5878
5879 l = (list_T *)alloc_clear(sizeof(list_T));
5880 if (l != NULL)
5881 {
5882 /* Prepend the list to the list of lists for garbage collection. */
5883 if (first_list != NULL)
5884 first_list->lv_used_prev = l;
5885 l->lv_used_prev = NULL;
5886 l->lv_used_next = first_list;
5887 first_list = l;
5888 }
5889 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005890}
5891
5892/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005893 * Allocate an empty list for a return value.
5894 * Returns OK or FAIL.
5895 */
5896 static int
5897rettv_list_alloc(rettv)
5898 typval_T *rettv;
5899{
5900 list_T *l = list_alloc();
5901
5902 if (l == NULL)
5903 return FAIL;
5904
5905 rettv->vval.v_list = l;
5906 rettv->v_type = VAR_LIST;
5907 ++l->lv_refcount;
5908 return OK;
5909}
5910
5911/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 * Unreference a list: decrement the reference count and free it when it
5913 * becomes zero.
5914 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005915 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005917 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005919 if (l != NULL && --l->lv_refcount <= 0)
5920 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921}
5922
5923/*
5924 * Free a list, including all items it points to.
5925 * Ignores the reference count.
5926 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005927 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005928list_free(l, recurse)
5929 list_T *l;
5930 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931{
Bram Moolenaar33570922005-01-25 22:26:29 +00005932 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005933
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005934 /* Remove the list from the list of lists for garbage collection. */
5935 if (l->lv_used_prev == NULL)
5936 first_list = l->lv_used_next;
5937 else
5938 l->lv_used_prev->lv_used_next = l->lv_used_next;
5939 if (l->lv_used_next != NULL)
5940 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5941
Bram Moolenaard9fba312005-06-26 22:34:35 +00005942 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005944 /* Remove the item before deleting it. */
5945 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005946 if (recurse || (item->li_tv.v_type != VAR_LIST
5947 && item->li_tv.v_type != VAR_DICT))
5948 clear_tv(&item->li_tv);
5949 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950 }
5951 vim_free(l);
5952}
5953
5954/*
5955 * Allocate a list item.
5956 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005957 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958listitem_alloc()
5959{
Bram Moolenaar33570922005-01-25 22:26:29 +00005960 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961}
5962
5963/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005964 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005966 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005968 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005970 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971 vim_free(item);
5972}
5973
5974/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005975 * Remove a list item from a List and free it. Also clears the value.
5976 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005977 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005978listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005979 list_T *l;
5980 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005981{
5982 list_remove(l, item, item);
5983 listitem_free(item);
5984}
5985
5986/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005987 * Get the number of items in a list.
5988 */
5989 static long
5990list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005991 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005992{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993 if (l == NULL)
5994 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005995 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005996}
5997
5998/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005999 * Return TRUE when two lists have exactly the same values.
6000 */
6001 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006002list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006003 list_T *l1;
6004 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006005 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006006 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007{
Bram Moolenaar33570922005-01-25 22:26:29 +00006008 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006009
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006010 if (l1 == NULL || l2 == NULL)
6011 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006012 if (l1 == l2)
6013 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006014 if (list_len(l1) != list_len(l2))
6015 return FALSE;
6016
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006017 for (item1 = l1->lv_first, item2 = l2->lv_first;
6018 item1 != NULL && item2 != NULL;
6019 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006020 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006021 return FALSE;
6022 return item1 == NULL && item2 == NULL;
6023}
6024
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006025#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6026 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006027/*
6028 * Return the dictitem that an entry in a hashtable points to.
6029 */
6030 dictitem_T *
6031dict_lookup(hi)
6032 hashitem_T *hi;
6033{
6034 return HI2DI(hi);
6035}
6036#endif
6037
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006038/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006039 * Return TRUE when two dictionaries have exactly the same key/values.
6040 */
6041 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006042dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006043 dict_T *d1;
6044 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006045 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006046 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006047{
Bram Moolenaar33570922005-01-25 22:26:29 +00006048 hashitem_T *hi;
6049 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006050 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006051
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006052 if (d1 == NULL || d2 == NULL)
6053 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006054 if (d1 == d2)
6055 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006056 if (dict_len(d1) != dict_len(d2))
6057 return FALSE;
6058
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006059 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006061 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006062 if (!HASHITEM_EMPTY(hi))
6063 {
6064 item2 = dict_find(d2, hi->hi_key, -1);
6065 if (item2 == NULL)
6066 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006067 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006068 return FALSE;
6069 --todo;
6070 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006071 }
6072 return TRUE;
6073}
6074
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006075static int tv_equal_recurse_limit;
6076
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006077/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006078 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006079 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006080 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006081 */
6082 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006083tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006084 typval_T *tv1;
6085 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006086 int ic; /* ignore case */
6087 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006088{
6089 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006090 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006091 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006092 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006093
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006094 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006095 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006096
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006097 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006098 * recursiveness to a limit. We guess they are equal then.
6099 * A fixed limit has the problem of still taking an awful long time.
6100 * Reduce the limit every time running into it. That should work fine for
6101 * deeply linked structures that are not recursively linked and catch
6102 * recursiveness quickly. */
6103 if (!recursive)
6104 tv_equal_recurse_limit = 1000;
6105 if (recursive_cnt >= tv_equal_recurse_limit)
6106 {
6107 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006108 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006109 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006110
6111 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006112 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006113 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006114 ++recursive_cnt;
6115 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6116 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006117 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006118
6119 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006120 ++recursive_cnt;
6121 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6122 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006123 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006124
6125 case VAR_FUNC:
6126 return (tv1->vval.v_string != NULL
6127 && tv2->vval.v_string != NULL
6128 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6129
6130 case VAR_NUMBER:
6131 return tv1->vval.v_number == tv2->vval.v_number;
6132
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006133#ifdef FEAT_FLOAT
6134 case VAR_FLOAT:
6135 return tv1->vval.v_float == tv2->vval.v_float;
6136#endif
6137
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006138 case VAR_STRING:
6139 s1 = get_tv_string_buf(tv1, buf1);
6140 s2 = get_tv_string_buf(tv2, buf2);
6141 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006142 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006143
6144 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006145 return TRUE;
6146}
6147
6148/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006149 * Locate item with index "n" in list "l" and return it.
6150 * A negative index is counted from the end; -1 is the last item.
6151 * Returns NULL when "n" is out of range.
6152 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006153 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006156 long n;
6157{
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006159 long idx;
6160
6161 if (l == NULL)
6162 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006163
6164 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006165 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006166 n = l->lv_len + n;
6167
6168 /* Check for index out of range. */
6169 if (n < 0 || n >= l->lv_len)
6170 return NULL;
6171
6172 /* When there is a cached index may start search from there. */
6173 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006174 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006175 if (n < l->lv_idx / 2)
6176 {
6177 /* closest to the start of the list */
6178 item = l->lv_first;
6179 idx = 0;
6180 }
6181 else if (n > (l->lv_idx + l->lv_len) / 2)
6182 {
6183 /* closest to the end of the list */
6184 item = l->lv_last;
6185 idx = l->lv_len - 1;
6186 }
6187 else
6188 {
6189 /* closest to the cached index */
6190 item = l->lv_idx_item;
6191 idx = l->lv_idx;
6192 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006193 }
6194 else
6195 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006196 if (n < l->lv_len / 2)
6197 {
6198 /* closest to the start of the list */
6199 item = l->lv_first;
6200 idx = 0;
6201 }
6202 else
6203 {
6204 /* closest to the end of the list */
6205 item = l->lv_last;
6206 idx = l->lv_len - 1;
6207 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006208 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006209
6210 while (n > idx)
6211 {
6212 /* search forward */
6213 item = item->li_next;
6214 ++idx;
6215 }
6216 while (n < idx)
6217 {
6218 /* search backward */
6219 item = item->li_prev;
6220 --idx;
6221 }
6222
6223 /* cache the used index */
6224 l->lv_idx = idx;
6225 l->lv_idx_item = item;
6226
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006227 return item;
6228}
6229
6230/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006231 * Get list item "l[idx]" as a number.
6232 */
6233 static long
6234list_find_nr(l, idx, errorp)
6235 list_T *l;
6236 long idx;
6237 int *errorp; /* set to TRUE when something wrong */
6238{
6239 listitem_T *li;
6240
6241 li = list_find(l, idx);
6242 if (li == NULL)
6243 {
6244 if (errorp != NULL)
6245 *errorp = TRUE;
6246 return -1L;
6247 }
6248 return get_tv_number_chk(&li->li_tv, errorp);
6249}
6250
6251/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006252 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6253 */
6254 char_u *
6255list_find_str(l, idx)
6256 list_T *l;
6257 long idx;
6258{
6259 listitem_T *li;
6260
6261 li = list_find(l, idx - 1);
6262 if (li == NULL)
6263 {
6264 EMSGN(_(e_listidx), idx);
6265 return NULL;
6266 }
6267 return get_tv_string(&li->li_tv);
6268}
6269
6270/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006271 * Locate "item" list "l" and return its index.
6272 * Returns -1 when "item" is not in the list.
6273 */
6274 static long
6275list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 list_T *l;
6277 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006278{
6279 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006280 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006281
6282 if (l == NULL)
6283 return -1;
6284 idx = 0;
6285 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6286 ++idx;
6287 if (li == NULL)
6288 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006289 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006290}
6291
6292/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293 * Append item "item" to the end of list "l".
6294 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006295 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006297 list_T *l;
6298 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006299{
6300 if (l->lv_last == NULL)
6301 {
6302 /* empty list */
6303 l->lv_first = item;
6304 l->lv_last = item;
6305 item->li_prev = NULL;
6306 }
6307 else
6308 {
6309 l->lv_last->li_next = item;
6310 item->li_prev = l->lv_last;
6311 l->lv_last = item;
6312 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006314 item->li_next = NULL;
6315}
6316
6317/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006318 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006319 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006321 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006323 list_T *l;
6324 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006325{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006326 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006327
Bram Moolenaar05159a02005-02-26 23:04:13 +00006328 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006329 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006330 copy_tv(tv, &li->li_tv);
6331 list_append(l, li);
6332 return OK;
6333}
6334
6335/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006336 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006337 * Return FAIL when out of memory.
6338 */
6339 int
6340list_append_dict(list, dict)
6341 list_T *list;
6342 dict_T *dict;
6343{
6344 listitem_T *li = listitem_alloc();
6345
6346 if (li == NULL)
6347 return FAIL;
6348 li->li_tv.v_type = VAR_DICT;
6349 li->li_tv.v_lock = 0;
6350 li->li_tv.vval.v_dict = dict;
6351 list_append(list, li);
6352 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006353 return OK;
6354}
6355
6356/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006357 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006358 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006359 * Returns FAIL when out of memory.
6360 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006361 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006362list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006363 list_T *l;
6364 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006365 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006366{
6367 listitem_T *li = listitem_alloc();
6368
6369 if (li == NULL)
6370 return FAIL;
6371 list_append(l, li);
6372 li->li_tv.v_type = VAR_STRING;
6373 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006374 if (str == NULL)
6375 li->li_tv.vval.v_string = NULL;
6376 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006377 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006378 return FAIL;
6379 return OK;
6380}
6381
6382/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006383 * Append "n" to list "l".
6384 * Returns FAIL when out of memory.
6385 */
6386 static int
6387list_append_number(l, n)
6388 list_T *l;
6389 varnumber_T n;
6390{
6391 listitem_T *li;
6392
6393 li = listitem_alloc();
6394 if (li == NULL)
6395 return FAIL;
6396 li->li_tv.v_type = VAR_NUMBER;
6397 li->li_tv.v_lock = 0;
6398 li->li_tv.vval.v_number = n;
6399 list_append(l, li);
6400 return OK;
6401}
6402
6403/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006404 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006405 * If "item" is NULL append at the end.
6406 * Return FAIL when out of memory.
6407 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006408 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006409list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006410 list_T *l;
6411 typval_T *tv;
6412 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006413{
Bram Moolenaar33570922005-01-25 22:26:29 +00006414 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006415
6416 if (ni == NULL)
6417 return FAIL;
6418 copy_tv(tv, &ni->li_tv);
6419 if (item == NULL)
6420 /* Append new item at end of list. */
6421 list_append(l, ni);
6422 else
6423 {
6424 /* Insert new item before existing item. */
6425 ni->li_prev = item->li_prev;
6426 ni->li_next = item;
6427 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006428 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006430 ++l->lv_idx;
6431 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006433 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006435 l->lv_idx_item = NULL;
6436 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006437 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006438 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006439 }
6440 return OK;
6441}
6442
6443/*
6444 * Extend "l1" with "l2".
6445 * If "bef" is NULL append at the end, otherwise insert before this item.
6446 * Returns FAIL when out of memory.
6447 */
6448 static int
6449list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006450 list_T *l1;
6451 list_T *l2;
6452 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453{
Bram Moolenaar33570922005-01-25 22:26:29 +00006454 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006455 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006456
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006457 /* We also quit the loop when we have inserted the original item count of
6458 * the list, avoid a hang when we extend a list with itself. */
6459 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006460 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6461 return FAIL;
6462 return OK;
6463}
6464
6465/*
6466 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6467 * Return FAIL when out of memory.
6468 */
6469 static int
6470list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006471 list_T *l1;
6472 list_T *l2;
6473 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474{
Bram Moolenaar33570922005-01-25 22:26:29 +00006475 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006476
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006477 if (l1 == NULL || l2 == NULL)
6478 return FAIL;
6479
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006480 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006481 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006482 if (l == NULL)
6483 return FAIL;
6484 tv->v_type = VAR_LIST;
6485 tv->vval.v_list = l;
6486
6487 /* append all items from the second list */
6488 return list_extend(l, l2, NULL);
6489}
6490
6491/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006492 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006493 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006494 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006495 * Returns NULL when out of memory.
6496 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006498list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006500 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006501 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006502{
Bram Moolenaar33570922005-01-25 22:26:29 +00006503 list_T *copy;
6504 listitem_T *item;
6505 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506
6507 if (orig == NULL)
6508 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006509
6510 copy = list_alloc();
6511 if (copy != NULL)
6512 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006513 if (copyID != 0)
6514 {
6515 /* Do this before adding the items, because one of the items may
6516 * refer back to this list. */
6517 orig->lv_copyID = copyID;
6518 orig->lv_copylist = copy;
6519 }
6520 for (item = orig->lv_first; item != NULL && !got_int;
6521 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006522 {
6523 ni = listitem_alloc();
6524 if (ni == NULL)
6525 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006526 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006527 {
6528 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6529 {
6530 vim_free(ni);
6531 break;
6532 }
6533 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006535 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006536 list_append(copy, ni);
6537 }
6538 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006539 if (item != NULL)
6540 {
6541 list_unref(copy);
6542 copy = NULL;
6543 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006544 }
6545
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006546 return copy;
6547}
6548
6549/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006551 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006552 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006553 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006554list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006555 list_T *l;
6556 listitem_T *item;
6557 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558{
Bram Moolenaar33570922005-01-25 22:26:29 +00006559 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561 /* notify watchers */
6562 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006563 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006564 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 list_fix_watch(l, ip);
6566 if (ip == item2)
6567 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006568 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006569
6570 if (item2->li_next == NULL)
6571 l->lv_last = item->li_prev;
6572 else
6573 item2->li_next->li_prev = item->li_prev;
6574 if (item->li_prev == NULL)
6575 l->lv_first = item2->li_next;
6576 else
6577 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006578 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579}
6580
6581/*
6582 * Return an allocated string with the string representation of a list.
6583 * May return NULL.
6584 */
6585 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006586list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006587 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006588 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589{
6590 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591
6592 if (tv->vval.v_list == NULL)
6593 return NULL;
6594 ga_init2(&ga, (int)sizeof(char), 80);
6595 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006596 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006597 {
6598 vim_free(ga.ga_data);
6599 return NULL;
6600 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006601 ga_append(&ga, ']');
6602 ga_append(&ga, NUL);
6603 return (char_u *)ga.ga_data;
6604}
6605
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006606typedef struct join_S {
6607 char_u *s;
6608 char_u *tofree;
6609} join_T;
6610
6611 static int
6612list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6613 garray_T *gap; /* to store the result in */
6614 list_T *l;
6615 char_u *sep;
6616 int echo_style;
6617 int copyID;
6618 garray_T *join_gap; /* to keep each list item string */
6619{
6620 int i;
6621 join_T *p;
6622 int len;
6623 int sumlen = 0;
6624 int first = TRUE;
6625 char_u *tofree;
6626 char_u numbuf[NUMBUFLEN];
6627 listitem_T *item;
6628 char_u *s;
6629
6630 /* Stringify each item in the list. */
6631 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6632 {
6633 if (echo_style)
6634 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6635 else
6636 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6637 if (s == NULL)
6638 return FAIL;
6639
6640 len = (int)STRLEN(s);
6641 sumlen += len;
6642
6643 ga_grow(join_gap, 1);
6644 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6645 if (tofree != NULL || s != numbuf)
6646 {
6647 p->s = s;
6648 p->tofree = tofree;
6649 }
6650 else
6651 {
6652 p->s = vim_strnsave(s, len);
6653 p->tofree = p->s;
6654 }
6655
6656 line_breakcheck();
6657 }
6658
6659 /* Allocate result buffer with its total size, avoid re-allocation and
6660 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6661 if (join_gap->ga_len >= 2)
6662 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6663 if (ga_grow(gap, sumlen + 2) == FAIL)
6664 return FAIL;
6665
6666 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6667 {
6668 if (first)
6669 first = FALSE;
6670 else
6671 ga_concat(gap, sep);
6672 p = ((join_T *)join_gap->ga_data) + i;
6673
6674 if (p->s != NULL)
6675 ga_concat(gap, p->s);
6676 line_breakcheck();
6677 }
6678
6679 return OK;
6680}
6681
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006683 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006684 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006685 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006686 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006687 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006688list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006689 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006690 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006691 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006692 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006693 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006694{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006695 garray_T join_ga;
6696 int retval;
6697 join_T *p;
6698 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006699
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006700 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6701 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6702
6703 /* Dispose each item in join_ga. */
6704 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006705 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006706 p = (join_T *)join_ga.ga_data;
6707 for (i = 0; i < join_ga.ga_len; ++i)
6708 {
6709 vim_free(p->tofree);
6710 ++p;
6711 }
6712 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006713 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006714
6715 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006716}
6717
6718/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006719 * Garbage collection for lists and dictionaries.
6720 *
6721 * We use reference counts to be able to free most items right away when they
6722 * are no longer used. But for composite items it's possible that it becomes
6723 * unused while the reference count is > 0: When there is a recursive
6724 * reference. Example:
6725 * :let l = [1, 2, 3]
6726 * :let d = {9: l}
6727 * :let l[1] = d
6728 *
6729 * Since this is quite unusual we handle this with garbage collection: every
6730 * once in a while find out which lists and dicts are not referenced from any
6731 * variable.
6732 *
6733 * Here is a good reference text about garbage collection (refers to Python
6734 * but it applies to all reference-counting mechanisms):
6735 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006736 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006737
6738/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006739 * Do garbage collection for lists and dicts.
6740 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006741 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006742 int
6743garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006744{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006745 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006746 buf_T *buf;
6747 win_T *wp;
6748 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006749 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006750 int did_free;
6751 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006752#ifdef FEAT_WINDOWS
6753 tabpage_T *tp;
6754#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006755
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006756 /* Only do this once. */
6757 want_garbage_collect = FALSE;
6758 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006759 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006760
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006761 /* We advance by two because we add one for items referenced through
6762 * previous_funccal. */
6763 current_copyID += COPYID_INC;
6764 copyID = current_copyID;
6765
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006766 /*
6767 * 1. Go through all accessible variables and mark all lists and dicts
6768 * with copyID.
6769 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006770
6771 /* Don't free variables in the previous_funccal list unless they are only
6772 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006773 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006774 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6775 {
6776 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6777 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6778 }
6779
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006780 /* script-local variables */
6781 for (i = 1; i <= ga_scripts.ga_len; ++i)
6782 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6783
6784 /* buffer-local variables */
6785 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006786 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006787
6788 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006789 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006790 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006791#ifdef FEAT_AUTOCMD
6792 if (aucmd_win != NULL)
6793 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6794#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006795
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006796#ifdef FEAT_WINDOWS
6797 /* tabpage-local variables */
6798 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006799 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006800#endif
6801
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006802 /* global variables */
6803 set_ref_in_ht(&globvarht, copyID);
6804
6805 /* function-local variables */
6806 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6807 {
6808 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6809 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6810 }
6811
Bram Moolenaard812df62008-11-09 12:46:09 +00006812 /* v: vars */
6813 set_ref_in_ht(&vimvarht, copyID);
6814
Bram Moolenaar1dced572012-04-05 16:54:08 +02006815#ifdef FEAT_LUA
6816 set_ref_in_lua(copyID);
6817#endif
6818
Bram Moolenaardb913952012-06-29 12:54:53 +02006819#ifdef FEAT_PYTHON
6820 set_ref_in_python(copyID);
6821#endif
6822
6823#ifdef FEAT_PYTHON3
6824 set_ref_in_python3(copyID);
6825#endif
6826
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006827 /*
6828 * 2. Free lists and dictionaries that are not referenced.
6829 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006830 did_free = free_unref_items(copyID);
6831
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006832 /*
6833 * 3. Check if any funccal can be freed now.
6834 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006835 for (pfc = &previous_funccal; *pfc != NULL; )
6836 {
6837 if (can_free_funccal(*pfc, copyID))
6838 {
6839 fc = *pfc;
6840 *pfc = fc->caller;
6841 free_funccal(fc, TRUE);
6842 did_free = TRUE;
6843 did_free_funccal = TRUE;
6844 }
6845 else
6846 pfc = &(*pfc)->caller;
6847 }
6848 if (did_free_funccal)
6849 /* When a funccal was freed some more items might be garbage
6850 * collected, so run again. */
6851 (void)garbage_collect();
6852
6853 return did_free;
6854}
6855
6856/*
6857 * Free lists and dictionaries that are no longer referenced.
6858 */
6859 static int
6860free_unref_items(copyID)
6861 int copyID;
6862{
6863 dict_T *dd;
6864 list_T *ll;
6865 int did_free = FALSE;
6866
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006868 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869 */
6870 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006872 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006873 /* Free the Dictionary and ordinary items it contains, but don't
6874 * recurse into Lists and Dictionaries, they will be in the list
6875 * of dicts or list of lists. */
6876 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877 did_free = TRUE;
6878
6879 /* restart, next dict may also have been freed */
6880 dd = first_dict;
6881 }
6882 else
6883 dd = dd->dv_used_next;
6884
6885 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006886 * Go through the list of lists and free items without the copyID.
6887 * But don't free a list that has a watcher (used in a for loop), these
6888 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006889 */
6890 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006891 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6892 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006894 /* Free the List and ordinary items it contains, but don't recurse
6895 * into Lists and Dictionaries, they will be in the list of dicts
6896 * or list of lists. */
6897 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 did_free = TRUE;
6899
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006900 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901 ll = first_list;
6902 }
6903 else
6904 ll = ll->lv_used_next;
6905
6906 return did_free;
6907}
6908
6909/*
6910 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6911 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006912 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913set_ref_in_ht(ht, copyID)
6914 hashtab_T *ht;
6915 int copyID;
6916{
6917 int todo;
6918 hashitem_T *hi;
6919
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006920 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006921 for (hi = ht->ht_array; todo > 0; ++hi)
6922 if (!HASHITEM_EMPTY(hi))
6923 {
6924 --todo;
6925 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6926 }
6927}
6928
6929/*
6930 * Mark all lists and dicts referenced through list "l" with "copyID".
6931 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006932 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006933set_ref_in_list(l, copyID)
6934 list_T *l;
6935 int copyID;
6936{
6937 listitem_T *li;
6938
6939 for (li = l->lv_first; li != NULL; li = li->li_next)
6940 set_ref_in_item(&li->li_tv, copyID);
6941}
6942
6943/*
6944 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6945 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006946 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006947set_ref_in_item(tv, copyID)
6948 typval_T *tv;
6949 int copyID;
6950{
6951 dict_T *dd;
6952 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006953
6954 switch (tv->v_type)
6955 {
6956 case VAR_DICT:
6957 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006958 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006959 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006960 /* Didn't see this dict yet. */
6961 dd->dv_copyID = copyID;
6962 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006964 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006965
6966 case VAR_LIST:
6967 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006968 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006969 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006970 /* Didn't see this list yet. */
6971 ll->lv_copyID = copyID;
6972 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006973 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006974 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006975 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006976 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006977}
6978
6979/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006980 * Allocate an empty header for a dictionary.
6981 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006982 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006983dict_alloc()
6984{
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006986
Bram Moolenaar33570922005-01-25 22:26:29 +00006987 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006988 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006989 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006990 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 if (first_dict != NULL)
6992 first_dict->dv_used_prev = d;
6993 d->dv_used_next = first_dict;
6994 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006995 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996
Bram Moolenaar33570922005-01-25 22:26:29 +00006997 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006998 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006999 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007000 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007001 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007002 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007003 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007004}
7005
7006/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007007 * Allocate an empty dict for a return value.
7008 * Returns OK or FAIL.
7009 */
7010 static int
7011rettv_dict_alloc(rettv)
7012 typval_T *rettv;
7013{
7014 dict_T *d = dict_alloc();
7015
7016 if (d == NULL)
7017 return FAIL;
7018
7019 rettv->vval.v_dict = d;
7020 rettv->v_type = VAR_DICT;
7021 ++d->dv_refcount;
7022 return OK;
7023}
7024
7025
7026/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007027 * Unreference a Dictionary: decrement the reference count and free it when it
7028 * becomes zero.
7029 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007030 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007031dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007032 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007034 if (d != NULL && --d->dv_refcount <= 0)
7035 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007036}
7037
7038/*
7039 * Free a Dictionary, including all items it contains.
7040 * Ignores the reference count.
7041 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007042 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007043dict_free(d, recurse)
7044 dict_T *d;
7045 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007046{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007047 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007048 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007049 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007050
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051 /* Remove the dict from the list of dicts for garbage collection. */
7052 if (d->dv_used_prev == NULL)
7053 first_dict = d->dv_used_next;
7054 else
7055 d->dv_used_prev->dv_used_next = d->dv_used_next;
7056 if (d->dv_used_next != NULL)
7057 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7058
7059 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007060 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007061 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007062 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007063 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007064 if (!HASHITEM_EMPTY(hi))
7065 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007066 /* Remove the item before deleting it, just in case there is
7067 * something recursive causing trouble. */
7068 di = HI2DI(hi);
7069 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007070 if (recurse || (di->di_tv.v_type != VAR_LIST
7071 && di->di_tv.v_type != VAR_DICT))
7072 clear_tv(&di->di_tv);
7073 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007074 --todo;
7075 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007077 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007078 vim_free(d);
7079}
7080
7081/*
7082 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007083 * The "key" is copied to the new item.
7084 * Note that the value of the item "di_tv" still needs to be initialized!
7085 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007086 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007087 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007088dictitem_alloc(key)
7089 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007090{
Bram Moolenaar33570922005-01-25 22:26:29 +00007091 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007093 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007094 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007095 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007096 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007097 di->di_flags = 0;
7098 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007099 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007100}
7101
7102/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103 * Make a copy of a Dictionary item.
7104 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007105 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007106dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007107 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007108{
Bram Moolenaar33570922005-01-25 22:26:29 +00007109 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007110
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007111 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7112 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007113 if (di != NULL)
7114 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007115 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007116 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007117 copy_tv(&org->di_tv, &di->di_tv);
7118 }
7119 return di;
7120}
7121
7122/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007123 * Remove item "item" from Dictionary "dict" and free it.
7124 */
7125 static void
7126dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 dict_T *dict;
7128 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007129{
Bram Moolenaar33570922005-01-25 22:26:29 +00007130 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131
Bram Moolenaar33570922005-01-25 22:26:29 +00007132 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007133 if (HASHITEM_EMPTY(hi))
7134 EMSG2(_(e_intern2), "dictitem_remove()");
7135 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007136 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007137 dictitem_free(item);
7138}
7139
7140/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007141 * Free a dict item. Also clears the value.
7142 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007143 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007144dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007145 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007146{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007147 clear_tv(&item->di_tv);
7148 vim_free(item);
7149}
7150
7151/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7153 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007154 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007155 * Returns NULL when out of memory.
7156 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007157 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007158dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007159 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007160 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007161 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007162{
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 dict_T *copy;
7164 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007165 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007166 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007167
7168 if (orig == NULL)
7169 return NULL;
7170
7171 copy = dict_alloc();
7172 if (copy != NULL)
7173 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007174 if (copyID != 0)
7175 {
7176 orig->dv_copyID = copyID;
7177 orig->dv_copydict = copy;
7178 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007179 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007180 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007181 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007183 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184 --todo;
7185
7186 di = dictitem_alloc(hi->hi_key);
7187 if (di == NULL)
7188 break;
7189 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007190 {
7191 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7192 copyID) == FAIL)
7193 {
7194 vim_free(di);
7195 break;
7196 }
7197 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007198 else
7199 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7200 if (dict_add(copy, di) == FAIL)
7201 {
7202 dictitem_free(di);
7203 break;
7204 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007205 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007207
Bram Moolenaare9a41262005-01-15 22:18:47 +00007208 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007209 if (todo > 0)
7210 {
7211 dict_unref(copy);
7212 copy = NULL;
7213 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007214 }
7215
7216 return copy;
7217}
7218
7219/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007221 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007223 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007225 dict_T *d;
7226 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227{
Bram Moolenaar33570922005-01-25 22:26:29 +00007228 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229}
7230
Bram Moolenaar8c711452005-01-14 21:53:12 +00007231/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007232 * Add a number or string entry to dictionary "d".
7233 * When "str" is NULL use number "nr", otherwise use "str".
7234 * Returns FAIL when out of memory and when key already exists.
7235 */
7236 int
7237dict_add_nr_str(d, key, nr, str)
7238 dict_T *d;
7239 char *key;
7240 long nr;
7241 char_u *str;
7242{
7243 dictitem_T *item;
7244
7245 item = dictitem_alloc((char_u *)key);
7246 if (item == NULL)
7247 return FAIL;
7248 item->di_tv.v_lock = 0;
7249 if (str == NULL)
7250 {
7251 item->di_tv.v_type = VAR_NUMBER;
7252 item->di_tv.vval.v_number = nr;
7253 }
7254 else
7255 {
7256 item->di_tv.v_type = VAR_STRING;
7257 item->di_tv.vval.v_string = vim_strsave(str);
7258 }
7259 if (dict_add(d, item) == FAIL)
7260 {
7261 dictitem_free(item);
7262 return FAIL;
7263 }
7264 return OK;
7265}
7266
7267/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007268 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007269 * Returns FAIL when out of memory and when key already exists.
7270 */
7271 int
7272dict_add_list(d, key, list)
7273 dict_T *d;
7274 char *key;
7275 list_T *list;
7276{
7277 dictitem_T *item;
7278
7279 item = dictitem_alloc((char_u *)key);
7280 if (item == NULL)
7281 return FAIL;
7282 item->di_tv.v_lock = 0;
7283 item->di_tv.v_type = VAR_LIST;
7284 item->di_tv.vval.v_list = list;
7285 if (dict_add(d, item) == FAIL)
7286 {
7287 dictitem_free(item);
7288 return FAIL;
7289 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007290 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007291 return OK;
7292}
7293
7294/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007295 * Get the number of items in a Dictionary.
7296 */
7297 static long
7298dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007299 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301 if (d == NULL)
7302 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007303 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304}
7305
7306/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007307 * Find item "key[len]" in Dictionary "d".
7308 * If "len" is negative use strlen(key).
7309 * Returns NULL when not found.
7310 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007311 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007312dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007313 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007314 char_u *key;
7315 int len;
7316{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317#define AKEYLEN 200
7318 char_u buf[AKEYLEN];
7319 char_u *akey;
7320 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007321 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007322
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 if (len < 0)
7324 akey = key;
7325 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007326 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 tofree = akey = vim_strnsave(key, len);
7328 if (akey == NULL)
7329 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007330 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007331 else
7332 {
7333 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007334 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007335 akey = buf;
7336 }
7337
Bram Moolenaar33570922005-01-25 22:26:29 +00007338 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007339 vim_free(tofree);
7340 if (HASHITEM_EMPTY(hi))
7341 return NULL;
7342 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343}
7344
7345/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007346 * Get a string item from a dictionary.
7347 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007348 * Returns NULL if the entry doesn't exist or out of memory.
7349 */
7350 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007351get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007352 dict_T *d;
7353 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007354 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007355{
7356 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007357 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007358
7359 di = dict_find(d, key, -1);
7360 if (di == NULL)
7361 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007362 s = get_tv_string(&di->di_tv);
7363 if (save && s != NULL)
7364 s = vim_strsave(s);
7365 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007366}
7367
7368/*
7369 * Get a number item from a dictionary.
7370 * Returns 0 if the entry doesn't exist or out of memory.
7371 */
7372 long
7373get_dict_number(d, key)
7374 dict_T *d;
7375 char_u *key;
7376{
7377 dictitem_T *di;
7378
7379 di = dict_find(d, key, -1);
7380 if (di == NULL)
7381 return 0;
7382 return get_tv_number(&di->di_tv);
7383}
7384
7385/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007386 * Return an allocated string with the string representation of a Dictionary.
7387 * May return NULL.
7388 */
7389 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007390dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007391 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007392 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007393{
7394 garray_T ga;
7395 int first = TRUE;
7396 char_u *tofree;
7397 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007398 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007399 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007400 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007403 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007404 return NULL;
7405 ga_init2(&ga, (int)sizeof(char), 80);
7406 ga_append(&ga, '{');
7407
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007408 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007409 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007411 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007413 --todo;
7414
7415 if (first)
7416 first = FALSE;
7417 else
7418 ga_concat(&ga, (char_u *)", ");
7419
7420 tofree = string_quote(hi->hi_key, FALSE);
7421 if (tofree != NULL)
7422 {
7423 ga_concat(&ga, tofree);
7424 vim_free(tofree);
7425 }
7426 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007427 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007428 if (s != NULL)
7429 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007431 if (s == NULL)
7432 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007433 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007434 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007435 if (todo > 0)
7436 {
7437 vim_free(ga.ga_data);
7438 return NULL;
7439 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007440
7441 ga_append(&ga, '}');
7442 ga_append(&ga, NUL);
7443 return (char_u *)ga.ga_data;
7444}
7445
7446/*
7447 * Allocate a variable for a Dictionary and fill it from "*arg".
7448 * Return OK or FAIL. Returns NOTDONE for {expr}.
7449 */
7450 static int
7451get_dict_tv(arg, rettv, evaluate)
7452 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007454 int evaluate;
7455{
Bram Moolenaar33570922005-01-25 22:26:29 +00007456 dict_T *d = NULL;
7457 typval_T tvkey;
7458 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007459 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007460 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007462 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463
7464 /*
7465 * First check if it's not a curly-braces thing: {expr}.
7466 * Must do this without evaluating, otherwise a function may be called
7467 * twice. Unfortunately this means we need to call eval1() twice for the
7468 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007469 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007471 if (*start != '}')
7472 {
7473 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7474 return FAIL;
7475 if (*start == '}')
7476 return NOTDONE;
7477 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007478
7479 if (evaluate)
7480 {
7481 d = dict_alloc();
7482 if (d == NULL)
7483 return FAIL;
7484 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007485 tvkey.v_type = VAR_UNKNOWN;
7486 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487
7488 *arg = skipwhite(*arg + 1);
7489 while (**arg != '}' && **arg != NUL)
7490 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007491 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492 goto failret;
7493 if (**arg != ':')
7494 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007495 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007496 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497 goto failret;
7498 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007499 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007501 key = get_tv_string_buf_chk(&tvkey, buf);
7502 if (key == NULL || *key == NUL)
7503 {
7504 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7505 if (key != NULL)
7506 EMSG(_(e_emptykey));
7507 clear_tv(&tvkey);
7508 goto failret;
7509 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007510 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007511
7512 *arg = skipwhite(*arg + 1);
7513 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7514 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007515 if (evaluate)
7516 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007517 goto failret;
7518 }
7519 if (evaluate)
7520 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007522 if (item != NULL)
7523 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007524 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007526 clear_tv(&tv);
7527 goto failret;
7528 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007529 item = dictitem_alloc(key);
7530 clear_tv(&tvkey);
7531 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007532 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007533 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007534 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535 if (dict_add(d, item) == FAIL)
7536 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007537 }
7538 }
7539
7540 if (**arg == '}')
7541 break;
7542 if (**arg != ',')
7543 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007544 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007545 goto failret;
7546 }
7547 *arg = skipwhite(*arg + 1);
7548 }
7549
7550 if (**arg != '}')
7551 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007552 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007553failret:
7554 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007555 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556 return FAIL;
7557 }
7558
7559 *arg = skipwhite(*arg + 1);
7560 if (evaluate)
7561 {
7562 rettv->v_type = VAR_DICT;
7563 rettv->vval.v_dict = d;
7564 ++d->dv_refcount;
7565 }
7566
7567 return OK;
7568}
7569
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007571 * Return a string with the string representation of a variable.
7572 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007573 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007574 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007575 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007576 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007577 */
7578 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007579echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007580 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007581 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007582 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007583 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007584{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007585 static int recurse = 0;
7586 char_u *r = NULL;
7587
Bram Moolenaar33570922005-01-25 22:26:29 +00007588 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007590 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007591 *tofree = NULL;
7592 return NULL;
7593 }
7594 ++recurse;
7595
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007596 switch (tv->v_type)
7597 {
7598 case VAR_FUNC:
7599 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007600 r = tv->vval.v_string;
7601 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007602
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007603 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007604 if (tv->vval.v_list == NULL)
7605 {
7606 *tofree = NULL;
7607 r = NULL;
7608 }
7609 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7610 {
7611 *tofree = NULL;
7612 r = (char_u *)"[...]";
7613 }
7614 else
7615 {
7616 tv->vval.v_list->lv_copyID = copyID;
7617 *tofree = list2string(tv, copyID);
7618 r = *tofree;
7619 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007620 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007621
Bram Moolenaar8c711452005-01-14 21:53:12 +00007622 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007623 if (tv->vval.v_dict == NULL)
7624 {
7625 *tofree = NULL;
7626 r = NULL;
7627 }
7628 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7629 {
7630 *tofree = NULL;
7631 r = (char_u *)"{...}";
7632 }
7633 else
7634 {
7635 tv->vval.v_dict->dv_copyID = copyID;
7636 *tofree = dict2string(tv, copyID);
7637 r = *tofree;
7638 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007640
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007641 case VAR_STRING:
7642 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007643 *tofree = NULL;
7644 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007645 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007646
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007647#ifdef FEAT_FLOAT
7648 case VAR_FLOAT:
7649 *tofree = NULL;
7650 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7651 r = numbuf;
7652 break;
7653#endif
7654
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007655 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007656 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007658 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007659
7660 --recurse;
7661 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007662}
7663
7664/*
7665 * Return a string with the string representation of a variable.
7666 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7667 * "numbuf" is used for a number.
7668 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007669 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007670 */
7671 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007672tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007673 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674 char_u **tofree;
7675 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007676 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007677{
7678 switch (tv->v_type)
7679 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007680 case VAR_FUNC:
7681 *tofree = string_quote(tv->vval.v_string, TRUE);
7682 return *tofree;
7683 case VAR_STRING:
7684 *tofree = string_quote(tv->vval.v_string, FALSE);
7685 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007686#ifdef FEAT_FLOAT
7687 case VAR_FLOAT:
7688 *tofree = NULL;
7689 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7690 return numbuf;
7691#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007692 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007693 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007694 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007695 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007696 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007697 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007698 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007699 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007700}
7701
7702/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 * Return string "str" in ' quotes, doubling ' characters.
7704 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007705 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007706 */
7707 static char_u *
7708string_quote(str, function)
7709 char_u *str;
7710 int function;
7711{
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007713 char_u *p, *r, *s;
7714
Bram Moolenaar33570922005-01-25 22:26:29 +00007715 len = (function ? 13 : 3);
7716 if (str != NULL)
7717 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007718 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007719 for (p = str; *p != NUL; mb_ptr_adv(p))
7720 if (*p == '\'')
7721 ++len;
7722 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007723 s = r = alloc(len);
7724 if (r != NULL)
7725 {
7726 if (function)
7727 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007728 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007729 r += 10;
7730 }
7731 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007733 if (str != NULL)
7734 for (p = str; *p != NUL; )
7735 {
7736 if (*p == '\'')
7737 *r++ = '\'';
7738 MB_COPY_CHAR(p, r);
7739 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007741 if (function)
7742 *r++ = ')';
7743 *r++ = NUL;
7744 }
7745 return s;
7746}
7747
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007748#ifdef FEAT_FLOAT
7749/*
7750 * Convert the string "text" to a floating point number.
7751 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7752 * this always uses a decimal point.
7753 * Returns the length of the text that was consumed.
7754 */
7755 static int
7756string2float(text, value)
7757 char_u *text;
7758 float_T *value; /* result stored here */
7759{
7760 char *s = (char *)text;
7761 float_T f;
7762
7763 f = strtod(s, &s);
7764 *value = f;
7765 return (int)((char_u *)s - text);
7766}
7767#endif
7768
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007769/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770 * Get the value of an environment variable.
7771 * "arg" is pointing to the '$'. It is advanced to after the name.
7772 * If the environment variable was not set, silently assume it is empty.
7773 * Always return OK.
7774 */
7775 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007776get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 int evaluate;
7780{
7781 char_u *string = NULL;
7782 int len;
7783 int cc;
7784 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007785 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786
7787 ++*arg;
7788 name = *arg;
7789 len = get_env_len(arg);
7790 if (evaluate)
7791 {
7792 if (len != 0)
7793 {
7794 cc = name[len];
7795 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007796 /* first try vim_getenv(), fast for normal environment vars */
7797 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007799 {
7800 if (!mustfree)
7801 string = vim_strsave(string);
7802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 else
7804 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007805 if (mustfree)
7806 vim_free(string);
7807
Bram Moolenaar071d4272004-06-13 20:20:40 +00007808 /* next try expanding things like $VIM and ${HOME} */
7809 string = expand_env_save(name - 1);
7810 if (string != NULL && *string == '$')
7811 {
7812 vim_free(string);
7813 string = NULL;
7814 }
7815 }
7816 name[len] = cc;
7817 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007818 rettv->v_type = VAR_STRING;
7819 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 }
7821
7822 return OK;
7823}
7824
7825/*
7826 * Array with names and number of arguments of all internal functions
7827 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7828 */
7829static struct fst
7830{
7831 char *f_name; /* function name */
7832 char f_min_argc; /* minimal number of arguments */
7833 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007834 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007835 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007836} functions[] =
7837{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838#ifdef FEAT_FLOAT
7839 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007840 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007841#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007842 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007843 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 {"append", 2, 2, f_append},
7845 {"argc", 0, 0, f_argc},
7846 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007847 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007848#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007849 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007850 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007851 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007852#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007854 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007855 {"bufexists", 1, 1, f_bufexists},
7856 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7857 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7858 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7859 {"buflisted", 1, 1, f_buflisted},
7860 {"bufloaded", 1, 1, f_bufloaded},
7861 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007862 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 {"bufwinnr", 1, 1, f_bufwinnr},
7864 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007865 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01007866 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007867 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007868#ifdef FEAT_FLOAT
7869 {"ceil", 1, 1, f_ceil},
7870#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007871 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007872 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007874 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007876#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007877 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007878 {"complete_add", 1, 1, f_complete_add},
7879 {"complete_check", 0, 0, f_complete_check},
7880#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007882 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883#ifdef FEAT_FLOAT
7884 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007885 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007886#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007887 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007889 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007890 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007891 {"delete", 1, 1, f_delete},
7892 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007893 {"diff_filler", 1, 1, f_diff_filler},
7894 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007895 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 {"eventhandler", 0, 0, f_eventhandler},
7899 {"executable", 1, 1, f_executable},
7900 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007901#ifdef FEAT_FLOAT
7902 {"exp", 1, 1, f_exp},
7903#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007904 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007905 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007906 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7908 {"filereadable", 1, 1, f_filereadable},
7909 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007910 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007911 {"finddir", 1, 3, f_finddir},
7912 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007913#ifdef FEAT_FLOAT
7914 {"float2nr", 1, 1, f_float2nr},
7915 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007916 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007917#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007918 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007919 {"fnamemodify", 2, 2, f_fnamemodify},
7920 {"foldclosed", 1, 1, f_foldclosed},
7921 {"foldclosedend", 1, 1, f_foldclosedend},
7922 {"foldlevel", 1, 1, f_foldlevel},
7923 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007924 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007925 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007926 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007927 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007928 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007929 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007930 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007931 {"getchar", 0, 1, f_getchar},
7932 {"getcharmod", 0, 0, f_getcharmod},
7933 {"getcmdline", 0, 0, f_getcmdline},
7934 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007935 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007937 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007938 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007939 {"getfsize", 1, 1, f_getfsize},
7940 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007941 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007942 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007943 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007944 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007945 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007946 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007947 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007948 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007950 {"gettabvar", 2, 3, f_gettabvar},
7951 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 {"getwinposx", 0, 0, f_getwinposx},
7953 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007954 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007955 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007956 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007958 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007959 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007960 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7962 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7963 {"histadd", 2, 2, f_histadd},
7964 {"histdel", 1, 2, f_histdel},
7965 {"histget", 1, 2, f_histget},
7966 {"histnr", 1, 1, f_histnr},
7967 {"hlID", 1, 1, f_hlID},
7968 {"hlexists", 1, 1, f_hlexists},
7969 {"hostname", 0, 0, f_hostname},
7970 {"iconv", 3, 3, f_iconv},
7971 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007972 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007973 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007974 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007975 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976 {"inputrestore", 0, 0, f_inputrestore},
7977 {"inputsave", 0, 0, f_inputsave},
7978 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007979 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007980 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007982 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007983 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007984 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007985 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007987 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988 {"libcall", 3, 3, f_libcall},
7989 {"libcallnr", 3, 3, f_libcallnr},
7990 {"line", 1, 1, f_line},
7991 {"line2byte", 1, 1, f_line2byte},
7992 {"lispindent", 1, 1, f_lispindent},
7993 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007994#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007995 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007996 {"log10", 1, 1, f_log10},
7997#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007998#ifdef FEAT_LUA
7999 {"luaeval", 1, 2, f_luaeval},
8000#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008001 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008002 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008003 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008004 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008005 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008006 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008007 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008008 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008009 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008010 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008011 {"max", 1, 1, f_max},
8012 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008013#ifdef vim_mkdir
8014 {"mkdir", 1, 3, f_mkdir},
8015#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008016 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008017#ifdef FEAT_MZSCHEME
8018 {"mzeval", 1, 1, f_mzeval},
8019#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008021 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008022 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008023 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008024#ifdef FEAT_FLOAT
8025 {"pow", 2, 2, f_pow},
8026#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008028 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008029 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008030#ifdef FEAT_PYTHON3
8031 {"py3eval", 1, 1, f_py3eval},
8032#endif
8033#ifdef FEAT_PYTHON
8034 {"pyeval", 1, 1, f_pyeval},
8035#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008036 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008037 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008038 {"reltime", 0, 2, f_reltime},
8039 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 {"remote_expr", 2, 3, f_remote_expr},
8041 {"remote_foreground", 1, 1, f_remote_foreground},
8042 {"remote_peek", 1, 2, f_remote_peek},
8043 {"remote_read", 1, 1, f_remote_read},
8044 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008045 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008047 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008049 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008050#ifdef FEAT_FLOAT
8051 {"round", 1, 1, f_round},
8052#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008053 {"screenattr", 2, 2, f_screenattr},
8054 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008055 {"screencol", 0, 0, f_screencol},
8056 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008057 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008058 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008059 {"searchpair", 3, 7, f_searchpair},
8060 {"searchpairpos", 3, 7, f_searchpairpos},
8061 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 {"server2client", 2, 2, f_server2client},
8063 {"serverlist", 0, 0, f_serverlist},
8064 {"setbufvar", 3, 3, f_setbufvar},
8065 {"setcmdpos", 1, 1, f_setcmdpos},
8066 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008067 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008068 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008069 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008070 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008072 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008073 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008075#ifdef FEAT_CRYPT
8076 {"sha256", 1, 1, f_sha256},
8077#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008078 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008079 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008080 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008081#ifdef FEAT_FLOAT
8082 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008083 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008084#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008085 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008086 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008087 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008088 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008089 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008090#ifdef FEAT_FLOAT
8091 {"sqrt", 1, 1, f_sqrt},
8092 {"str2float", 1, 1, f_str2float},
8093#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008094 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008095 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008096 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097#ifdef HAVE_STRFTIME
8098 {"strftime", 1, 2, f_strftime},
8099#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008100 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008101 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {"strlen", 1, 1, f_strlen},
8103 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008104 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008106 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 {"submatch", 1, 1, f_submatch},
8108 {"substitute", 4, 4, f_substitute},
8109 {"synID", 3, 3, f_synID},
8110 {"synIDattr", 2, 3, f_synIDattr},
8111 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008112 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008113 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008114 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008115 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008116 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008117 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008118 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008119 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008120#ifdef FEAT_FLOAT
8121 {"tan", 1, 1, f_tan},
8122 {"tanh", 1, 1, f_tanh},
8123#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008125 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 {"tolower", 1, 1, f_tolower},
8127 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008128 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008129#ifdef FEAT_FLOAT
8130 {"trunc", 1, 1, f_trunc},
8131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008133 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008134 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008135 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"virtcol", 1, 1, f_virtcol},
8137 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008138 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 {"winbufnr", 1, 1, f_winbufnr},
8140 {"wincol", 0, 0, f_wincol},
8141 {"winheight", 1, 1, f_winheight},
8142 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008143 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008145 {"winrestview", 1, 1, f_winrestview},
8146 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008148 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008149 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150};
8151
8152#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8153
8154/*
8155 * Function given to ExpandGeneric() to obtain the list of internal
8156 * or user defined function names.
8157 */
8158 char_u *
8159get_function_name(xp, idx)
8160 expand_T *xp;
8161 int idx;
8162{
8163 static int intidx = -1;
8164 char_u *name;
8165
8166 if (idx == 0)
8167 intidx = -1;
8168 if (intidx < 0)
8169 {
8170 name = get_user_func_name(xp, idx);
8171 if (name != NULL)
8172 return name;
8173 }
8174 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8175 {
8176 STRCPY(IObuff, functions[intidx].f_name);
8177 STRCAT(IObuff, "(");
8178 if (functions[intidx].f_max_argc == 0)
8179 STRCAT(IObuff, ")");
8180 return IObuff;
8181 }
8182
8183 return NULL;
8184}
8185
8186/*
8187 * Function given to ExpandGeneric() to obtain the list of internal or
8188 * user defined variable or function names.
8189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 char_u *
8191get_expr_name(xp, idx)
8192 expand_T *xp;
8193 int idx;
8194{
8195 static int intidx = -1;
8196 char_u *name;
8197
8198 if (idx == 0)
8199 intidx = -1;
8200 if (intidx < 0)
8201 {
8202 name = get_function_name(xp, idx);
8203 if (name != NULL)
8204 return name;
8205 }
8206 return get_user_var_name(xp, ++intidx);
8207}
8208
8209#endif /* FEAT_CMDL_COMPL */
8210
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008211#if defined(EBCDIC) || defined(PROTO)
8212/*
8213 * Compare struct fst by function name.
8214 */
8215 static int
8216compare_func_name(s1, s2)
8217 const void *s1;
8218 const void *s2;
8219{
8220 struct fst *p1 = (struct fst *)s1;
8221 struct fst *p2 = (struct fst *)s2;
8222
8223 return STRCMP(p1->f_name, p2->f_name);
8224}
8225
8226/*
8227 * Sort the function table by function name.
8228 * The sorting of the table above is ASCII dependant.
8229 * On machines using EBCDIC we have to sort it.
8230 */
8231 static void
8232sortFunctions()
8233{
8234 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8235
8236 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8237}
8238#endif
8239
8240
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241/*
8242 * Find internal function in table above.
8243 * Return index, or -1 if not found
8244 */
8245 static int
8246find_internal_func(name)
8247 char_u *name; /* name of the function */
8248{
8249 int first = 0;
8250 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8251 int cmp;
8252 int x;
8253
8254 /*
8255 * Find the function name in the table. Binary search.
8256 */
8257 while (first <= last)
8258 {
8259 x = first + ((unsigned)(last - first) >> 1);
8260 cmp = STRCMP(name, functions[x].f_name);
8261 if (cmp < 0)
8262 last = x - 1;
8263 else if (cmp > 0)
8264 first = x + 1;
8265 else
8266 return x;
8267 }
8268 return -1;
8269}
8270
8271/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008272 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8273 * name it contains, otherwise return "name".
8274 */
8275 static char_u *
8276deref_func_name(name, lenp)
8277 char_u *name;
8278 int *lenp;
8279{
Bram Moolenaar33570922005-01-25 22:26:29 +00008280 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008281 int cc;
8282
8283 cc = name[*lenp];
8284 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008285 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008286 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008287 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008288 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008289 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008290 {
8291 *lenp = 0;
8292 return (char_u *)""; /* just in case */
8293 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008294 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008295 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008296 }
8297
8298 return name;
8299}
8300
8301/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 * Allocate a variable for the result of a function.
8303 * Return OK or FAIL.
8304 */
8305 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008306get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8307 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 char_u *name; /* name of the function */
8309 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008310 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 char_u **arg; /* argument, pointing to the '(' */
8312 linenr_T firstline; /* first line of range */
8313 linenr_T lastline; /* last line of range */
8314 int *doesrange; /* return: function handled range */
8315 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008316 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317{
8318 char_u *argp;
8319 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008320 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 int argcount = 0; /* number of arguments found */
8322
8323 /*
8324 * Get the arguments.
8325 */
8326 argp = *arg;
8327 while (argcount < MAX_FUNC_ARGS)
8328 {
8329 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8330 if (*argp == ')' || *argp == ',' || *argp == NUL)
8331 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8333 {
8334 ret = FAIL;
8335 break;
8336 }
8337 ++argcount;
8338 if (*argp != ',')
8339 break;
8340 }
8341 if (*argp == ')')
8342 ++argp;
8343 else
8344 ret = FAIL;
8345
8346 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008347 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008348 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008349 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008350 {
8351 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008352 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008353 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008354 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356
8357 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008358 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359
8360 *arg = skipwhite(argp);
8361 return ret;
8362}
8363
8364
8365/*
8366 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008367 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008368 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008369 */
8370 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008371call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008372 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008373 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008375 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008377 typval_T *argvars; /* vars for arguments, must have "argcount"
8378 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379 linenr_T firstline; /* first line of range */
8380 linenr_T lastline; /* last line of range */
8381 int *doesrange; /* return: function handled range */
8382 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008383 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384{
8385 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386#define ERROR_UNKNOWN 0
8387#define ERROR_TOOMANY 1
8388#define ERROR_TOOFEW 2
8389#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008390#define ERROR_DICT 4
8391#define ERROR_NONE 5
8392#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 int error = ERROR_NONE;
8394 int i;
8395 int llen;
8396 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397#define FLEN_FIXED 40
8398 char_u fname_buf[FLEN_FIXED + 1];
8399 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008400 char_u *name;
8401
8402 /* Make a copy of the name, if it comes from a funcref variable it could
8403 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008404 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008405 if (name == NULL)
8406 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407
8408 /*
8409 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8410 * Change <SNR>123_name() to K_SNR 123_name().
8411 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8412 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 llen = eval_fname_script(name);
8414 if (llen > 0)
8415 {
8416 fname_buf[0] = K_SPECIAL;
8417 fname_buf[1] = KS_EXTRA;
8418 fname_buf[2] = (int)KE_SNR;
8419 i = 3;
8420 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8421 {
8422 if (current_SID <= 0)
8423 error = ERROR_SCRIPT;
8424 else
8425 {
8426 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8427 i = (int)STRLEN(fname_buf);
8428 }
8429 }
8430 if (i + STRLEN(name + llen) < FLEN_FIXED)
8431 {
8432 STRCPY(fname_buf + i, name + llen);
8433 fname = fname_buf;
8434 }
8435 else
8436 {
8437 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8438 if (fname == NULL)
8439 error = ERROR_OTHER;
8440 else
8441 {
8442 mch_memmove(fname, fname_buf, (size_t)i);
8443 STRCPY(fname + i, name + llen);
8444 }
8445 }
8446 }
8447 else
8448 fname = name;
8449
8450 *doesrange = FALSE;
8451
8452
8453 /* execute the function if no errors detected and executing */
8454 if (evaluate && error == ERROR_NONE)
8455 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008456 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8457 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 error = ERROR_UNKNOWN;
8459
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008460 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {
8462 /*
8463 * User defined function.
8464 */
8465 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008466
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008468 /* Trigger FuncUndefined event, may load the function. */
8469 if (fp == NULL
8470 && apply_autocmds(EVENT_FUNCUNDEFINED,
8471 fname, fname, TRUE, NULL)
8472 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008474 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475 fp = find_func(fname);
8476 }
8477#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008478 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008479 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008480 {
8481 /* loaded a package, search for the function again */
8482 fp = find_func(fname);
8483 }
8484
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 if (fp != NULL)
8486 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008487 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008489 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008491 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008493 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008494 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 else
8496 {
8497 /*
8498 * Call the user function.
8499 * Save and restore search patterns, script variables and
8500 * redo buffer.
8501 */
8502 save_search_patterns();
8503 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008504 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008505 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008506 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008507 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8508 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8509 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008510 /* Function was unreferenced while being used, free it
8511 * now. */
8512 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 restoreRedobuff();
8514 restore_search_patterns();
8515 error = ERROR_NONE;
8516 }
8517 }
8518 }
8519 else
8520 {
8521 /*
8522 * Find the function name in the table, call its implementation.
8523 */
8524 i = find_internal_func(fname);
8525 if (i >= 0)
8526 {
8527 if (argcount < functions[i].f_min_argc)
8528 error = ERROR_TOOFEW;
8529 else if (argcount > functions[i].f_max_argc)
8530 error = ERROR_TOOMANY;
8531 else
8532 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008533 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008534 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 error = ERROR_NONE;
8536 }
8537 }
8538 }
8539 /*
8540 * The function call (or "FuncUndefined" autocommand sequence) might
8541 * have been aborted by an error, an interrupt, or an explicitly thrown
8542 * exception that has not been caught so far. This situation can be
8543 * tested for by calling aborting(). For an error in an internal
8544 * function or for the "E132" error in call_user_func(), however, the
8545 * throw point at which the "force_abort" flag (temporarily reset by
8546 * emsg()) is normally updated has not been reached yet. We need to
8547 * update that flag first to make aborting() reliable.
8548 */
8549 update_force_abort();
8550 }
8551 if (error == ERROR_NONE)
8552 ret = OK;
8553
8554 /*
8555 * Report an error unless the argument evaluation or function call has been
8556 * cancelled due to an aborting error, an interrupt, or an exception.
8557 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008558 if (!aborting())
8559 {
8560 switch (error)
8561 {
8562 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008563 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008564 break;
8565 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008566 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008567 break;
8568 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008569 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008570 name);
8571 break;
8572 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008573 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008574 name);
8575 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008576 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008577 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008578 name);
8579 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008580 }
8581 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583 if (fname != name && fname != fname_buf)
8584 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008585 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586
8587 return ret;
8588}
8589
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008590/*
8591 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008592 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008593 */
8594 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008595emsg_funcname(ermsg, name)
8596 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008597 char_u *name;
8598{
8599 char_u *p;
8600
8601 if (*name == K_SPECIAL)
8602 p = concat_str((char_u *)"<SNR>", name + 3);
8603 else
8604 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008605 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008606 if (p != name)
8607 vim_free(p);
8608}
8609
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008610/*
8611 * Return TRUE for a non-zero Number and a non-empty String.
8612 */
8613 static int
8614non_zero_arg(argvars)
8615 typval_T *argvars;
8616{
8617 return ((argvars[0].v_type == VAR_NUMBER
8618 && argvars[0].vval.v_number != 0)
8619 || (argvars[0].v_type == VAR_STRING
8620 && argvars[0].vval.v_string != NULL
8621 && *argvars[0].vval.v_string != NUL));
8622}
8623
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624/*********************************************
8625 * Implementation of the built-in functions
8626 */
8627
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008628#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008629static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8630
8631/*
8632 * Get the float value of "argvars[0]" into "f".
8633 * Returns FAIL when the argument is not a Number or Float.
8634 */
8635 static int
8636get_float_arg(argvars, f)
8637 typval_T *argvars;
8638 float_T *f;
8639{
8640 if (argvars[0].v_type == VAR_FLOAT)
8641 {
8642 *f = argvars[0].vval.v_float;
8643 return OK;
8644 }
8645 if (argvars[0].v_type == VAR_NUMBER)
8646 {
8647 *f = (float_T)argvars[0].vval.v_number;
8648 return OK;
8649 }
8650 EMSG(_("E808: Number or Float required"));
8651 return FAIL;
8652}
8653
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008654/*
8655 * "abs(expr)" function
8656 */
8657 static void
8658f_abs(argvars, rettv)
8659 typval_T *argvars;
8660 typval_T *rettv;
8661{
8662 if (argvars[0].v_type == VAR_FLOAT)
8663 {
8664 rettv->v_type = VAR_FLOAT;
8665 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8666 }
8667 else
8668 {
8669 varnumber_T n;
8670 int error = FALSE;
8671
8672 n = get_tv_number_chk(&argvars[0], &error);
8673 if (error)
8674 rettv->vval.v_number = -1;
8675 else if (n > 0)
8676 rettv->vval.v_number = n;
8677 else
8678 rettv->vval.v_number = -n;
8679 }
8680}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008681
8682/*
8683 * "acos()" function
8684 */
8685 static void
8686f_acos(argvars, rettv)
8687 typval_T *argvars;
8688 typval_T *rettv;
8689{
8690 float_T f;
8691
8692 rettv->v_type = VAR_FLOAT;
8693 if (get_float_arg(argvars, &f) == OK)
8694 rettv->vval.v_float = acos(f);
8695 else
8696 rettv->vval.v_float = 0.0;
8697}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008698#endif
8699
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008701 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 */
8703 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008704f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008705 typval_T *argvars;
8706 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707{
Bram Moolenaar33570922005-01-25 22:26:29 +00008708 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008710 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008711 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008712 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008713 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008714 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008715 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008716 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008717 }
8718 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008719 EMSG(_(e_listreq));
8720}
8721
8722/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008723 * "and(expr, expr)" function
8724 */
8725 static void
8726f_and(argvars, rettv)
8727 typval_T *argvars;
8728 typval_T *rettv;
8729{
8730 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8731 & get_tv_number_chk(&argvars[1], NULL);
8732}
8733
8734/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008735 * "append(lnum, string/list)" function
8736 */
8737 static void
8738f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008739 typval_T *argvars;
8740 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008741{
8742 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008743 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008744 list_T *l = NULL;
8745 listitem_T *li = NULL;
8746 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008747 long added = 0;
8748
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008749 /* When coming here from Insert mode, sync undo, so that this can be
8750 * undone separately from what was previously inserted. */
8751 if (u_sync_once == 2)
8752 {
8753 u_sync_once = 1; /* notify that u_sync() was called */
8754 u_sync(TRUE);
8755 }
8756
Bram Moolenaar0d660222005-01-07 21:51:51 +00008757 lnum = get_tv_lnum(argvars);
8758 if (lnum >= 0
8759 && lnum <= curbuf->b_ml.ml_line_count
8760 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008761 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008762 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008763 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008764 l = argvars[1].vval.v_list;
8765 if (l == NULL)
8766 return;
8767 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008768 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008769 for (;;)
8770 {
8771 if (l == NULL)
8772 tv = &argvars[1]; /* append a string */
8773 else if (li == NULL)
8774 break; /* end of list */
8775 else
8776 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008777 line = get_tv_string_chk(tv);
8778 if (line == NULL) /* type error */
8779 {
8780 rettv->vval.v_number = 1; /* Failed */
8781 break;
8782 }
8783 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008784 ++added;
8785 if (l == NULL)
8786 break;
8787 li = li->li_next;
8788 }
8789
8790 appended_lines_mark(lnum, added);
8791 if (curwin->w_cursor.lnum > lnum)
8792 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008794 else
8795 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008796}
8797
8798/*
8799 * "argc()" function
8800 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008803 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008804 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008806 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807}
8808
8809/*
8810 * "argidx()" function
8811 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008813f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008814 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008815 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008817 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818}
8819
8820/*
8821 * "argv(nr)" function
8822 */
8823 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008824f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008825 typval_T *argvars;
8826 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827{
8828 int idx;
8829
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008830 if (argvars[0].v_type != VAR_UNKNOWN)
8831 {
8832 idx = get_tv_number_chk(&argvars[0], NULL);
8833 if (idx >= 0 && idx < ARGCOUNT)
8834 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8835 else
8836 rettv->vval.v_string = NULL;
8837 rettv->v_type = VAR_STRING;
8838 }
8839 else if (rettv_list_alloc(rettv) == OK)
8840 for (idx = 0; idx < ARGCOUNT; ++idx)
8841 list_append_string(rettv->vval.v_list,
8842 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843}
8844
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008845#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008846/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008847 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008848 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008849 static void
8850f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008851 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008852 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008853{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008854 float_T f;
8855
8856 rettv->v_type = VAR_FLOAT;
8857 if (get_float_arg(argvars, &f) == OK)
8858 rettv->vval.v_float = asin(f);
8859 else
8860 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008861}
8862
8863/*
8864 * "atan()" function
8865 */
8866 static void
8867f_atan(argvars, rettv)
8868 typval_T *argvars;
8869 typval_T *rettv;
8870{
8871 float_T f;
8872
8873 rettv->v_type = VAR_FLOAT;
8874 if (get_float_arg(argvars, &f) == OK)
8875 rettv->vval.v_float = atan(f);
8876 else
8877 rettv->vval.v_float = 0.0;
8878}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008879
8880/*
8881 * "atan2()" function
8882 */
8883 static void
8884f_atan2(argvars, rettv)
8885 typval_T *argvars;
8886 typval_T *rettv;
8887{
8888 float_T fx, fy;
8889
8890 rettv->v_type = VAR_FLOAT;
8891 if (get_float_arg(argvars, &fx) == OK
8892 && get_float_arg(&argvars[1], &fy) == OK)
8893 rettv->vval.v_float = atan2(fx, fy);
8894 else
8895 rettv->vval.v_float = 0.0;
8896}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008897#endif
8898
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899/*
8900 * "browse(save, title, initdir, default)" function
8901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008903f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008904 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906{
8907#ifdef FEAT_BROWSE
8908 int save;
8909 char_u *title;
8910 char_u *initdir;
8911 char_u *defname;
8912 char_u buf[NUMBUFLEN];
8913 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008914 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008916 save = get_tv_number_chk(&argvars[0], &error);
8917 title = get_tv_string_chk(&argvars[1]);
8918 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8919 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008921 if (error || title == NULL || initdir == NULL || defname == NULL)
8922 rettv->vval.v_string = NULL;
8923 else
8924 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008925 do_browse(save ? BROWSE_SAVE : 0,
8926 title, defname, NULL, initdir, NULL, curbuf);
8927#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008928 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008929#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008930 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008931}
8932
8933/*
8934 * "browsedir(title, initdir)" function
8935 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008936 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008937f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008938 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008939 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008940{
8941#ifdef FEAT_BROWSE
8942 char_u *title;
8943 char_u *initdir;
8944 char_u buf[NUMBUFLEN];
8945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008946 title = get_tv_string_chk(&argvars[0]);
8947 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008949 if (title == NULL || initdir == NULL)
8950 rettv->vval.v_string = NULL;
8951 else
8952 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008953 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008955 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008956#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008957 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958}
8959
Bram Moolenaar33570922005-01-25 22:26:29 +00008960static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008961
Bram Moolenaar071d4272004-06-13 20:20:40 +00008962/*
8963 * Find a buffer by number or exact name.
8964 */
8965 static buf_T *
8966find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008967 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968{
8969 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008971 if (avar->v_type == VAR_NUMBER)
8972 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008973 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008974 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008975 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008976 if (buf == NULL)
8977 {
8978 /* No full path name match, try a match with a URL or a "nofile"
8979 * buffer, these don't use the full path. */
8980 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8981 if (buf->b_fname != NULL
8982 && (path_with_url(buf->b_fname)
8983#ifdef FEAT_QUICKFIX
8984 || bt_nofile(buf)
8985#endif
8986 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008987 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008988 break;
8989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008990 }
8991 return buf;
8992}
8993
8994/*
8995 * "bufexists(expr)" function
8996 */
8997 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008999 typval_T *argvars;
9000 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009002 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003}
9004
9005/*
9006 * "buflisted(expr)" function
9007 */
9008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009009f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009010 typval_T *argvars;
9011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012{
9013 buf_T *buf;
9014
9015 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009016 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009017}
9018
9019/*
9020 * "bufloaded(expr)" function
9021 */
9022 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009023f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009024 typval_T *argvars;
9025 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009026{
9027 buf_T *buf;
9028
9029 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009030 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031}
9032
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009033static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009034
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035/*
9036 * Get buffer by number or pattern.
9037 */
9038 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009039get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009040 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009041 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009042{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009043 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009044 int save_magic;
9045 char_u *save_cpo;
9046 buf_T *buf;
9047
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009048 if (tv->v_type == VAR_NUMBER)
9049 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009050 if (tv->v_type != VAR_STRING)
9051 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 if (name == NULL || *name == NUL)
9053 return curbuf;
9054 if (name[0] == '$' && name[1] == NUL)
9055 return lastbuf;
9056
9057 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9058 save_magic = p_magic;
9059 p_magic = TRUE;
9060 save_cpo = p_cpo;
9061 p_cpo = (char_u *)"";
9062
9063 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009064 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065
9066 p_magic = save_magic;
9067 p_cpo = save_cpo;
9068
9069 /* If not found, try expanding the name, like done for bufexists(). */
9070 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009071 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072
9073 return buf;
9074}
9075
9076/*
9077 * "bufname(expr)" function
9078 */
9079 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009080f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009081 typval_T *argvars;
9082 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083{
9084 buf_T *buf;
9085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009086 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009088 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009091 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009093 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 --emsg_off;
9095}
9096
9097/*
9098 * "bufnr(expr)" function
9099 */
9100 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009101f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009102 typval_T *argvars;
9103 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104{
9105 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009106 int error = FALSE;
9107 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009109 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009111 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009112 --emsg_off;
9113
9114 /* If the buffer isn't found and the second argument is not zero create a
9115 * new buffer. */
9116 if (buf == NULL
9117 && argvars[1].v_type != VAR_UNKNOWN
9118 && get_tv_number_chk(&argvars[1], &error) != 0
9119 && !error
9120 && (name = get_tv_string_chk(&argvars[0])) != NULL
9121 && !error)
9122 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9123
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009125 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009127 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128}
9129
9130/*
9131 * "bufwinnr(nr)" function
9132 */
9133 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009134f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009135 typval_T *argvars;
9136 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137{
9138#ifdef FEAT_WINDOWS
9139 win_T *wp;
9140 int winnr = 0;
9141#endif
9142 buf_T *buf;
9143
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009144 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009146 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147#ifdef FEAT_WINDOWS
9148 for (wp = firstwin; wp; wp = wp->w_next)
9149 {
9150 ++winnr;
9151 if (wp->w_buffer == buf)
9152 break;
9153 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157#endif
9158 --emsg_off;
9159}
9160
9161/*
9162 * "byte2line(byte)" function
9163 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009165f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009166 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009167 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168{
9169#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009170 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171#else
9172 long boff = 0;
9173
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009174 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009176 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009178 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179 (linenr_T)0, &boff);
9180#endif
9181}
9182
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009183 static void
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009184byteidx(argvars, rettv, comp)
Bram Moolenaar33570922005-01-25 22:26:29 +00009185 typval_T *argvars;
9186 typval_T *rettv;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009187 int comp;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009188{
9189#ifdef FEAT_MBYTE
9190 char_u *t;
9191#endif
9192 char_u *str;
9193 long idx;
9194
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009195 str = get_tv_string_chk(&argvars[0]);
9196 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009197 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009198 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009199 return;
9200
9201#ifdef FEAT_MBYTE
9202 t = str;
9203 for ( ; idx > 0; idx--)
9204 {
9205 if (*t == NUL) /* EOL reached */
9206 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009207 if (enc_utf8 && comp)
9208 t += utf_ptr2len(t);
9209 else
9210 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009211 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009212 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009213#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009214 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009215 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009216#endif
9217}
9218
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009219/*
9220 * "byteidx()" function
9221 */
9222 static void
9223f_byteidx(argvars, rettv)
9224 typval_T *argvars;
9225 typval_T *rettv;
9226{
9227 byteidx(argvars, rettv, FALSE);
9228}
9229
9230/*
9231 * "byteidxcomp()" function
9232 */
9233 static void
9234f_byteidxcomp(argvars, rettv)
9235 typval_T *argvars;
9236 typval_T *rettv;
9237{
9238 byteidx(argvars, rettv, TRUE);
9239}
9240
Bram Moolenaardb913952012-06-29 12:54:53 +02009241 int
9242func_call(name, args, selfdict, rettv)
9243 char_u *name;
9244 typval_T *args;
9245 dict_T *selfdict;
9246 typval_T *rettv;
9247{
9248 listitem_T *item;
9249 typval_T argv[MAX_FUNC_ARGS + 1];
9250 int argc = 0;
9251 int dummy;
9252 int r = 0;
9253
9254 for (item = args->vval.v_list->lv_first; item != NULL;
9255 item = item->li_next)
9256 {
9257 if (argc == MAX_FUNC_ARGS)
9258 {
9259 EMSG(_("E699: Too many arguments"));
9260 break;
9261 }
9262 /* Make a copy of each argument. This is needed to be able to set
9263 * v_lock to VAR_FIXED in the copy without changing the original list.
9264 */
9265 copy_tv(&item->li_tv, &argv[argc++]);
9266 }
9267
9268 if (item == NULL)
9269 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9270 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9271 &dummy, TRUE, selfdict);
9272
9273 /* Free the arguments. */
9274 while (argc > 0)
9275 clear_tv(&argv[--argc]);
9276
9277 return r;
9278}
9279
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009280/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009281 * "call(func, arglist)" function
9282 */
9283 static void
9284f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009285 typval_T *argvars;
9286 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009287{
9288 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009289 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009290
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009291 if (argvars[1].v_type != VAR_LIST)
9292 {
9293 EMSG(_(e_listreq));
9294 return;
9295 }
9296 if (argvars[1].vval.v_list == NULL)
9297 return;
9298
9299 if (argvars[0].v_type == VAR_FUNC)
9300 func = argvars[0].vval.v_string;
9301 else
9302 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009303 if (*func == NUL)
9304 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009305
Bram Moolenaare9a41262005-01-15 22:18:47 +00009306 if (argvars[2].v_type != VAR_UNKNOWN)
9307 {
9308 if (argvars[2].v_type != VAR_DICT)
9309 {
9310 EMSG(_(e_dictreq));
9311 return;
9312 }
9313 selfdict = argvars[2].vval.v_dict;
9314 }
9315
Bram Moolenaardb913952012-06-29 12:54:53 +02009316 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009317}
9318
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009319#ifdef FEAT_FLOAT
9320/*
9321 * "ceil({float})" function
9322 */
9323 static void
9324f_ceil(argvars, rettv)
9325 typval_T *argvars;
9326 typval_T *rettv;
9327{
9328 float_T f;
9329
9330 rettv->v_type = VAR_FLOAT;
9331 if (get_float_arg(argvars, &f) == OK)
9332 rettv->vval.v_float = ceil(f);
9333 else
9334 rettv->vval.v_float = 0.0;
9335}
9336#endif
9337
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009338/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009339 * "changenr()" function
9340 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009341 static void
9342f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009343 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009344 typval_T *rettv;
9345{
9346 rettv->vval.v_number = curbuf->b_u_seq_cur;
9347}
9348
9349/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009350 * "char2nr(string)" function
9351 */
9352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009354 typval_T *argvars;
9355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356{
9357#ifdef FEAT_MBYTE
9358 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009359 {
9360 int utf8 = 0;
9361
9362 if (argvars[1].v_type != VAR_UNKNOWN)
9363 utf8 = get_tv_number_chk(&argvars[1], NULL);
9364
9365 if (utf8)
9366 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9367 else
9368 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009370 else
9371#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009372 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009373}
9374
9375/*
9376 * "cindent(lnum)" function
9377 */
9378 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009379f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009380 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009381 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009382{
9383#ifdef FEAT_CINDENT
9384 pos_T pos;
9385 linenr_T lnum;
9386
9387 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009388 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009389 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9390 {
9391 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009392 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 curwin->w_cursor = pos;
9394 }
9395 else
9396#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009397 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398}
9399
9400/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009401 * "clearmatches()" function
9402 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009403 static void
9404f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009405 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009406 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009407{
9408#ifdef FEAT_SEARCH_EXTRA
9409 clear_matches(curwin);
9410#endif
9411}
9412
9413/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 * "col(string)" function
9415 */
9416 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009417f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009418 typval_T *argvars;
9419 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009420{
9421 colnr_T col = 0;
9422 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009423 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009425 fp = var2fpos(&argvars[0], FALSE, &fnum);
9426 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 {
9428 if (fp->col == MAXCOL)
9429 {
9430 /* '> can be MAXCOL, get the length of the line then */
9431 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009432 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 else
9434 col = MAXCOL;
9435 }
9436 else
9437 {
9438 col = fp->col + 1;
9439#ifdef FEAT_VIRTUALEDIT
9440 /* col(".") when the cursor is on the NUL at the end of the line
9441 * because of "coladd" can be seen as an extra column. */
9442 if (virtual_active() && fp == &curwin->w_cursor)
9443 {
9444 char_u *p = ml_get_cursor();
9445
9446 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9447 curwin->w_virtcol - curwin->w_cursor.coladd))
9448 {
9449# ifdef FEAT_MBYTE
9450 int l;
9451
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009452 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 col += l;
9454# else
9455 if (*p != NUL && p[1] == NUL)
9456 ++col;
9457# endif
9458 }
9459 }
9460#endif
9461 }
9462 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009463 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009464}
9465
Bram Moolenaar572cb562005-08-05 21:35:02 +00009466#if defined(FEAT_INS_EXPAND)
9467/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009468 * "complete()" function
9469 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009470 static void
9471f_complete(argvars, rettv)
9472 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009473 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009474{
9475 int startcol;
9476
9477 if ((State & INSERT) == 0)
9478 {
9479 EMSG(_("E785: complete() can only be used in Insert mode"));
9480 return;
9481 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009482
9483 /* Check for undo allowed here, because if something was already inserted
9484 * the line was already saved for undo and this check isn't done. */
9485 if (!undo_allowed())
9486 return;
9487
Bram Moolenaarade00832006-03-10 21:46:58 +00009488 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9489 {
9490 EMSG(_(e_invarg));
9491 return;
9492 }
9493
9494 startcol = get_tv_number_chk(&argvars[0], NULL);
9495 if (startcol <= 0)
9496 return;
9497
9498 set_completion(startcol - 1, argvars[1].vval.v_list);
9499}
9500
9501/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009502 * "complete_add()" function
9503 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009504 static void
9505f_complete_add(argvars, rettv)
9506 typval_T *argvars;
9507 typval_T *rettv;
9508{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009509 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009510}
9511
9512/*
9513 * "complete_check()" function
9514 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009515 static void
9516f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009517 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009518 typval_T *rettv;
9519{
9520 int saved = RedrawingDisabled;
9521
9522 RedrawingDisabled = 0;
9523 ins_compl_check_keys(0);
9524 rettv->vval.v_number = compl_interrupted;
9525 RedrawingDisabled = saved;
9526}
9527#endif
9528
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529/*
9530 * "confirm(message, buttons[, default [, type]])" function
9531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009533f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009534 typval_T *argvars UNUSED;
9535 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536{
9537#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9538 char_u *message;
9539 char_u *buttons = NULL;
9540 char_u buf[NUMBUFLEN];
9541 char_u buf2[NUMBUFLEN];
9542 int def = 1;
9543 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009544 char_u *typestr;
9545 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009547 message = get_tv_string_chk(&argvars[0]);
9548 if (message == NULL)
9549 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009550 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009552 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9553 if (buttons == NULL)
9554 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009555 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009558 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009560 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9561 if (typestr == NULL)
9562 error = TRUE;
9563 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009565 switch (TOUPPER_ASC(*typestr))
9566 {
9567 case 'E': type = VIM_ERROR; break;
9568 case 'Q': type = VIM_QUESTION; break;
9569 case 'I': type = VIM_INFO; break;
9570 case 'W': type = VIM_WARNING; break;
9571 case 'G': type = VIM_GENERIC; break;
9572 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573 }
9574 }
9575 }
9576 }
9577
9578 if (buttons == NULL || *buttons == NUL)
9579 buttons = (char_u *)_("&Ok");
9580
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009581 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009582 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009583 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584#endif
9585}
9586
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009587/*
9588 * "copy()" function
9589 */
9590 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009591f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009592 typval_T *argvars;
9593 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009594{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009595 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009596}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009598#ifdef FEAT_FLOAT
9599/*
9600 * "cos()" function
9601 */
9602 static void
9603f_cos(argvars, rettv)
9604 typval_T *argvars;
9605 typval_T *rettv;
9606{
9607 float_T f;
9608
9609 rettv->v_type = VAR_FLOAT;
9610 if (get_float_arg(argvars, &f) == OK)
9611 rettv->vval.v_float = cos(f);
9612 else
9613 rettv->vval.v_float = 0.0;
9614}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009615
9616/*
9617 * "cosh()" function
9618 */
9619 static void
9620f_cosh(argvars, rettv)
9621 typval_T *argvars;
9622 typval_T *rettv;
9623{
9624 float_T f;
9625
9626 rettv->v_type = VAR_FLOAT;
9627 if (get_float_arg(argvars, &f) == OK)
9628 rettv->vval.v_float = cosh(f);
9629 else
9630 rettv->vval.v_float = 0.0;
9631}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009632#endif
9633
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009635 * "count()" function
9636 */
9637 static void
9638f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009639 typval_T *argvars;
9640 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009641{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009642 long n = 0;
9643 int ic = FALSE;
9644
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009646 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009647 listitem_T *li;
9648 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009649 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009650
Bram Moolenaare9a41262005-01-15 22:18:47 +00009651 if ((l = argvars[0].vval.v_list) != NULL)
9652 {
9653 li = l->lv_first;
9654 if (argvars[2].v_type != VAR_UNKNOWN)
9655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009656 int error = FALSE;
9657
9658 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009659 if (argvars[3].v_type != VAR_UNKNOWN)
9660 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009661 idx = get_tv_number_chk(&argvars[3], &error);
9662 if (!error)
9663 {
9664 li = list_find(l, idx);
9665 if (li == NULL)
9666 EMSGN(_(e_listidx), idx);
9667 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009668 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009669 if (error)
9670 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009671 }
9672
9673 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009674 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009675 ++n;
9676 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009677 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009678 else if (argvars[0].v_type == VAR_DICT)
9679 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009680 int todo;
9681 dict_T *d;
9682 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009683
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009684 if ((d = argvars[0].vval.v_dict) != NULL)
9685 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009686 int error = FALSE;
9687
Bram Moolenaare9a41262005-01-15 22:18:47 +00009688 if (argvars[2].v_type != VAR_UNKNOWN)
9689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009690 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009691 if (argvars[3].v_type != VAR_UNKNOWN)
9692 EMSG(_(e_invarg));
9693 }
9694
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009695 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009696 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009697 {
9698 if (!HASHITEM_EMPTY(hi))
9699 {
9700 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009701 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009702 ++n;
9703 }
9704 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009705 }
9706 }
9707 else
9708 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009709 rettv->vval.v_number = n;
9710}
9711
9712/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9714 *
9715 * Checks the existence of a cscope connection.
9716 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009718f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009719 typval_T *argvars UNUSED;
9720 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
9722#ifdef FEAT_CSCOPE
9723 int num = 0;
9724 char_u *dbpath = NULL;
9725 char_u *prepend = NULL;
9726 char_u buf[NUMBUFLEN];
9727
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009728 if (argvars[0].v_type != VAR_UNKNOWN
9729 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009731 num = (int)get_tv_number(&argvars[0]);
9732 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009733 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009734 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009735 }
9736
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009737 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738#endif
9739}
9740
9741/*
9742 * "cursor(lnum, col)" function
9743 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009744 * Moves the cursor to the specified line and column.
9745 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009748f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009749 typval_T *argvars;
9750 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751{
9752 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009753#ifdef FEAT_VIRTUALEDIT
9754 long coladd = 0;
9755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009757 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009758 if (argvars[1].v_type == VAR_UNKNOWN)
9759 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009760 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009761
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009762 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009763 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009764 line = pos.lnum;
9765 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009766#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009767 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009768#endif
9769 }
9770 else
9771 {
9772 line = get_tv_lnum(argvars);
9773 col = get_tv_number_chk(&argvars[1], NULL);
9774#ifdef FEAT_VIRTUALEDIT
9775 if (argvars[2].v_type != VAR_UNKNOWN)
9776 coladd = get_tv_number_chk(&argvars[2], NULL);
9777#endif
9778 }
9779 if (line < 0 || col < 0
9780#ifdef FEAT_VIRTUALEDIT
9781 || coladd < 0
9782#endif
9783 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009784 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785 if (line > 0)
9786 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 if (col > 0)
9788 curwin->w_cursor.col = col - 1;
9789#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009790 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791#endif
9792
9793 /* Make sure the cursor is in a valid position. */
9794 check_cursor();
9795#ifdef FEAT_MBYTE
9796 /* Correct cursor for multi-byte character. */
9797 if (has_mbyte)
9798 mb_adjust_cursor();
9799#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009800
9801 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009802 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803}
9804
9805/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009806 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807 */
9808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009810 typval_T *argvars;
9811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009813 int noref = 0;
9814
9815 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009816 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009817 if (noref < 0 || noref > 1)
9818 EMSG(_(e_invarg));
9819 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009820 {
9821 current_copyID += COPYID_INC;
9822 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824}
9825
9826/*
9827 * "delete()" function
9828 */
9829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009831 typval_T *argvars;
9832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009833{
9834 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009837 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838}
9839
9840/*
9841 * "did_filetype()" function
9842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009845 typval_T *argvars UNUSED;
9846 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847{
9848#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850#endif
9851}
9852
9853/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009854 * "diff_filler()" function
9855 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009856 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009857f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009858 typval_T *argvars UNUSED;
9859 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009860{
9861#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009862 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009863#endif
9864}
9865
9866/*
9867 * "diff_hlID()" function
9868 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009869 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009871 typval_T *argvars UNUSED;
9872 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009873{
9874#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009875 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009876 static linenr_T prev_lnum = 0;
9877 static int changedtick = 0;
9878 static int fnum = 0;
9879 static int change_start = 0;
9880 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009881 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009882 int filler_lines;
9883 int col;
9884
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009885 if (lnum < 0) /* ignore type error in {lnum} arg */
9886 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009887 if (lnum != prev_lnum
9888 || changedtick != curbuf->b_changedtick
9889 || fnum != curbuf->b_fnum)
9890 {
9891 /* New line, buffer, change: need to get the values. */
9892 filler_lines = diff_check(curwin, lnum);
9893 if (filler_lines < 0)
9894 {
9895 if (filler_lines == -1)
9896 {
9897 change_start = MAXCOL;
9898 change_end = -1;
9899 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9900 hlID = HLF_ADD; /* added line */
9901 else
9902 hlID = HLF_CHD; /* changed line */
9903 }
9904 else
9905 hlID = HLF_ADD; /* added line */
9906 }
9907 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009908 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009909 prev_lnum = lnum;
9910 changedtick = curbuf->b_changedtick;
9911 fnum = curbuf->b_fnum;
9912 }
9913
9914 if (hlID == HLF_CHD || hlID == HLF_TXD)
9915 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009916 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009917 if (col >= change_start && col <= change_end)
9918 hlID = HLF_TXD; /* changed text */
9919 else
9920 hlID = HLF_CHD; /* changed line */
9921 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009922 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009923#endif
9924}
9925
9926/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009927 * "empty({expr})" function
9928 */
9929 static void
9930f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009931 typval_T *argvars;
9932 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009933{
9934 int n;
9935
9936 switch (argvars[0].v_type)
9937 {
9938 case VAR_STRING:
9939 case VAR_FUNC:
9940 n = argvars[0].vval.v_string == NULL
9941 || *argvars[0].vval.v_string == NUL;
9942 break;
9943 case VAR_NUMBER:
9944 n = argvars[0].vval.v_number == 0;
9945 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009946#ifdef FEAT_FLOAT
9947 case VAR_FLOAT:
9948 n = argvars[0].vval.v_float == 0.0;
9949 break;
9950#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009951 case VAR_LIST:
9952 n = argvars[0].vval.v_list == NULL
9953 || argvars[0].vval.v_list->lv_first == NULL;
9954 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009955 case VAR_DICT:
9956 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009957 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009958 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009959 default:
9960 EMSG2(_(e_intern2), "f_empty()");
9961 n = 0;
9962 }
9963
9964 rettv->vval.v_number = n;
9965}
9966
9967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009968 * "escape({string}, {chars})" function
9969 */
9970 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009971f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009972 typval_T *argvars;
9973 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974{
9975 char_u buf[NUMBUFLEN];
9976
Bram Moolenaar758711c2005-02-02 23:11:38 +00009977 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9978 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009979 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980}
9981
9982/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009983 * "eval()" function
9984 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009985 static void
9986f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009987 typval_T *argvars;
9988 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009989{
9990 char_u *s;
9991
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009992 s = get_tv_string_chk(&argvars[0]);
9993 if (s != NULL)
9994 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009995
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009996 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9997 {
9998 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009999 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010000 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010001 else if (*s != NUL)
10002 EMSG(_(e_trailing));
10003}
10004
10005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 * "eventhandler()" function
10007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010009f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010010 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010011 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010012{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010013 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014}
10015
10016/*
10017 * "executable()" function
10018 */
10019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010020f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010021 typval_T *argvars;
10022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010023{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010024 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025}
10026
10027/*
10028 * "exists()" function
10029 */
10030 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010031f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010032 typval_T *argvars;
10033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010034{
10035 char_u *p;
10036 char_u *name;
10037 int n = FALSE;
10038 int len = 0;
10039
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010040 no_autoload = TRUE;
10041
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010042 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010043 if (*p == '$') /* environment variable */
10044 {
10045 /* first try "normal" environment variables (fast) */
10046 if (mch_getenv(p + 1) != NULL)
10047 n = TRUE;
10048 else
10049 {
10050 /* try expanding things like $VIM and ${HOME} */
10051 p = expand_env_save(p);
10052 if (p != NULL && *p != '$')
10053 n = TRUE;
10054 vim_free(p);
10055 }
10056 }
10057 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010059 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010060 if (*skipwhite(p) != NUL)
10061 n = FALSE; /* trailing garbage */
10062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 else if (*p == '*') /* internal or user defined function */
10064 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010065 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066 }
10067 else if (*p == ':')
10068 {
10069 n = cmd_exists(p + 1);
10070 }
10071 else if (*p == '#')
10072 {
10073#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010074 if (p[1] == '#')
10075 n = autocmd_supported(p + 2);
10076 else
10077 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078#endif
10079 }
10080 else /* internal variable */
10081 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010082 char_u *tofree;
10083 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010085 /* get_name_len() takes care of expanding curly braces */
10086 name = p;
10087 len = get_name_len(&p, &tofree, TRUE, FALSE);
10088 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010089 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010090 if (tofree != NULL)
10091 name = tofree;
10092 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10093 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010095 /* handle d.key, l[idx], f(expr) */
10096 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10097 if (n)
10098 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010099 }
10100 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010101 if (*p != NUL)
10102 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010103
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010104 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 }
10106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010107 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010108
10109 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110}
10111
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010112#ifdef FEAT_FLOAT
10113/*
10114 * "exp()" function
10115 */
10116 static void
10117f_exp(argvars, rettv)
10118 typval_T *argvars;
10119 typval_T *rettv;
10120{
10121 float_T f;
10122
10123 rettv->v_type = VAR_FLOAT;
10124 if (get_float_arg(argvars, &f) == OK)
10125 rettv->vval.v_float = exp(f);
10126 else
10127 rettv->vval.v_float = 0.0;
10128}
10129#endif
10130
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131/*
10132 * "expand()" function
10133 */
10134 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010135f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010136 typval_T *argvars;
10137 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138{
10139 char_u *s;
10140 int len;
10141 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010142 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010144 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010145 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010147 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010148 if (argvars[1].v_type != VAR_UNKNOWN
10149 && argvars[2].v_type != VAR_UNKNOWN
10150 && get_tv_number_chk(&argvars[2], &error)
10151 && !error)
10152 {
10153 rettv->v_type = VAR_LIST;
10154 rettv->vval.v_list = NULL;
10155 }
10156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010157 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 if (*s == '%' || *s == '#' || *s == '<')
10159 {
10160 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010161 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010163 if (rettv->v_type == VAR_LIST)
10164 {
10165 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10166 list_append_string(rettv->vval.v_list, result, -1);
10167 else
10168 vim_free(result);
10169 }
10170 else
10171 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172 }
10173 else
10174 {
10175 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010176 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010177 if (argvars[1].v_type != VAR_UNKNOWN
10178 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010179 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010180 if (!error)
10181 {
10182 ExpandInit(&xpc);
10183 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010184 if (p_wic)
10185 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010186 if (rettv->v_type == VAR_STRING)
10187 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10188 options, WILD_ALL);
10189 else if (rettv_list_alloc(rettv) != FAIL)
10190 {
10191 int i;
10192
10193 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10194 for (i = 0; i < xpc.xp_numfiles; i++)
10195 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10196 ExpandCleanup(&xpc);
10197 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010198 }
10199 else
10200 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 }
10202}
10203
10204/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010205 * Go over all entries in "d2" and add them to "d1".
10206 * When "action" is "error" then a duplicate key is an error.
10207 * When "action" is "force" then a duplicate key is overwritten.
10208 * Otherwise duplicate keys are ignored ("action" is "keep").
10209 */
10210 void
10211dict_extend(d1, d2, action)
10212 dict_T *d1;
10213 dict_T *d2;
10214 char_u *action;
10215{
10216 dictitem_T *di1;
10217 hashitem_T *hi2;
10218 int todo;
10219
10220 todo = (int)d2->dv_hashtab.ht_used;
10221 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10222 {
10223 if (!HASHITEM_EMPTY(hi2))
10224 {
10225 --todo;
10226 di1 = dict_find(d1, hi2->hi_key, -1);
10227 if (d1->dv_scope != 0)
10228 {
10229 /* Disallow replacing a builtin function in l: and g:.
10230 * Check the key to be valid when adding to any
10231 * scope. */
10232 if (d1->dv_scope == VAR_DEF_SCOPE
10233 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10234 && var_check_func_name(hi2->hi_key,
10235 di1 == NULL))
10236 break;
10237 if (!valid_varname(hi2->hi_key))
10238 break;
10239 }
10240 if (di1 == NULL)
10241 {
10242 di1 = dictitem_copy(HI2DI(hi2));
10243 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10244 dictitem_free(di1);
10245 }
10246 else if (*action == 'e')
10247 {
10248 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10249 break;
10250 }
10251 else if (*action == 'f' && HI2DI(hi2) != di1)
10252 {
10253 clear_tv(&di1->di_tv);
10254 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10255 }
10256 }
10257 }
10258}
10259
10260/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010261 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010262 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010263 */
10264 static void
10265f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010266 typval_T *argvars;
10267 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010268{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010269 char *arg_errmsg = N_("extend() argument");
10270
Bram Moolenaare9a41262005-01-15 22:18:47 +000010271 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010272 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010273 list_T *l1, *l2;
10274 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010275 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010276 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010277
Bram Moolenaare9a41262005-01-15 22:18:47 +000010278 l1 = argvars[0].vval.v_list;
10279 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010280 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010281 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010282 {
10283 if (argvars[2].v_type != VAR_UNKNOWN)
10284 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010285 before = get_tv_number_chk(&argvars[2], &error);
10286 if (error)
10287 return; /* type error; errmsg already given */
10288
Bram Moolenaar758711c2005-02-02 23:11:38 +000010289 if (before == l1->lv_len)
10290 item = NULL;
10291 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010292 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010293 item = list_find(l1, before);
10294 if (item == NULL)
10295 {
10296 EMSGN(_(e_listidx), before);
10297 return;
10298 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010299 }
10300 }
10301 else
10302 item = NULL;
10303 list_extend(l1, l2, item);
10304
Bram Moolenaare9a41262005-01-15 22:18:47 +000010305 copy_tv(&argvars[0], rettv);
10306 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010307 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010308 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10309 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010310 dict_T *d1, *d2;
10311 char_u *action;
10312 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010313
10314 d1 = argvars[0].vval.v_dict;
10315 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010316 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010317 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010318 {
10319 /* Check the third argument. */
10320 if (argvars[2].v_type != VAR_UNKNOWN)
10321 {
10322 static char *(av[]) = {"keep", "force", "error"};
10323
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010324 action = get_tv_string_chk(&argvars[2]);
10325 if (action == NULL)
10326 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010327 for (i = 0; i < 3; ++i)
10328 if (STRCMP(action, av[i]) == 0)
10329 break;
10330 if (i == 3)
10331 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010332 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010333 return;
10334 }
10335 }
10336 else
10337 action = (char_u *)"force";
10338
Bram Moolenaara9922d62013-05-30 13:01:18 +020010339 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010340
Bram Moolenaare9a41262005-01-15 22:18:47 +000010341 copy_tv(&argvars[0], rettv);
10342 }
10343 }
10344 else
10345 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010346}
10347
10348/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010349 * "feedkeys()" function
10350 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010351 static void
10352f_feedkeys(argvars, rettv)
10353 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010354 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010355{
10356 int remap = TRUE;
10357 char_u *keys, *flags;
10358 char_u nbuf[NUMBUFLEN];
10359 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010360 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010361
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010362 /* This is not allowed in the sandbox. If the commands would still be
10363 * executed in the sandbox it would be OK, but it probably happens later,
10364 * when "sandbox" is no longer set. */
10365 if (check_secure())
10366 return;
10367
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010368 keys = get_tv_string(&argvars[0]);
10369 if (*keys != NUL)
10370 {
10371 if (argvars[1].v_type != VAR_UNKNOWN)
10372 {
10373 flags = get_tv_string_buf(&argvars[1], nbuf);
10374 for ( ; *flags != NUL; ++flags)
10375 {
10376 switch (*flags)
10377 {
10378 case 'n': remap = FALSE; break;
10379 case 'm': remap = TRUE; break;
10380 case 't': typed = TRUE; break;
10381 }
10382 }
10383 }
10384
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010385 /* Need to escape K_SPECIAL and CSI before putting the string in the
10386 * typeahead buffer. */
10387 keys_esc = vim_strsave_escape_csi(keys);
10388 if (keys_esc != NULL)
10389 {
10390 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010391 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010392 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010393 if (vgetc_busy)
10394 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010395 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010396 }
10397}
10398
10399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400 * "filereadable()" function
10401 */
10402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010403f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010404 typval_T *argvars;
10405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010406{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010407 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408 char_u *p;
10409 int n;
10410
Bram Moolenaarc236c162008-07-13 17:41:49 +000010411#ifndef O_NONBLOCK
10412# define O_NONBLOCK 0
10413#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010414 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010415 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10416 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010417 {
10418 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010419 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 }
10421 else
10422 n = FALSE;
10423
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010424 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010425}
10426
10427/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010428 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010429 * rights to write into.
10430 */
10431 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010432f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010433 typval_T *argvars;
10434 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010435{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010436 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437}
10438
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010439static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010440
10441 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010442findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010443 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010444 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010445 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010446{
10447#ifdef FEAT_SEARCHPATH
10448 char_u *fname;
10449 char_u *fresult = NULL;
10450 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10451 char_u *p;
10452 char_u pathbuf[NUMBUFLEN];
10453 int count = 1;
10454 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010455 int error = FALSE;
10456#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010457
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010458 rettv->vval.v_string = NULL;
10459 rettv->v_type = VAR_STRING;
10460
10461#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010462 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010463
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010464 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010465 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10467 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010468 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010469 else
10470 {
10471 if (*p != NUL)
10472 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010473
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010474 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010475 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010476 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010477 }
10478
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010479 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10480 error = TRUE;
10481
10482 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010483 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010484 do
10485 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010486 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010487 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010488 fresult = find_file_in_path_option(first ? fname : NULL,
10489 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010490 0, first, path,
10491 find_what,
10492 curbuf->b_ffname,
10493 find_what == FINDFILE_DIR
10494 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010495 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010496
10497 if (fresult != NULL && rettv->v_type == VAR_LIST)
10498 list_append_string(rettv->vval.v_list, fresult, -1);
10499
10500 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010501 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010502
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010503 if (rettv->v_type == VAR_STRING)
10504 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010505#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010506}
10507
Bram Moolenaar33570922005-01-25 22:26:29 +000010508static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10509static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010510
10511/*
10512 * Implementation of map() and filter().
10513 */
10514 static void
10515filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010516 typval_T *argvars;
10517 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010518 int map;
10519{
10520 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010521 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010522 listitem_T *li, *nli;
10523 list_T *l = NULL;
10524 dictitem_T *di;
10525 hashtab_T *ht;
10526 hashitem_T *hi;
10527 dict_T *d = NULL;
10528 typval_T save_val;
10529 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010530 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010531 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010532 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10533 char *arg_errmsg = (map ? N_("map() argument")
10534 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010535 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010536 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010537
Bram Moolenaare9a41262005-01-15 22:18:47 +000010538 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010539 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010540 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010541 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010542 return;
10543 }
10544 else if (argvars[0].v_type == VAR_DICT)
10545 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010546 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010547 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010548 return;
10549 }
10550 else
10551 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010552 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010553 return;
10554 }
10555
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010556 expr = get_tv_string_buf_chk(&argvars[1], buf);
10557 /* On type errors, the preceding call has already displayed an error
10558 * message. Avoid a misleading error message for an empty string that
10559 * was not passed as argument. */
10560 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010561 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010562 prepare_vimvar(VV_VAL, &save_val);
10563 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010564
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010565 /* We reset "did_emsg" to be able to detect whether an error
10566 * occurred during evaluation of the expression. */
10567 save_did_emsg = did_emsg;
10568 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010569
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010570 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010572 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010573 vimvars[VV_KEY].vv_type = VAR_STRING;
10574
10575 ht = &d->dv_hashtab;
10576 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010577 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010578 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010579 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010580 if (!HASHITEM_EMPTY(hi))
10581 {
10582 --todo;
10583 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010584 if (tv_check_lock(di->di_tv.v_lock,
10585 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010586 break;
10587 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010588 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010589 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010590 break;
10591 if (!map && rem)
10592 dictitem_remove(d, di);
10593 clear_tv(&vimvars[VV_KEY].vv_tv);
10594 }
10595 }
10596 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010597 }
10598 else
10599 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010600 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10601
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010602 for (li = l->lv_first; li != NULL; li = nli)
10603 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010604 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010605 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010606 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010607 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010608 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010609 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010610 break;
10611 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010612 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010613 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010614 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010615 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010616
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010617 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010618 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010619
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010620 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010621 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010622
10623 copy_tv(&argvars[0], rettv);
10624}
10625
10626 static int
10627filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010628 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010629 char_u *expr;
10630 int map;
10631 int *remp;
10632{
Bram Moolenaar33570922005-01-25 22:26:29 +000010633 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010634 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010635 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010636
Bram Moolenaar33570922005-01-25 22:26:29 +000010637 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010638 s = expr;
10639 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010640 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010641 if (*s != NUL) /* check for trailing chars after expr */
10642 {
10643 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010644 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010645 }
10646 if (map)
10647 {
10648 /* map(): replace the list item value */
10649 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010650 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010651 *tv = rettv;
10652 }
10653 else
10654 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010655 int error = FALSE;
10656
Bram Moolenaare9a41262005-01-15 22:18:47 +000010657 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010658 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010659 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010660 /* On type error, nothing has been removed; return FAIL to stop the
10661 * loop. The error message was given by get_tv_number_chk(). */
10662 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010663 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010664 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010665 retval = OK;
10666theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010667 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010668 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010669}
10670
10671/*
10672 * "filter()" function
10673 */
10674 static void
10675f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010676 typval_T *argvars;
10677 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010678{
10679 filter_map(argvars, rettv, FALSE);
10680}
10681
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010682/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010683 * "finddir({fname}[, {path}[, {count}]])" function
10684 */
10685 static void
10686f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010687 typval_T *argvars;
10688 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010689{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010690 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010691}
10692
10693/*
10694 * "findfile({fname}[, {path}[, {count}]])" function
10695 */
10696 static void
10697f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010698 typval_T *argvars;
10699 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010700{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010701 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010702}
10703
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010704#ifdef FEAT_FLOAT
10705/*
10706 * "float2nr({float})" function
10707 */
10708 static void
10709f_float2nr(argvars, rettv)
10710 typval_T *argvars;
10711 typval_T *rettv;
10712{
10713 float_T f;
10714
10715 if (get_float_arg(argvars, &f) == OK)
10716 {
10717 if (f < -0x7fffffff)
10718 rettv->vval.v_number = -0x7fffffff;
10719 else if (f > 0x7fffffff)
10720 rettv->vval.v_number = 0x7fffffff;
10721 else
10722 rettv->vval.v_number = (varnumber_T)f;
10723 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010724}
10725
10726/*
10727 * "floor({float})" function
10728 */
10729 static void
10730f_floor(argvars, rettv)
10731 typval_T *argvars;
10732 typval_T *rettv;
10733{
10734 float_T f;
10735
10736 rettv->v_type = VAR_FLOAT;
10737 if (get_float_arg(argvars, &f) == OK)
10738 rettv->vval.v_float = floor(f);
10739 else
10740 rettv->vval.v_float = 0.0;
10741}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010742
10743/*
10744 * "fmod()" function
10745 */
10746 static void
10747f_fmod(argvars, rettv)
10748 typval_T *argvars;
10749 typval_T *rettv;
10750{
10751 float_T fx, fy;
10752
10753 rettv->v_type = VAR_FLOAT;
10754 if (get_float_arg(argvars, &fx) == OK
10755 && get_float_arg(&argvars[1], &fy) == OK)
10756 rettv->vval.v_float = fmod(fx, fy);
10757 else
10758 rettv->vval.v_float = 0.0;
10759}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010760#endif
10761
Bram Moolenaar0d660222005-01-07 21:51:51 +000010762/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010763 * "fnameescape({string})" function
10764 */
10765 static void
10766f_fnameescape(argvars, rettv)
10767 typval_T *argvars;
10768 typval_T *rettv;
10769{
10770 rettv->vval.v_string = vim_strsave_fnameescape(
10771 get_tv_string(&argvars[0]), FALSE);
10772 rettv->v_type = VAR_STRING;
10773}
10774
10775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 * "fnamemodify({fname}, {mods})" function
10777 */
10778 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010779f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010780 typval_T *argvars;
10781 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782{
10783 char_u *fname;
10784 char_u *mods;
10785 int usedlen = 0;
10786 int len;
10787 char_u *fbuf = NULL;
10788 char_u buf[NUMBUFLEN];
10789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010790 fname = get_tv_string_chk(&argvars[0]);
10791 mods = get_tv_string_buf_chk(&argvars[1], buf);
10792 if (fname == NULL || mods == NULL)
10793 fname = NULL;
10794 else
10795 {
10796 len = (int)STRLEN(fname);
10797 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010799
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010800 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010801 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010802 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010804 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 vim_free(fbuf);
10806}
10807
Bram Moolenaar33570922005-01-25 22:26:29 +000010808static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809
10810/*
10811 * "foldclosed()" function
10812 */
10813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010814foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010815 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010816 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010817 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010818{
10819#ifdef FEAT_FOLDING
10820 linenr_T lnum;
10821 linenr_T first, last;
10822
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010823 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10825 {
10826 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10827 {
10828 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010829 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010830 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010831 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 return;
10833 }
10834 }
10835#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010836 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837}
10838
10839/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010840 * "foldclosed()" function
10841 */
10842 static void
10843f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010844 typval_T *argvars;
10845 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010846{
10847 foldclosed_both(argvars, rettv, FALSE);
10848}
10849
10850/*
10851 * "foldclosedend()" function
10852 */
10853 static void
10854f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010855 typval_T *argvars;
10856 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010857{
10858 foldclosed_both(argvars, rettv, TRUE);
10859}
10860
10861/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010862 * "foldlevel()" function
10863 */
10864 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010865f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010866 typval_T *argvars UNUSED;
10867 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868{
10869#ifdef FEAT_FOLDING
10870 linenr_T lnum;
10871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010872 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010874 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876}
10877
10878/*
10879 * "foldtext()" function
10880 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010881 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010882f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010883 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010884 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885{
10886#ifdef FEAT_FOLDING
10887 linenr_T lnum;
10888 char_u *s;
10889 char_u *r;
10890 int len;
10891 char *txt;
10892#endif
10893
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010894 rettv->v_type = VAR_STRING;
10895 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010897 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10898 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10899 <= curbuf->b_ml.ml_line_count
10900 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 {
10902 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010903 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10904 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010905 {
10906 if (!linewhite(lnum))
10907 break;
10908 ++lnum;
10909 }
10910
10911 /* Find interesting text in this line. */
10912 s = skipwhite(ml_get(lnum));
10913 /* skip C comment-start */
10914 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010915 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010916 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010917 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010918 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010919 {
10920 s = skipwhite(ml_get(lnum + 1));
10921 if (*s == '*')
10922 s = skipwhite(s + 1);
10923 }
10924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010925 txt = _("+-%s%3ld lines: ");
10926 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010927 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928 + 20 /* for %3ld */
10929 + STRLEN(s))); /* concatenated */
10930 if (r != NULL)
10931 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010932 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10933 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10934 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010935 len = (int)STRLEN(r);
10936 STRCAT(r, s);
10937 /* remove 'foldmarker' and 'commentstring' */
10938 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010939 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940 }
10941 }
10942#endif
10943}
10944
10945/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010946 * "foldtextresult(lnum)" function
10947 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010948 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010950 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010951 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010952{
10953#ifdef FEAT_FOLDING
10954 linenr_T lnum;
10955 char_u *text;
10956 char_u buf[51];
10957 foldinfo_T foldinfo;
10958 int fold_count;
10959#endif
10960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010961 rettv->v_type = VAR_STRING;
10962 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010963#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010964 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010965 /* treat illegal types and illegal string values for {lnum} the same */
10966 if (lnum < 0)
10967 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010968 fold_count = foldedCount(curwin, lnum, &foldinfo);
10969 if (fold_count > 0)
10970 {
10971 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10972 &foldinfo, buf);
10973 if (text == buf)
10974 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010976 }
10977#endif
10978}
10979
10980/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010981 * "foreground()" function
10982 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010983 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010984f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010985 typval_T *argvars UNUSED;
10986 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010987{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010988#ifdef FEAT_GUI
10989 if (gui.in_use)
10990 gui_mch_set_foreground();
10991#else
10992# ifdef WIN32
10993 win32_set_foreground();
10994# endif
10995#endif
10996}
10997
10998/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010999 * "function()" function
11000 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011001 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011002f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011003 typval_T *argvars;
11004 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011005{
11006 char_u *s;
11007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011008 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011009 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011010 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011011 /* Don't check an autoload name for existence here. */
11012 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011013 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011014 else
11015 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011016 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011017 {
11018 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011019 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011020
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011021 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11022 * also be called from another script. Using trans_function_name()
11023 * would also work, but some plugins depend on the name being
11024 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011025 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011026 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011027 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011028 if (rettv->vval.v_string != NULL)
11029 {
11030 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011031 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011032 }
11033 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011034 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011035 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011036 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011037 }
11038}
11039
11040/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011041 * "garbagecollect()" function
11042 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011043 static void
11044f_garbagecollect(argvars, rettv)
11045 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011046 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011047{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011048 /* This is postponed until we are back at the toplevel, because we may be
11049 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11050 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011051
11052 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11053 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011054}
11055
11056/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011057 * "get()" function
11058 */
11059 static void
11060f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011061 typval_T *argvars;
11062 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011063{
Bram Moolenaar33570922005-01-25 22:26:29 +000011064 listitem_T *li;
11065 list_T *l;
11066 dictitem_T *di;
11067 dict_T *d;
11068 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011069
Bram Moolenaare9a41262005-01-15 22:18:47 +000011070 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011071 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011072 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011073 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011074 int error = FALSE;
11075
11076 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11077 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011078 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011079 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011080 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011081 else if (argvars[0].v_type == VAR_DICT)
11082 {
11083 if ((d = argvars[0].vval.v_dict) != NULL)
11084 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011085 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011086 if (di != NULL)
11087 tv = &di->di_tv;
11088 }
11089 }
11090 else
11091 EMSG2(_(e_listdictarg), "get()");
11092
11093 if (tv == NULL)
11094 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011095 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011096 copy_tv(&argvars[2], rettv);
11097 }
11098 else
11099 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011100}
11101
Bram Moolenaar342337a2005-07-21 21:11:17 +000011102static 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 +000011103
11104/*
11105 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011106 * Return a range (from start to end) of lines in rettv from the specified
11107 * buffer.
11108 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011109 */
11110 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011111get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011112 buf_T *buf;
11113 linenr_T start;
11114 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011115 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011116 typval_T *rettv;
11117{
11118 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011119
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011120 if (retlist && rettv_list_alloc(rettv) == FAIL)
11121 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011122
11123 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11124 return;
11125
11126 if (!retlist)
11127 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011128 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11129 p = ml_get_buf(buf, start, FALSE);
11130 else
11131 p = (char_u *)"";
11132
11133 rettv->v_type = VAR_STRING;
11134 rettv->vval.v_string = vim_strsave(p);
11135 }
11136 else
11137 {
11138 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011139 return;
11140
11141 if (start < 1)
11142 start = 1;
11143 if (end > buf->b_ml.ml_line_count)
11144 end = buf->b_ml.ml_line_count;
11145 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011146 if (list_append_string(rettv->vval.v_list,
11147 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011148 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011149 }
11150}
11151
11152/*
11153 * "getbufline()" function
11154 */
11155 static void
11156f_getbufline(argvars, rettv)
11157 typval_T *argvars;
11158 typval_T *rettv;
11159{
11160 linenr_T lnum;
11161 linenr_T end;
11162 buf_T *buf;
11163
11164 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11165 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011166 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011167 --emsg_off;
11168
Bram Moolenaar661b1822005-07-28 22:36:45 +000011169 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011170 if (argvars[2].v_type == VAR_UNKNOWN)
11171 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011172 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011173 end = get_tv_lnum_buf(&argvars[2], buf);
11174
Bram Moolenaar342337a2005-07-21 21:11:17 +000011175 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011176}
11177
Bram Moolenaar0d660222005-01-07 21:51:51 +000011178/*
11179 * "getbufvar()" function
11180 */
11181 static void
11182f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011183 typval_T *argvars;
11184 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011185{
11186 buf_T *buf;
11187 buf_T *save_curbuf;
11188 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011189 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011190 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011191
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011192 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11193 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011194 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011195 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011196
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011197 rettv->v_type = VAR_STRING;
11198 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011199
11200 if (buf != NULL && varname != NULL)
11201 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011202 /* set curbuf to be our buf, temporarily */
11203 save_curbuf = curbuf;
11204 curbuf = buf;
11205
Bram Moolenaar0d660222005-01-07 21:51:51 +000011206 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011207 {
11208 if (get_option_tv(&varname, rettv, TRUE) == OK)
11209 done = TRUE;
11210 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011211 else if (STRCMP(varname, "changedtick") == 0)
11212 {
11213 rettv->v_type = VAR_NUMBER;
11214 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011215 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011216 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011217 else
11218 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011219 /* Look up the variable. */
11220 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11221 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11222 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011223 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011224 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011225 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011226 done = TRUE;
11227 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011228 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011229
11230 /* restore previous notion of curbuf */
11231 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011232 }
11233
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011234 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11235 /* use the default value */
11236 copy_tv(&argvars[2], rettv);
11237
Bram Moolenaar0d660222005-01-07 21:51:51 +000011238 --emsg_off;
11239}
11240
11241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 * "getchar()" function
11243 */
11244 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011245f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011246 typval_T *argvars;
11247 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248{
11249 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011250 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011251
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011252 /* Position the cursor. Needed after a message that ends in a space. */
11253 windgoto(msg_row, msg_col);
11254
Bram Moolenaar071d4272004-06-13 20:20:40 +000011255 ++no_mapping;
11256 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011257 for (;;)
11258 {
11259 if (argvars[0].v_type == VAR_UNKNOWN)
11260 /* getchar(): blocking wait. */
11261 n = safe_vgetc();
11262 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11263 /* getchar(1): only check if char avail */
11264 n = vpeekc();
11265 else if (error || vpeekc() == NUL)
11266 /* illegal argument or getchar(0) and no char avail: return zero */
11267 n = 0;
11268 else
11269 /* getchar(0) and char avail: return char */
11270 n = safe_vgetc();
11271 if (n == K_IGNORE)
11272 continue;
11273 break;
11274 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275 --no_mapping;
11276 --allow_keys;
11277
Bram Moolenaar219b8702006-11-01 14:32:36 +000011278 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11279 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11280 vimvars[VV_MOUSE_COL].vv_nr = 0;
11281
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011283 if (IS_SPECIAL(n) || mod_mask != 0)
11284 {
11285 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11286 int i = 0;
11287
11288 /* Turn a special key into three bytes, plus modifier. */
11289 if (mod_mask != 0)
11290 {
11291 temp[i++] = K_SPECIAL;
11292 temp[i++] = KS_MODIFIER;
11293 temp[i++] = mod_mask;
11294 }
11295 if (IS_SPECIAL(n))
11296 {
11297 temp[i++] = K_SPECIAL;
11298 temp[i++] = K_SECOND(n);
11299 temp[i++] = K_THIRD(n);
11300 }
11301#ifdef FEAT_MBYTE
11302 else if (has_mbyte)
11303 i += (*mb_char2bytes)(n, temp + i);
11304#endif
11305 else
11306 temp[i++] = n;
11307 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011308 rettv->v_type = VAR_STRING;
11309 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011310
11311#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011312 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011313 {
11314 int row = mouse_row;
11315 int col = mouse_col;
11316 win_T *win;
11317 linenr_T lnum;
11318# ifdef FEAT_WINDOWS
11319 win_T *wp;
11320# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011321 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011322
11323 if (row >= 0 && col >= 0)
11324 {
11325 /* Find the window at the mouse coordinates and compute the
11326 * text position. */
11327 win = mouse_find_win(&row, &col);
11328 (void)mouse_comp_pos(win, &row, &col, &lnum);
11329# ifdef FEAT_WINDOWS
11330 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011331 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011332# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011333 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011334 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11335 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11336 }
11337 }
11338#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 }
11340}
11341
11342/*
11343 * "getcharmod()" function
11344 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011347 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011348 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011350 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011351}
11352
11353/*
11354 * "getcmdline()" function
11355 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011357f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011358 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011360{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011361 rettv->v_type = VAR_STRING;
11362 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011363}
11364
11365/*
11366 * "getcmdpos()" function
11367 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011369f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011370 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011371 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011373 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374}
11375
11376/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011377 * "getcmdtype()" function
11378 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011379 static void
11380f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011381 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011382 typval_T *rettv;
11383{
11384 rettv->v_type = VAR_STRING;
11385 rettv->vval.v_string = alloc(2);
11386 if (rettv->vval.v_string != NULL)
11387 {
11388 rettv->vval.v_string[0] = get_cmdline_type();
11389 rettv->vval.v_string[1] = NUL;
11390 }
11391}
11392
11393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 * "getcwd()" function
11395 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011397f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011398 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011399 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011400{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011401 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011403 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011404 rettv->vval.v_string = NULL;
11405 cwd = alloc(MAXPATHL);
11406 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011407 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011408 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11409 {
11410 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011412 if (rettv->vval.v_string != NULL)
11413 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011415 }
11416 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011417 }
11418}
11419
11420/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011421 * "getfontname()" function
11422 */
11423 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011424f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011425 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011426 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011427{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011428 rettv->v_type = VAR_STRING;
11429 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011430#ifdef FEAT_GUI
11431 if (gui.in_use)
11432 {
11433 GuiFont font;
11434 char_u *name = NULL;
11435
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011436 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011437 {
11438 /* Get the "Normal" font. Either the name saved by
11439 * hl_set_font_name() or from the font ID. */
11440 font = gui.norm_font;
11441 name = hl_get_font_name();
11442 }
11443 else
11444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011446 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11447 return;
11448 font = gui_mch_get_font(name, FALSE);
11449 if (font == NOFONT)
11450 return; /* Invalid font name, return empty string. */
11451 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011452 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011453 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011454 gui_mch_free_font(font);
11455 }
11456#endif
11457}
11458
11459/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011460 * "getfperm({fname})" function
11461 */
11462 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011463f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011464 typval_T *argvars;
11465 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011466{
11467 char_u *fname;
11468 struct stat st;
11469 char_u *perm = NULL;
11470 char_u flags[] = "rwx";
11471 int i;
11472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011473 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011476 if (mch_stat((char *)fname, &st) >= 0)
11477 {
11478 perm = vim_strsave((char_u *)"---------");
11479 if (perm != NULL)
11480 {
11481 for (i = 0; i < 9; i++)
11482 {
11483 if (st.st_mode & (1 << (8 - i)))
11484 perm[i] = flags[i % 3];
11485 }
11486 }
11487 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011488 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011489}
11490
11491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 * "getfsize({fname})" function
11493 */
11494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011495f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011496 typval_T *argvars;
11497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011498{
11499 char_u *fname;
11500 struct stat st;
11501
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011502 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011504 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011505
11506 if (mch_stat((char *)fname, &st) >= 0)
11507 {
11508 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011509 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011510 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011511 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011512 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011513
11514 /* non-perfect check for overflow */
11515 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11516 rettv->vval.v_number = -2;
11517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518 }
11519 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011520 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011521}
11522
11523/*
11524 * "getftime({fname})" function
11525 */
11526 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011528 typval_T *argvars;
11529 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011530{
11531 char_u *fname;
11532 struct stat st;
11533
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011534 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011535
11536 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011539 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011540}
11541
11542/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011543 * "getftype({fname})" function
11544 */
11545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011546f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011547 typval_T *argvars;
11548 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011549{
11550 char_u *fname;
11551 struct stat st;
11552 char_u *type = NULL;
11553 char *t;
11554
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011555 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011557 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011558 if (mch_lstat((char *)fname, &st) >= 0)
11559 {
11560#ifdef S_ISREG
11561 if (S_ISREG(st.st_mode))
11562 t = "file";
11563 else if (S_ISDIR(st.st_mode))
11564 t = "dir";
11565# ifdef S_ISLNK
11566 else if (S_ISLNK(st.st_mode))
11567 t = "link";
11568# endif
11569# ifdef S_ISBLK
11570 else if (S_ISBLK(st.st_mode))
11571 t = "bdev";
11572# endif
11573# ifdef S_ISCHR
11574 else if (S_ISCHR(st.st_mode))
11575 t = "cdev";
11576# endif
11577# ifdef S_ISFIFO
11578 else if (S_ISFIFO(st.st_mode))
11579 t = "fifo";
11580# endif
11581# ifdef S_ISSOCK
11582 else if (S_ISSOCK(st.st_mode))
11583 t = "fifo";
11584# endif
11585 else
11586 t = "other";
11587#else
11588# ifdef S_IFMT
11589 switch (st.st_mode & S_IFMT)
11590 {
11591 case S_IFREG: t = "file"; break;
11592 case S_IFDIR: t = "dir"; break;
11593# ifdef S_IFLNK
11594 case S_IFLNK: t = "link"; break;
11595# endif
11596# ifdef S_IFBLK
11597 case S_IFBLK: t = "bdev"; break;
11598# endif
11599# ifdef S_IFCHR
11600 case S_IFCHR: t = "cdev"; break;
11601# endif
11602# ifdef S_IFIFO
11603 case S_IFIFO: t = "fifo"; break;
11604# endif
11605# ifdef S_IFSOCK
11606 case S_IFSOCK: t = "socket"; break;
11607# endif
11608 default: t = "other";
11609 }
11610# else
11611 if (mch_isdir(fname))
11612 t = "dir";
11613 else
11614 t = "file";
11615# endif
11616#endif
11617 type = vim_strsave((char_u *)t);
11618 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011619 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011620}
11621
11622/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011623 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011624 */
11625 static void
11626f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011627 typval_T *argvars;
11628 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011629{
11630 linenr_T lnum;
11631 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011632 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011633
11634 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011635 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011636 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011637 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011638 retlist = FALSE;
11639 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011640 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011641 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011642 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011643 retlist = TRUE;
11644 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011645
Bram Moolenaar342337a2005-07-21 21:11:17 +000011646 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011647}
11648
11649/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011650 * "getmatches()" function
11651 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011652 static void
11653f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011654 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011655 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011656{
11657#ifdef FEAT_SEARCH_EXTRA
11658 dict_T *dict;
11659 matchitem_T *cur = curwin->w_match_head;
11660
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011661 if (rettv_list_alloc(rettv) == OK)
11662 {
11663 while (cur != NULL)
11664 {
11665 dict = dict_alloc();
11666 if (dict == NULL)
11667 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011668 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11669 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11670 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11671 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11672 list_append_dict(rettv->vval.v_list, dict);
11673 cur = cur->next;
11674 }
11675 }
11676#endif
11677}
11678
11679/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011680 * "getpid()" function
11681 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011682 static void
11683f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011684 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011685 typval_T *rettv;
11686{
11687 rettv->vval.v_number = mch_get_pid();
11688}
11689
11690/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011691 * "getpos(string)" function
11692 */
11693 static void
11694f_getpos(argvars, rettv)
11695 typval_T *argvars;
11696 typval_T *rettv;
11697{
11698 pos_T *fp;
11699 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011700 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011701
11702 if (rettv_list_alloc(rettv) == OK)
11703 {
11704 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011705 fp = var2fpos(&argvars[0], TRUE, &fnum);
11706 if (fnum != -1)
11707 list_append_number(l, (varnumber_T)fnum);
11708 else
11709 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011710 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11711 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011712 list_append_number(l, (fp != NULL)
11713 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011714 : (varnumber_T)0);
11715 list_append_number(l,
11716#ifdef FEAT_VIRTUALEDIT
11717 (fp != NULL) ? (varnumber_T)fp->coladd :
11718#endif
11719 (varnumber_T)0);
11720 }
11721 else
11722 rettv->vval.v_number = FALSE;
11723}
11724
11725/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011726 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011727 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011728 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011729f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011730 typval_T *argvars UNUSED;
11731 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011732{
11733#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011734 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011735#endif
11736
Bram Moolenaar2641f772005-03-25 21:58:17 +000011737#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011738 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011739 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011740 wp = NULL;
11741 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11742 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011743 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011744 if (wp == NULL)
11745 return;
11746 }
11747
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011748 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011749 }
11750#endif
11751}
11752
11753/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 * "getreg()" function
11755 */
11756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011757f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011758 typval_T *argvars;
11759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760{
11761 char_u *strregname;
11762 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011763 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011764 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011766 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011767 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011768 strregname = get_tv_string_chk(&argvars[0]);
11769 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011770 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011771 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011773 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011774 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011775 regname = (strregname == NULL ? '"' : *strregname);
11776 if (regname == 0)
11777 regname = '"';
11778
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011780 rettv->vval.v_string = error ? NULL :
11781 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782}
11783
11784/*
11785 * "getregtype()" function
11786 */
11787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011789 typval_T *argvars;
11790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791{
11792 char_u *strregname;
11793 int regname;
11794 char_u buf[NUMBUFLEN + 2];
11795 long reglen = 0;
11796
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011797 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011798 {
11799 strregname = get_tv_string_chk(&argvars[0]);
11800 if (strregname == NULL) /* type error; errmsg already given */
11801 {
11802 rettv->v_type = VAR_STRING;
11803 rettv->vval.v_string = NULL;
11804 return;
11805 }
11806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011807 else
11808 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011809 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810
11811 regname = (strregname == NULL ? '"' : *strregname);
11812 if (regname == 0)
11813 regname = '"';
11814
11815 buf[0] = NUL;
11816 buf[1] = NUL;
11817 switch (get_reg_type(regname, &reglen))
11818 {
11819 case MLINE: buf[0] = 'V'; break;
11820 case MCHAR: buf[0] = 'v'; break;
11821#ifdef FEAT_VISUAL
11822 case MBLOCK:
11823 buf[0] = Ctrl_V;
11824 sprintf((char *)buf + 1, "%ld", reglen + 1);
11825 break;
11826#endif
11827 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011828 rettv->v_type = VAR_STRING;
11829 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011830}
11831
11832/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011833 * "gettabvar()" function
11834 */
11835 static void
11836f_gettabvar(argvars, rettv)
11837 typval_T *argvars;
11838 typval_T *rettv;
11839{
11840 tabpage_T *tp;
11841 dictitem_T *v;
11842 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011843 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011844
11845 rettv->v_type = VAR_STRING;
11846 rettv->vval.v_string = NULL;
11847
11848 varname = get_tv_string_chk(&argvars[1]);
11849 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11850 if (tp != NULL && varname != NULL)
11851 {
11852 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011853 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011854 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011855 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011856 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011857 done = TRUE;
11858 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011859 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011860
11861 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011862 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011863}
11864
11865/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011866 * "gettabwinvar()" function
11867 */
11868 static void
11869f_gettabwinvar(argvars, rettv)
11870 typval_T *argvars;
11871 typval_T *rettv;
11872{
11873 getwinvar(argvars, rettv, 1);
11874}
11875
11876/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 * "getwinposx()" function
11878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011880f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011881 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011883{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011884 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885#ifdef FEAT_GUI
11886 if (gui.in_use)
11887 {
11888 int x, y;
11889
11890 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011891 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011892 }
11893#endif
11894}
11895
11896/*
11897 * "getwinposy()" function
11898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011900f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011901 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011903{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011905#ifdef FEAT_GUI
11906 if (gui.in_use)
11907 {
11908 int x, y;
11909
11910 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011911 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011912 }
11913#endif
11914}
11915
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011916/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011917 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011918 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011919 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011920find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011921 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011922 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011923{
11924#ifdef FEAT_WINDOWS
11925 win_T *wp;
11926#endif
11927 int nr;
11928
11929 nr = get_tv_number_chk(vp, NULL);
11930
11931#ifdef FEAT_WINDOWS
11932 if (nr < 0)
11933 return NULL;
11934 if (nr == 0)
11935 return curwin;
11936
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011937 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11938 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011939 if (--nr <= 0)
11940 break;
11941 return wp;
11942#else
11943 if (nr == 0 || nr == 1)
11944 return curwin;
11945 return NULL;
11946#endif
11947}
11948
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949/*
11950 * "getwinvar()" function
11951 */
11952 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011953f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011954 typval_T *argvars;
11955 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011956{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011957 getwinvar(argvars, rettv, 0);
11958}
11959
11960/*
11961 * getwinvar() and gettabwinvar()
11962 */
11963 static void
11964getwinvar(argvars, rettv, off)
11965 typval_T *argvars;
11966 typval_T *rettv;
11967 int off; /* 1 for gettabwinvar() */
11968{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969 win_T *win, *oldcurwin;
11970 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011971 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011972 tabpage_T *tp = NULL;
11973 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011974 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011975
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011976#ifdef FEAT_WINDOWS
11977 if (off == 1)
11978 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11979 else
11980 tp = curtab;
11981#endif
11982 win = find_win_by_nr(&argvars[off], tp);
11983 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011984 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011985
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011986 rettv->v_type = VAR_STRING;
11987 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988
11989 if (win != NULL && varname != NULL)
11990 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011991 /* Set curwin to be our win, temporarily. Also set the tabpage,
11992 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020011993 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011994
Bram Moolenaar071d4272004-06-13 20:20:40 +000011995 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011996 {
11997 if (get_option_tv(&varname, rettv, 1) == OK)
11998 done = TRUE;
11999 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012000 else
12001 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012002 /* Look up the variable. */
12003 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
12004 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012006 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012007 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012008 done = TRUE;
12009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012010 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000012011
12012 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020012013 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012014 }
12015
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012016 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
12017 /* use the default return value */
12018 copy_tv(&argvars[off + 2], rettv);
12019
Bram Moolenaar071d4272004-06-13 20:20:40 +000012020 --emsg_off;
12021}
12022
12023/*
12024 * "glob()" function
12025 */
12026 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012027f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012028 typval_T *argvars;
12029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012030{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012031 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012032 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012033 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012034
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012035 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012036 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012037 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012038 if (argvars[1].v_type != VAR_UNKNOWN)
12039 {
12040 if (get_tv_number_chk(&argvars[1], &error))
12041 options |= WILD_KEEP_ALL;
12042 if (argvars[2].v_type != VAR_UNKNOWN
12043 && get_tv_number_chk(&argvars[2], &error))
12044 {
12045 rettv->v_type = VAR_LIST;
12046 rettv->vval.v_list = NULL;
12047 }
12048 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012049 if (!error)
12050 {
12051 ExpandInit(&xpc);
12052 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012053 if (p_wic)
12054 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012055 if (rettv->v_type == VAR_STRING)
12056 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012057 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012058 else if (rettv_list_alloc(rettv) != FAIL)
12059 {
12060 int i;
12061
12062 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12063 NULL, options, WILD_ALL_KEEP);
12064 for (i = 0; i < xpc.xp_numfiles; i++)
12065 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12066
12067 ExpandCleanup(&xpc);
12068 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012069 }
12070 else
12071 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072}
12073
12074/*
12075 * "globpath()" function
12076 */
12077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012078f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012079 typval_T *argvars;
12080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012082 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012083 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012084 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012085 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012086
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012087 /* When the optional second argument is non-zero, don't remove matches
12088 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12089 if (argvars[2].v_type != VAR_UNKNOWN
12090 && get_tv_number_chk(&argvars[2], &error))
12091 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012092 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012093 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012094 rettv->vval.v_string = NULL;
12095 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012096 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12097 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012098}
12099
12100/*
12101 * "has()" function
12102 */
12103 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012104f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012105 typval_T *argvars;
12106 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012107{
12108 int i;
12109 char_u *name;
12110 int n = FALSE;
12111 static char *(has_list[]) =
12112 {
12113#ifdef AMIGA
12114 "amiga",
12115# ifdef FEAT_ARP
12116 "arp",
12117# endif
12118#endif
12119#ifdef __BEOS__
12120 "beos",
12121#endif
12122#ifdef MSDOS
12123# ifdef DJGPP
12124 "dos32",
12125# else
12126 "dos16",
12127# endif
12128#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012129#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130 "mac",
12131#endif
12132#if defined(MACOS_X_UNIX)
12133 "macunix",
12134#endif
12135#ifdef OS2
12136 "os2",
12137#endif
12138#ifdef __QNX__
12139 "qnx",
12140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141#ifdef UNIX
12142 "unix",
12143#endif
12144#ifdef VMS
12145 "vms",
12146#endif
12147#ifdef WIN16
12148 "win16",
12149#endif
12150#ifdef WIN32
12151 "win32",
12152#endif
12153#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12154 "win32unix",
12155#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012156#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012157 "win64",
12158#endif
12159#ifdef EBCDIC
12160 "ebcdic",
12161#endif
12162#ifndef CASE_INSENSITIVE_FILENAME
12163 "fname_case",
12164#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012165#ifdef HAVE_ACL
12166 "acl",
12167#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168#ifdef FEAT_ARABIC
12169 "arabic",
12170#endif
12171#ifdef FEAT_AUTOCMD
12172 "autocmd",
12173#endif
12174#ifdef FEAT_BEVAL
12175 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012176# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12177 "balloon_multiline",
12178# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012179#endif
12180#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12181 "builtin_terms",
12182# ifdef ALL_BUILTIN_TCAPS
12183 "all_builtin_terms",
12184# endif
12185#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012186#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12187 || defined(FEAT_GUI_W32) \
12188 || defined(FEAT_GUI_MOTIF))
12189 "browsefilter",
12190#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012191#ifdef FEAT_BYTEOFF
12192 "byte_offset",
12193#endif
12194#ifdef FEAT_CINDENT
12195 "cindent",
12196#endif
12197#ifdef FEAT_CLIENTSERVER
12198 "clientserver",
12199#endif
12200#ifdef FEAT_CLIPBOARD
12201 "clipboard",
12202#endif
12203#ifdef FEAT_CMDL_COMPL
12204 "cmdline_compl",
12205#endif
12206#ifdef FEAT_CMDHIST
12207 "cmdline_hist",
12208#endif
12209#ifdef FEAT_COMMENTS
12210 "comments",
12211#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012212#ifdef FEAT_CONCEAL
12213 "conceal",
12214#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215#ifdef FEAT_CRYPT
12216 "cryptv",
12217#endif
12218#ifdef FEAT_CSCOPE
12219 "cscope",
12220#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012221#ifdef FEAT_CURSORBIND
12222 "cursorbind",
12223#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012224#ifdef CURSOR_SHAPE
12225 "cursorshape",
12226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227#ifdef DEBUG
12228 "debug",
12229#endif
12230#ifdef FEAT_CON_DIALOG
12231 "dialog_con",
12232#endif
12233#ifdef FEAT_GUI_DIALOG
12234 "dialog_gui",
12235#endif
12236#ifdef FEAT_DIFF
12237 "diff",
12238#endif
12239#ifdef FEAT_DIGRAPHS
12240 "digraphs",
12241#endif
12242#ifdef FEAT_DND
12243 "dnd",
12244#endif
12245#ifdef FEAT_EMACS_TAGS
12246 "emacs_tags",
12247#endif
12248 "eval", /* always present, of course! */
12249#ifdef FEAT_EX_EXTRA
12250 "ex_extra",
12251#endif
12252#ifdef FEAT_SEARCH_EXTRA
12253 "extra_search",
12254#endif
12255#ifdef FEAT_FKMAP
12256 "farsi",
12257#endif
12258#ifdef FEAT_SEARCHPATH
12259 "file_in_path",
12260#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012261#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012262 "filterpipe",
12263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264#ifdef FEAT_FIND_ID
12265 "find_in_path",
12266#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012267#ifdef FEAT_FLOAT
12268 "float",
12269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012270#ifdef FEAT_FOLDING
12271 "folding",
12272#endif
12273#ifdef FEAT_FOOTER
12274 "footer",
12275#endif
12276#if !defined(USE_SYSTEM) && defined(UNIX)
12277 "fork",
12278#endif
12279#ifdef FEAT_GETTEXT
12280 "gettext",
12281#endif
12282#ifdef FEAT_GUI
12283 "gui",
12284#endif
12285#ifdef FEAT_GUI_ATHENA
12286# ifdef FEAT_GUI_NEXTAW
12287 "gui_neXtaw",
12288# else
12289 "gui_athena",
12290# endif
12291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292#ifdef FEAT_GUI_GTK
12293 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012294 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012296#ifdef FEAT_GUI_GNOME
12297 "gui_gnome",
12298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299#ifdef FEAT_GUI_MAC
12300 "gui_mac",
12301#endif
12302#ifdef FEAT_GUI_MOTIF
12303 "gui_motif",
12304#endif
12305#ifdef FEAT_GUI_PHOTON
12306 "gui_photon",
12307#endif
12308#ifdef FEAT_GUI_W16
12309 "gui_win16",
12310#endif
12311#ifdef FEAT_GUI_W32
12312 "gui_win32",
12313#endif
12314#ifdef FEAT_HANGULIN
12315 "hangul_input",
12316#endif
12317#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12318 "iconv",
12319#endif
12320#ifdef FEAT_INS_EXPAND
12321 "insert_expand",
12322#endif
12323#ifdef FEAT_JUMPLIST
12324 "jumplist",
12325#endif
12326#ifdef FEAT_KEYMAP
12327 "keymap",
12328#endif
12329#ifdef FEAT_LANGMAP
12330 "langmap",
12331#endif
12332#ifdef FEAT_LIBCALL
12333 "libcall",
12334#endif
12335#ifdef FEAT_LINEBREAK
12336 "linebreak",
12337#endif
12338#ifdef FEAT_LISP
12339 "lispindent",
12340#endif
12341#ifdef FEAT_LISTCMDS
12342 "listcmds",
12343#endif
12344#ifdef FEAT_LOCALMAP
12345 "localmap",
12346#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012347#ifdef FEAT_LUA
12348# ifndef DYNAMIC_LUA
12349 "lua",
12350# endif
12351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012352#ifdef FEAT_MENU
12353 "menu",
12354#endif
12355#ifdef FEAT_SESSION
12356 "mksession",
12357#endif
12358#ifdef FEAT_MODIFY_FNAME
12359 "modify_fname",
12360#endif
12361#ifdef FEAT_MOUSE
12362 "mouse",
12363#endif
12364#ifdef FEAT_MOUSESHAPE
12365 "mouseshape",
12366#endif
12367#if defined(UNIX) || defined(VMS)
12368# ifdef FEAT_MOUSE_DEC
12369 "mouse_dec",
12370# endif
12371# ifdef FEAT_MOUSE_GPM
12372 "mouse_gpm",
12373# endif
12374# ifdef FEAT_MOUSE_JSB
12375 "mouse_jsbterm",
12376# endif
12377# ifdef FEAT_MOUSE_NET
12378 "mouse_netterm",
12379# endif
12380# ifdef FEAT_MOUSE_PTERM
12381 "mouse_pterm",
12382# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012383# ifdef FEAT_MOUSE_SGR
12384 "mouse_sgr",
12385# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012386# ifdef FEAT_SYSMOUSE
12387 "mouse_sysmouse",
12388# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012389# ifdef FEAT_MOUSE_URXVT
12390 "mouse_urxvt",
12391# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012392# ifdef FEAT_MOUSE_XTERM
12393 "mouse_xterm",
12394# endif
12395#endif
12396#ifdef FEAT_MBYTE
12397 "multi_byte",
12398#endif
12399#ifdef FEAT_MBYTE_IME
12400 "multi_byte_ime",
12401#endif
12402#ifdef FEAT_MULTI_LANG
12403 "multi_lang",
12404#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012405#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012406#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012407 "mzscheme",
12408#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012409#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012410#ifdef FEAT_OLE
12411 "ole",
12412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012413#ifdef FEAT_PATH_EXTRA
12414 "path_extra",
12415#endif
12416#ifdef FEAT_PERL
12417#ifndef DYNAMIC_PERL
12418 "perl",
12419#endif
12420#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012421#ifdef FEAT_PERSISTENT_UNDO
12422 "persistent_undo",
12423#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424#ifdef FEAT_PYTHON
12425#ifndef DYNAMIC_PYTHON
12426 "python",
12427#endif
12428#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012429#ifdef FEAT_PYTHON3
12430#ifndef DYNAMIC_PYTHON3
12431 "python3",
12432#endif
12433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012434#ifdef FEAT_POSTSCRIPT
12435 "postscript",
12436#endif
12437#ifdef FEAT_PRINTER
12438 "printer",
12439#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012440#ifdef FEAT_PROFILE
12441 "profile",
12442#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012443#ifdef FEAT_RELTIME
12444 "reltime",
12445#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012446#ifdef FEAT_QUICKFIX
12447 "quickfix",
12448#endif
12449#ifdef FEAT_RIGHTLEFT
12450 "rightleft",
12451#endif
12452#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12453 "ruby",
12454#endif
12455#ifdef FEAT_SCROLLBIND
12456 "scrollbind",
12457#endif
12458#ifdef FEAT_CMDL_INFO
12459 "showcmd",
12460 "cmdline_info",
12461#endif
12462#ifdef FEAT_SIGNS
12463 "signs",
12464#endif
12465#ifdef FEAT_SMARTINDENT
12466 "smartindent",
12467#endif
12468#ifdef FEAT_SNIFF
12469 "sniff",
12470#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012471#ifdef STARTUPTIME
12472 "startuptime",
12473#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012474#ifdef FEAT_STL_OPT
12475 "statusline",
12476#endif
12477#ifdef FEAT_SUN_WORKSHOP
12478 "sun_workshop",
12479#endif
12480#ifdef FEAT_NETBEANS_INTG
12481 "netbeans_intg",
12482#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012483#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012484 "spell",
12485#endif
12486#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487 "syntax",
12488#endif
12489#if defined(USE_SYSTEM) || !defined(UNIX)
12490 "system",
12491#endif
12492#ifdef FEAT_TAG_BINS
12493 "tag_binary",
12494#endif
12495#ifdef FEAT_TAG_OLDSTATIC
12496 "tag_old_static",
12497#endif
12498#ifdef FEAT_TAG_ANYWHITE
12499 "tag_any_white",
12500#endif
12501#ifdef FEAT_TCL
12502# ifndef DYNAMIC_TCL
12503 "tcl",
12504# endif
12505#endif
12506#ifdef TERMINFO
12507 "terminfo",
12508#endif
12509#ifdef FEAT_TERMRESPONSE
12510 "termresponse",
12511#endif
12512#ifdef FEAT_TEXTOBJ
12513 "textobjects",
12514#endif
12515#ifdef HAVE_TGETENT
12516 "tgetent",
12517#endif
12518#ifdef FEAT_TITLE
12519 "title",
12520#endif
12521#ifdef FEAT_TOOLBAR
12522 "toolbar",
12523#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012524#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12525 "unnamedplus",
12526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527#ifdef FEAT_USR_CMDS
12528 "user-commands", /* was accidentally included in 5.4 */
12529 "user_commands",
12530#endif
12531#ifdef FEAT_VIMINFO
12532 "viminfo",
12533#endif
12534#ifdef FEAT_VERTSPLIT
12535 "vertsplit",
12536#endif
12537#ifdef FEAT_VIRTUALEDIT
12538 "virtualedit",
12539#endif
12540#ifdef FEAT_VISUAL
12541 "visual",
12542#endif
12543#ifdef FEAT_VISUALEXTRA
12544 "visualextra",
12545#endif
12546#ifdef FEAT_VREPLACE
12547 "vreplace",
12548#endif
12549#ifdef FEAT_WILDIGN
12550 "wildignore",
12551#endif
12552#ifdef FEAT_WILDMENU
12553 "wildmenu",
12554#endif
12555#ifdef FEAT_WINDOWS
12556 "windows",
12557#endif
12558#ifdef FEAT_WAK
12559 "winaltkeys",
12560#endif
12561#ifdef FEAT_WRITEBACKUP
12562 "writebackup",
12563#endif
12564#ifdef FEAT_XIM
12565 "xim",
12566#endif
12567#ifdef FEAT_XFONTSET
12568 "xfontset",
12569#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012570#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012571 "xpm",
12572 "xpm_w32", /* for backward compatibility */
12573#else
12574# if defined(HAVE_XPM)
12575 "xpm",
12576# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012577#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012578#ifdef USE_XSMP
12579 "xsmp",
12580#endif
12581#ifdef USE_XSMP_INTERACT
12582 "xsmp_interact",
12583#endif
12584#ifdef FEAT_XCLIPBOARD
12585 "xterm_clipboard",
12586#endif
12587#ifdef FEAT_XTERM_SAVE
12588 "xterm_save",
12589#endif
12590#if defined(UNIX) && defined(FEAT_X11)
12591 "X11",
12592#endif
12593 NULL
12594 };
12595
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012596 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597 for (i = 0; has_list[i] != NULL; ++i)
12598 if (STRICMP(name, has_list[i]) == 0)
12599 {
12600 n = TRUE;
12601 break;
12602 }
12603
12604 if (n == FALSE)
12605 {
12606 if (STRNICMP(name, "patch", 5) == 0)
12607 n = has_patch(atoi((char *)name + 5));
12608 else if (STRICMP(name, "vim_starting") == 0)
12609 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012610#ifdef FEAT_MBYTE
12611 else if (STRICMP(name, "multi_byte_encoding") == 0)
12612 n = has_mbyte;
12613#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012614#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12615 else if (STRICMP(name, "balloon_multiline") == 0)
12616 n = multiline_balloon_available();
12617#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618#ifdef DYNAMIC_TCL
12619 else if (STRICMP(name, "tcl") == 0)
12620 n = tcl_enabled(FALSE);
12621#endif
12622#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12623 else if (STRICMP(name, "iconv") == 0)
12624 n = iconv_enabled(FALSE);
12625#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012626#ifdef DYNAMIC_LUA
12627 else if (STRICMP(name, "lua") == 0)
12628 n = lua_enabled(FALSE);
12629#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012630#ifdef DYNAMIC_MZSCHEME
12631 else if (STRICMP(name, "mzscheme") == 0)
12632 n = mzscheme_enabled(FALSE);
12633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634#ifdef DYNAMIC_RUBY
12635 else if (STRICMP(name, "ruby") == 0)
12636 n = ruby_enabled(FALSE);
12637#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012638#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012639#ifdef DYNAMIC_PYTHON
12640 else if (STRICMP(name, "python") == 0)
12641 n = python_enabled(FALSE);
12642#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012643#endif
12644#ifdef FEAT_PYTHON3
12645#ifdef DYNAMIC_PYTHON3
12646 else if (STRICMP(name, "python3") == 0)
12647 n = python3_enabled(FALSE);
12648#endif
12649#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012650#ifdef DYNAMIC_PERL
12651 else if (STRICMP(name, "perl") == 0)
12652 n = perl_enabled(FALSE);
12653#endif
12654#ifdef FEAT_GUI
12655 else if (STRICMP(name, "gui_running") == 0)
12656 n = (gui.in_use || gui.starting);
12657# ifdef FEAT_GUI_W32
12658 else if (STRICMP(name, "gui_win32s") == 0)
12659 n = gui_is_win32s();
12660# endif
12661# ifdef FEAT_BROWSE
12662 else if (STRICMP(name, "browse") == 0)
12663 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12664# endif
12665#endif
12666#ifdef FEAT_SYN_HL
12667 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012668 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012669#endif
12670#if defined(WIN3264)
12671 else if (STRICMP(name, "win95") == 0)
12672 n = mch_windows95();
12673#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012674#ifdef FEAT_NETBEANS_INTG
12675 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012676 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012678 }
12679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012680 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012681}
12682
12683/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012684 * "has_key()" function
12685 */
12686 static void
12687f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012688 typval_T *argvars;
12689 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012690{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012691 if (argvars[0].v_type != VAR_DICT)
12692 {
12693 EMSG(_(e_dictreq));
12694 return;
12695 }
12696 if (argvars[0].vval.v_dict == NULL)
12697 return;
12698
12699 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012700 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012701}
12702
12703/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012704 * "haslocaldir()" function
12705 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012706 static void
12707f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012708 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012709 typval_T *rettv;
12710{
12711 rettv->vval.v_number = (curwin->w_localdir != NULL);
12712}
12713
12714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715 * "hasmapto()" function
12716 */
12717 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012718f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012719 typval_T *argvars;
12720 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012721{
12722 char_u *name;
12723 char_u *mode;
12724 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012725 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012726
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012727 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012728 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012729 mode = (char_u *)"nvo";
12730 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012731 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012732 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012733 if (argvars[2].v_type != VAR_UNKNOWN)
12734 abbr = get_tv_number(&argvars[2]);
12735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736
Bram Moolenaar2c932302006-03-18 21:42:09 +000012737 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012738 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012740 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741}
12742
12743/*
12744 * "histadd()" function
12745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012747f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012748 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012749 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750{
12751#ifdef FEAT_CMDHIST
12752 int histype;
12753 char_u *str;
12754 char_u buf[NUMBUFLEN];
12755#endif
12756
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012757 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758 if (check_restricted() || check_secure())
12759 return;
12760#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012761 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12762 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763 if (histype >= 0)
12764 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012765 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012766 if (*str != NUL)
12767 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012768 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012769 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012770 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771 return;
12772 }
12773 }
12774#endif
12775}
12776
12777/*
12778 * "histdel()" function
12779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012781f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012782 typval_T *argvars UNUSED;
12783 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784{
12785#ifdef FEAT_CMDHIST
12786 int n;
12787 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012788 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012790 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12791 if (str == NULL)
12792 n = 0;
12793 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012795 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012796 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012798 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012799 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012800 else
12801 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012802 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012803 get_tv_string_buf(&argvars[1], buf));
12804 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012805#endif
12806}
12807
12808/*
12809 * "histget()" function
12810 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012812f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012813 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012814 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012815{
12816#ifdef FEAT_CMDHIST
12817 int type;
12818 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012819 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012821 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12822 if (str == NULL)
12823 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012824 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012825 {
12826 type = get_histtype(str);
12827 if (argvars[1].v_type == VAR_UNKNOWN)
12828 idx = get_history_idx(type);
12829 else
12830 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12831 /* -1 on type error */
12832 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012837 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012838}
12839
12840/*
12841 * "histnr()" function
12842 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012845 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012846 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012847{
12848 int i;
12849
12850#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012851 char_u *history = get_tv_string_chk(&argvars[0]);
12852
12853 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012854 if (i >= HIST_CMD && i < HIST_COUNT)
12855 i = get_history_idx(i);
12856 else
12857#endif
12858 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860}
12861
12862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863 * "highlightID(name)" function
12864 */
12865 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012866f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012867 typval_T *argvars;
12868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012869{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012870 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871}
12872
12873/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012874 * "highlight_exists()" function
12875 */
12876 static void
12877f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012878 typval_T *argvars;
12879 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012880{
12881 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12882}
12883
12884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012885 * "hostname()" function
12886 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012889 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012890 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891{
12892 char_u hostname[256];
12893
12894 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012895 rettv->v_type = VAR_STRING;
12896 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897}
12898
12899/*
12900 * iconv() function
12901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012903f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012904 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012905 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012906{
12907#ifdef FEAT_MBYTE
12908 char_u buf1[NUMBUFLEN];
12909 char_u buf2[NUMBUFLEN];
12910 char_u *from, *to, *str;
12911 vimconv_T vimconv;
12912#endif
12913
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914 rettv->v_type = VAR_STRING;
12915 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916
12917#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012918 str = get_tv_string(&argvars[0]);
12919 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12920 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921 vimconv.vc_type = CONV_NONE;
12922 convert_setup(&vimconv, from, to);
12923
12924 /* If the encodings are equal, no conversion needed. */
12925 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012926 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012927 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012929
12930 convert_setup(&vimconv, NULL, NULL);
12931 vim_free(from);
12932 vim_free(to);
12933#endif
12934}
12935
12936/*
12937 * "indent()" function
12938 */
12939 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012940f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012941 typval_T *argvars;
12942 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012943{
12944 linenr_T lnum;
12945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012950 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012951}
12952
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012953/*
12954 * "index()" function
12955 */
12956 static void
12957f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012958 typval_T *argvars;
12959 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012960{
Bram Moolenaar33570922005-01-25 22:26:29 +000012961 list_T *l;
12962 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012963 long idx = 0;
12964 int ic = FALSE;
12965
12966 rettv->vval.v_number = -1;
12967 if (argvars[0].v_type != VAR_LIST)
12968 {
12969 EMSG(_(e_listreq));
12970 return;
12971 }
12972 l = argvars[0].vval.v_list;
12973 if (l != NULL)
12974 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012975 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012976 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012977 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012978 int error = FALSE;
12979
Bram Moolenaar758711c2005-02-02 23:11:38 +000012980 /* Start at specified item. Use the cached index that list_find()
12981 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012982 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012983 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012984 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012985 ic = get_tv_number_chk(&argvars[3], &error);
12986 if (error)
12987 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012988 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012989
Bram Moolenaar758711c2005-02-02 23:11:38 +000012990 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012991 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012992 {
12993 rettv->vval.v_number = idx;
12994 break;
12995 }
12996 }
12997}
12998
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999static int inputsecret_flag = 0;
13000
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013001static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
13002
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013004 * This function is used by f_input() and f_inputdialog() functions. The third
13005 * argument to f_input() specifies the type of completion to use at the
13006 * prompt. The third argument to f_inputdialog() specifies the value to return
13007 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000013008 */
13009 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013010get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000013011 typval_T *argvars;
13012 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013013 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013014{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013015 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013016 char_u *p = NULL;
13017 int c;
13018 char_u buf[NUMBUFLEN];
13019 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013020 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013021 int xp_type = EXPAND_NOTHING;
13022 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013023
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013024 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000013025 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013026
13027#ifdef NO_CONSOLE_INPUT
13028 /* While starting up, there is no place to enter text. */
13029 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013030 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013031#endif
13032
13033 cmd_silent = FALSE; /* Want to see the prompt. */
13034 if (prompt != NULL)
13035 {
13036 /* Only the part of the message after the last NL is considered as
13037 * prompt for the command line */
13038 p = vim_strrchr(prompt, '\n');
13039 if (p == NULL)
13040 p = prompt;
13041 else
13042 {
13043 ++p;
13044 c = *p;
13045 *p = NUL;
13046 msg_start();
13047 msg_clr_eos();
13048 msg_puts_attr(prompt, echo_attr);
13049 msg_didout = FALSE;
13050 msg_starthere();
13051 *p = c;
13052 }
13053 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013055 if (argvars[1].v_type != VAR_UNKNOWN)
13056 {
13057 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13058 if (defstr != NULL)
13059 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013060
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013061 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013062 {
13063 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013064 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013065 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013066
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013067 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013068 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013069
Bram Moolenaar4463f292005-09-25 22:20:24 +000013070 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13071 if (xp_name == NULL)
13072 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013073
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013074 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013075
Bram Moolenaar4463f292005-09-25 22:20:24 +000013076 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13077 &xp_arg) == FAIL)
13078 return;
13079 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013080 }
13081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013082 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013083 {
13084# ifdef FEAT_EX_EXTRA
13085 int save_ex_normal_busy = ex_normal_busy;
13086 ex_normal_busy = 0;
13087# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013088 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013089 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13090 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020013091# ifdef FEAT_EX_EXTRA
13092 ex_normal_busy = save_ex_normal_busy;
13093# endif
13094 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020013095 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013096 && argvars[1].v_type != VAR_UNKNOWN
13097 && argvars[2].v_type != VAR_UNKNOWN)
13098 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13099 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013100
13101 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013103 /* since the user typed this, no need to wait for return */
13104 need_wait_return = FALSE;
13105 msg_didout = FALSE;
13106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 cmd_silent = cmd_silent_save;
13108}
13109
13110/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013111 * "input()" function
13112 * Also handles inputsecret() when inputsecret is set.
13113 */
13114 static void
13115f_input(argvars, rettv)
13116 typval_T *argvars;
13117 typval_T *rettv;
13118{
13119 get_user_input(argvars, rettv, FALSE);
13120}
13121
13122/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123 * "inputdialog()" function
13124 */
13125 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013126f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013127 typval_T *argvars;
13128 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013129{
13130#if defined(FEAT_GUI_TEXTDIALOG)
13131 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13132 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13133 {
13134 char_u *message;
13135 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013136 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013137
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013138 message = get_tv_string_chk(&argvars[0]);
13139 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013140 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013141 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142 else
13143 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013144 if (message != NULL && defstr != NULL
13145 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013146 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013147 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148 else
13149 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013150 if (message != NULL && defstr != NULL
13151 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013152 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013153 rettv->vval.v_string = vim_strsave(
13154 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013155 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013156 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013158 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013159 }
13160 else
13161#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013162 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163}
13164
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013165/*
13166 * "inputlist()" function
13167 */
13168 static void
13169f_inputlist(argvars, rettv)
13170 typval_T *argvars;
13171 typval_T *rettv;
13172{
13173 listitem_T *li;
13174 int selected;
13175 int mouse_used;
13176
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013177#ifdef NO_CONSOLE_INPUT
13178 /* While starting up, there is no place to enter text. */
13179 if (no_console_input())
13180 return;
13181#endif
13182 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13183 {
13184 EMSG2(_(e_listarg), "inputlist()");
13185 return;
13186 }
13187
13188 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013189 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013190 lines_left = Rows; /* avoid more prompt */
13191 msg_scroll = TRUE;
13192 msg_clr_eos();
13193
13194 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13195 {
13196 msg_puts(get_tv_string(&li->li_tv));
13197 msg_putchar('\n');
13198 }
13199
13200 /* Ask for choice. */
13201 selected = prompt_for_number(&mouse_used);
13202 if (mouse_used)
13203 selected -= lines_left;
13204
13205 rettv->vval.v_number = selected;
13206}
13207
13208
Bram Moolenaar071d4272004-06-13 20:20:40 +000013209static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13210
13211/*
13212 * "inputrestore()" function
13213 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013215f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013216 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013217 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218{
13219 if (ga_userinput.ga_len > 0)
13220 {
13221 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13223 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013224 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225 }
13226 else if (p_verbose > 1)
13227 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013228 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013229 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 }
13231}
13232
13233/*
13234 * "inputsave()" function
13235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013237f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013238 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013239 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013240{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013241 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013242 if (ga_grow(&ga_userinput, 1) == OK)
13243 {
13244 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13245 + ga_userinput.ga_len);
13246 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013247 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248 }
13249 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013250 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013251}
13252
13253/*
13254 * "inputsecret()" function
13255 */
13256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013257f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013258 typval_T *argvars;
13259 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013260{
13261 ++cmdline_star;
13262 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013263 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013264 --cmdline_star;
13265 --inputsecret_flag;
13266}
13267
13268/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013269 * "insert()" function
13270 */
13271 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013272f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013273 typval_T *argvars;
13274 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013275{
13276 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013277 listitem_T *item;
13278 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013279 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013280
13281 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013282 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013283 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013284 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013285 {
13286 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013287 before = get_tv_number_chk(&argvars[2], &error);
13288 if (error)
13289 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013290
Bram Moolenaar758711c2005-02-02 23:11:38 +000013291 if (before == l->lv_len)
13292 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013293 else
13294 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013295 item = list_find(l, before);
13296 if (item == NULL)
13297 {
13298 EMSGN(_(e_listidx), before);
13299 l = NULL;
13300 }
13301 }
13302 if (l != NULL)
13303 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013304 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013305 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013306 }
13307 }
13308}
13309
13310/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013311 * "invert(expr)" function
13312 */
13313 static void
13314f_invert(argvars, rettv)
13315 typval_T *argvars;
13316 typval_T *rettv;
13317{
13318 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13319}
13320
13321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322 * "isdirectory()" function
13323 */
13324 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013325f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013326 typval_T *argvars;
13327 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013328{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013329 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013330}
13331
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013332/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013333 * "islocked()" function
13334 */
13335 static void
13336f_islocked(argvars, rettv)
13337 typval_T *argvars;
13338 typval_T *rettv;
13339{
13340 lval_T lv;
13341 char_u *end;
13342 dictitem_T *di;
13343
13344 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013345 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13346 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013347 if (end != NULL && lv.ll_name != NULL)
13348 {
13349 if (*end != NUL)
13350 EMSG(_(e_trailing));
13351 else
13352 {
13353 if (lv.ll_tv == NULL)
13354 {
13355 if (check_changedtick(lv.ll_name))
13356 rettv->vval.v_number = 1; /* always locked */
13357 else
13358 {
13359 di = find_var(lv.ll_name, NULL);
13360 if (di != NULL)
13361 {
13362 /* Consider a variable locked when:
13363 * 1. the variable itself is locked
13364 * 2. the value of the variable is locked.
13365 * 3. the List or Dict value is locked.
13366 */
13367 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13368 || tv_islocked(&di->di_tv));
13369 }
13370 }
13371 }
13372 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013373 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013374 else if (lv.ll_newkey != NULL)
13375 EMSG2(_(e_dictkey), lv.ll_newkey);
13376 else if (lv.ll_list != NULL)
13377 /* List item. */
13378 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13379 else
13380 /* Dictionary item. */
13381 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13382 }
13383 }
13384
13385 clear_lval(&lv);
13386}
13387
Bram Moolenaar33570922005-01-25 22:26:29 +000013388static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013389
13390/*
13391 * Turn a dict into a list:
13392 * "what" == 0: list of keys
13393 * "what" == 1: list of values
13394 * "what" == 2: list of items
13395 */
13396 static void
13397dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013398 typval_T *argvars;
13399 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013400 int what;
13401{
Bram Moolenaar33570922005-01-25 22:26:29 +000013402 list_T *l2;
13403 dictitem_T *di;
13404 hashitem_T *hi;
13405 listitem_T *li;
13406 listitem_T *li2;
13407 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013408 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013409
Bram Moolenaar8c711452005-01-14 21:53:12 +000013410 if (argvars[0].v_type != VAR_DICT)
13411 {
13412 EMSG(_(e_dictreq));
13413 return;
13414 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013415 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013416 return;
13417
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013418 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013419 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013420
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013421 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013422 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013423 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013424 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013425 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013426 --todo;
13427 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013428
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013429 li = listitem_alloc();
13430 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013431 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013432 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013433
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013434 if (what == 0)
13435 {
13436 /* keys() */
13437 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013438 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013439 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13440 }
13441 else if (what == 1)
13442 {
13443 /* values() */
13444 copy_tv(&di->di_tv, &li->li_tv);
13445 }
13446 else
13447 {
13448 /* items() */
13449 l2 = list_alloc();
13450 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013451 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013452 li->li_tv.vval.v_list = l2;
13453 if (l2 == NULL)
13454 break;
13455 ++l2->lv_refcount;
13456
13457 li2 = listitem_alloc();
13458 if (li2 == NULL)
13459 break;
13460 list_append(l2, li2);
13461 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013462 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013463 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13464
13465 li2 = listitem_alloc();
13466 if (li2 == NULL)
13467 break;
13468 list_append(l2, li2);
13469 copy_tv(&di->di_tv, &li2->li_tv);
13470 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013471 }
13472 }
13473}
13474
13475/*
13476 * "items(dict)" function
13477 */
13478 static void
13479f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013480 typval_T *argvars;
13481 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013482{
13483 dict_list(argvars, rettv, 2);
13484}
13485
Bram Moolenaar071d4272004-06-13 20:20:40 +000013486/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013487 * "join()" function
13488 */
13489 static void
13490f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013491 typval_T *argvars;
13492 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013493{
13494 garray_T ga;
13495 char_u *sep;
13496
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013497 if (argvars[0].v_type != VAR_LIST)
13498 {
13499 EMSG(_(e_listreq));
13500 return;
13501 }
13502 if (argvars[0].vval.v_list == NULL)
13503 return;
13504 if (argvars[1].v_type == VAR_UNKNOWN)
13505 sep = (char_u *)" ";
13506 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013507 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013508
13509 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013510
13511 if (sep != NULL)
13512 {
13513 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013514 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013515 ga_append(&ga, NUL);
13516 rettv->vval.v_string = (char_u *)ga.ga_data;
13517 }
13518 else
13519 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013520}
13521
13522/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013523 * "keys()" function
13524 */
13525 static void
13526f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013527 typval_T *argvars;
13528 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013529{
13530 dict_list(argvars, rettv, 0);
13531}
13532
13533/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534 * "last_buffer_nr()" function.
13535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013537f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013538 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013539 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540{
13541 int n = 0;
13542 buf_T *buf;
13543
13544 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13545 if (n < buf->b_fnum)
13546 n = buf->b_fnum;
13547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013548 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013549}
13550
13551/*
13552 * "len()" function
13553 */
13554 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013555f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013556 typval_T *argvars;
13557 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013558{
13559 switch (argvars[0].v_type)
13560 {
13561 case VAR_STRING:
13562 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013563 rettv->vval.v_number = (varnumber_T)STRLEN(
13564 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013565 break;
13566 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013567 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013568 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013569 case VAR_DICT:
13570 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13571 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013572 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013573 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013574 break;
13575 }
13576}
13577
Bram Moolenaar33570922005-01-25 22:26:29 +000013578static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013579
13580 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013581libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013582 typval_T *argvars;
13583 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013584 int type;
13585{
13586#ifdef FEAT_LIBCALL
13587 char_u *string_in;
13588 char_u **string_result;
13589 int nr_result;
13590#endif
13591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013592 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013593 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013594 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013595
13596 if (check_restricted() || check_secure())
13597 return;
13598
13599#ifdef FEAT_LIBCALL
13600 /* The first two args must be strings, otherwise its meaningless */
13601 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13602 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013603 string_in = NULL;
13604 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013605 string_in = argvars[2].vval.v_string;
13606 if (type == VAR_NUMBER)
13607 string_result = NULL;
13608 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013609 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013610 if (mch_libcall(argvars[0].vval.v_string,
13611 argvars[1].vval.v_string,
13612 string_in,
13613 argvars[2].vval.v_number,
13614 string_result,
13615 &nr_result) == OK
13616 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013617 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013618 }
13619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620}
13621
13622/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013623 * "libcall()" function
13624 */
13625 static void
13626f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013627 typval_T *argvars;
13628 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013629{
13630 libcall_common(argvars, rettv, VAR_STRING);
13631}
13632
13633/*
13634 * "libcallnr()" function
13635 */
13636 static void
13637f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013638 typval_T *argvars;
13639 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013640{
13641 libcall_common(argvars, rettv, VAR_NUMBER);
13642}
13643
13644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645 * "line(string)" function
13646 */
13647 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013648f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013649 typval_T *argvars;
13650 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651{
13652 linenr_T lnum = 0;
13653 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013654 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013656 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657 if (fp != NULL)
13658 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013659 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660}
13661
13662/*
13663 * "line2byte(lnum)" function
13664 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013665 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013666f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013667 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013668 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669{
13670#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672#else
13673 linenr_T lnum;
13674
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013675 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013677 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013679 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13680 if (rettv->vval.v_number >= 0)
13681 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682#endif
13683}
13684
13685/*
13686 * "lispindent(lnum)" function
13687 */
13688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013689f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013690 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692{
13693#ifdef FEAT_LISP
13694 pos_T pos;
13695 linenr_T lnum;
13696
13697 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013698 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13700 {
13701 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013702 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703 curwin->w_cursor = pos;
13704 }
13705 else
13706#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013707 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708}
13709
13710/*
13711 * "localtime()" function
13712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013714f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013715 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013717{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013718 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719}
13720
Bram Moolenaar33570922005-01-25 22:26:29 +000013721static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722
13723 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013724get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013725 typval_T *argvars;
13726 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 int exact;
13728{
13729 char_u *keys;
13730 char_u *which;
13731 char_u buf[NUMBUFLEN];
13732 char_u *keys_buf = NULL;
13733 char_u *rhs;
13734 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013735 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013736 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013737 mapblock_T *mp;
13738 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739
13740 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013741 rettv->v_type = VAR_STRING;
13742 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013743
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013744 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013745 if (*keys == NUL)
13746 return;
13747
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013748 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013749 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013750 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013751 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013752 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013753 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013754 if (argvars[3].v_type != VAR_UNKNOWN)
13755 get_dict = get_tv_number(&argvars[3]);
13756 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758 else
13759 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013760 if (which == NULL)
13761 return;
13762
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763 mode = get_map_mode(&which, 0);
13764
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013765 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013766 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013767 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013768
13769 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013771 /* Return a string. */
13772 if (rhs != NULL)
13773 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013774
Bram Moolenaarbd743252010-10-20 21:23:33 +020013775 }
13776 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13777 {
13778 /* Return a dictionary. */
13779 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13780 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13781 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013782
Bram Moolenaarbd743252010-10-20 21:23:33 +020013783 dict_add_nr_str(dict, "lhs", 0L, lhs);
13784 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13785 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13786 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13787 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13788 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13789 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013790 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013791 dict_add_nr_str(dict, "mode", 0L, mapmode);
13792
13793 vim_free(lhs);
13794 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013795 }
13796}
13797
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013798#ifdef FEAT_FLOAT
13799/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013800 * "log()" function
13801 */
13802 static void
13803f_log(argvars, rettv)
13804 typval_T *argvars;
13805 typval_T *rettv;
13806{
13807 float_T f;
13808
13809 rettv->v_type = VAR_FLOAT;
13810 if (get_float_arg(argvars, &f) == OK)
13811 rettv->vval.v_float = log(f);
13812 else
13813 rettv->vval.v_float = 0.0;
13814}
13815
13816/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013817 * "log10()" function
13818 */
13819 static void
13820f_log10(argvars, rettv)
13821 typval_T *argvars;
13822 typval_T *rettv;
13823{
13824 float_T f;
13825
13826 rettv->v_type = VAR_FLOAT;
13827 if (get_float_arg(argvars, &f) == OK)
13828 rettv->vval.v_float = log10(f);
13829 else
13830 rettv->vval.v_float = 0.0;
13831}
13832#endif
13833
Bram Moolenaar1dced572012-04-05 16:54:08 +020013834#ifdef FEAT_LUA
13835/*
13836 * "luaeval()" function
13837 */
13838 static void
13839f_luaeval(argvars, rettv)
13840 typval_T *argvars;
13841 typval_T *rettv;
13842{
13843 char_u *str;
13844 char_u buf[NUMBUFLEN];
13845
13846 str = get_tv_string_buf(&argvars[0], buf);
13847 do_luaeval(str, argvars + 1, rettv);
13848}
13849#endif
13850
Bram Moolenaar071d4272004-06-13 20:20:40 +000013851/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013852 * "map()" function
13853 */
13854 static void
13855f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013856 typval_T *argvars;
13857 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013858{
13859 filter_map(argvars, rettv, TRUE);
13860}
13861
13862/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013863 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013864 */
13865 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013866f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013867 typval_T *argvars;
13868 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013870 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871}
13872
13873/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013874 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 */
13876 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013877f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013878 typval_T *argvars;
13879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013881 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013882}
13883
Bram Moolenaar33570922005-01-25 22:26:29 +000013884static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885
13886 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013887find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013888 typval_T *argvars;
13889 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890 int type;
13891{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013892 char_u *str = NULL;
13893 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013894 char_u *pat;
13895 regmatch_T regmatch;
13896 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013897 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898 char_u *save_cpo;
13899 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013900 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013901 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013902 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013903 list_T *l = NULL;
13904 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013905 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013906 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013907
13908 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13909 save_cpo = p_cpo;
13910 p_cpo = (char_u *)"";
13911
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013912 rettv->vval.v_number = -1;
13913 if (type == 3)
13914 {
13915 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013916 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013917 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013918 }
13919 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013921 rettv->v_type = VAR_STRING;
13922 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013924
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013925 if (argvars[0].v_type == VAR_LIST)
13926 {
13927 if ((l = argvars[0].vval.v_list) == NULL)
13928 goto theend;
13929 li = l->lv_first;
13930 }
13931 else
13932 expr = str = get_tv_string(&argvars[0]);
13933
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013934 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13935 if (pat == NULL)
13936 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013937
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013938 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013940 int error = FALSE;
13941
13942 start = get_tv_number_chk(&argvars[2], &error);
13943 if (error)
13944 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013945 if (l != NULL)
13946 {
13947 li = list_find(l, start);
13948 if (li == NULL)
13949 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013950 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013951 }
13952 else
13953 {
13954 if (start < 0)
13955 start = 0;
13956 if (start > (long)STRLEN(str))
13957 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013958 /* When "count" argument is there ignore matches before "start",
13959 * otherwise skip part of the string. Differs when pattern is "^"
13960 * or "\<". */
13961 if (argvars[3].v_type != VAR_UNKNOWN)
13962 startcol = start;
13963 else
13964 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013965 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013966
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013967 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013968 nth = get_tv_number_chk(&argvars[3], &error);
13969 if (error)
13970 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971 }
13972
13973 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13974 if (regmatch.regprog != NULL)
13975 {
13976 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013977
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013978 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013979 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013980 if (l != NULL)
13981 {
13982 if (li == NULL)
13983 {
13984 match = FALSE;
13985 break;
13986 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013987 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013988 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013989 if (str == NULL)
13990 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013991 }
13992
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013993 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013994
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013995 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013996 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013997 if (l == NULL && !match)
13998 break;
13999
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014000 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014001 if (l != NULL)
14002 {
14003 li = li->li_next;
14004 ++idx;
14005 }
14006 else
14007 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014008#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014009 startcol = (colnr_T)(regmatch.startp[0]
14010 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014011#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020014012 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014013#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014014 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000014015 }
14016
14017 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014019 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014020 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014021 int i;
14022
14023 /* return list with matched string and submatches */
14024 for (i = 0; i < NSUBEXP; ++i)
14025 {
14026 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000014027 {
14028 if (list_append_string(rettv->vval.v_list,
14029 (char_u *)"", 0) == FAIL)
14030 break;
14031 }
14032 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000014033 regmatch.startp[i],
14034 (int)(regmatch.endp[i] - regmatch.startp[i]))
14035 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014036 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014037 }
14038 }
14039 else if (type == 2)
14040 {
14041 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014042 if (l != NULL)
14043 copy_tv(&li->li_tv, rettv);
14044 else
14045 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014047 }
14048 else if (l != NULL)
14049 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050 else
14051 {
14052 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014053 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 (varnumber_T)(regmatch.startp[0] - str);
14055 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014056 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014058 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 }
14060 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014061 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062 }
14063
14064theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014065 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014066 p_cpo = save_cpo;
14067}
14068
14069/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014070 * "match()" function
14071 */
14072 static void
14073f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014074 typval_T *argvars;
14075 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014076{
14077 find_some_match(argvars, rettv, 1);
14078}
14079
14080/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014081 * "matchadd()" function
14082 */
14083 static void
14084f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014085 typval_T *argvars UNUSED;
14086 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014087{
14088#ifdef FEAT_SEARCH_EXTRA
14089 char_u buf[NUMBUFLEN];
14090 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14091 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14092 int prio = 10; /* default priority */
14093 int id = -1;
14094 int error = FALSE;
14095
14096 rettv->vval.v_number = -1;
14097
14098 if (grp == NULL || pat == NULL)
14099 return;
14100 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014101 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014102 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014103 if (argvars[3].v_type != VAR_UNKNOWN)
14104 id = get_tv_number_chk(&argvars[3], &error);
14105 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014106 if (error == TRUE)
14107 return;
14108 if (id >= 1 && id <= 3)
14109 {
14110 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14111 return;
14112 }
14113
14114 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14115#endif
14116}
14117
14118/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014119 * "matcharg()" function
14120 */
14121 static void
14122f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014123 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014124 typval_T *rettv;
14125{
14126 if (rettv_list_alloc(rettv) == OK)
14127 {
14128#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014129 int id = get_tv_number(&argvars[0]);
14130 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014131
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014132 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014133 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014134 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14135 {
14136 list_append_string(rettv->vval.v_list,
14137 syn_id2name(m->hlg_id), -1);
14138 list_append_string(rettv->vval.v_list, m->pattern, -1);
14139 }
14140 else
14141 {
14142 list_append_string(rettv->vval.v_list, NUL, -1);
14143 list_append_string(rettv->vval.v_list, NUL, -1);
14144 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014145 }
14146#endif
14147 }
14148}
14149
14150/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014151 * "matchdelete()" function
14152 */
14153 static void
14154f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014155 typval_T *argvars UNUSED;
14156 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014157{
14158#ifdef FEAT_SEARCH_EXTRA
14159 rettv->vval.v_number = match_delete(curwin,
14160 (int)get_tv_number(&argvars[0]), TRUE);
14161#endif
14162}
14163
14164/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014165 * "matchend()" function
14166 */
14167 static void
14168f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014169 typval_T *argvars;
14170 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014171{
14172 find_some_match(argvars, rettv, 0);
14173}
14174
14175/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014176 * "matchlist()" function
14177 */
14178 static void
14179f_matchlist(argvars, rettv)
14180 typval_T *argvars;
14181 typval_T *rettv;
14182{
14183 find_some_match(argvars, rettv, 3);
14184}
14185
14186/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014187 * "matchstr()" function
14188 */
14189 static void
14190f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014191 typval_T *argvars;
14192 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014193{
14194 find_some_match(argvars, rettv, 2);
14195}
14196
Bram Moolenaar33570922005-01-25 22:26:29 +000014197static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014198
14199 static void
14200max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014201 typval_T *argvars;
14202 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014203 int domax;
14204{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014205 long n = 0;
14206 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014207 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014208
14209 if (argvars[0].v_type == VAR_LIST)
14210 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014211 list_T *l;
14212 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014213
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014214 l = argvars[0].vval.v_list;
14215 if (l != NULL)
14216 {
14217 li = l->lv_first;
14218 if (li != NULL)
14219 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014220 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014221 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014222 {
14223 li = li->li_next;
14224 if (li == NULL)
14225 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014226 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014227 if (domax ? i > n : i < n)
14228 n = i;
14229 }
14230 }
14231 }
14232 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014233 else if (argvars[0].v_type == VAR_DICT)
14234 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014235 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014236 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014237 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014238 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014239
14240 d = argvars[0].vval.v_dict;
14241 if (d != NULL)
14242 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014243 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014244 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014245 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014246 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014247 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014248 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014249 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014250 if (first)
14251 {
14252 n = i;
14253 first = FALSE;
14254 }
14255 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014256 n = i;
14257 }
14258 }
14259 }
14260 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014261 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014262 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014263 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014264}
14265
14266/*
14267 * "max()" function
14268 */
14269 static void
14270f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014271 typval_T *argvars;
14272 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014273{
14274 max_min(argvars, rettv, TRUE);
14275}
14276
14277/*
14278 * "min()" function
14279 */
14280 static void
14281f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014282 typval_T *argvars;
14283 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014284{
14285 max_min(argvars, rettv, FALSE);
14286}
14287
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014288static int mkdir_recurse __ARGS((char_u *dir, int prot));
14289
14290/*
14291 * Create the directory in which "dir" is located, and higher levels when
14292 * needed.
14293 */
14294 static int
14295mkdir_recurse(dir, prot)
14296 char_u *dir;
14297 int prot;
14298{
14299 char_u *p;
14300 char_u *updir;
14301 int r = FAIL;
14302
14303 /* Get end of directory name in "dir".
14304 * We're done when it's "/" or "c:/". */
14305 p = gettail_sep(dir);
14306 if (p <= get_past_head(dir))
14307 return OK;
14308
14309 /* If the directory exists we're done. Otherwise: create it.*/
14310 updir = vim_strnsave(dir, (int)(p - dir));
14311 if (updir == NULL)
14312 return FAIL;
14313 if (mch_isdir(updir))
14314 r = OK;
14315 else if (mkdir_recurse(updir, prot) == OK)
14316 r = vim_mkdir_emsg(updir, prot);
14317 vim_free(updir);
14318 return r;
14319}
14320
14321#ifdef vim_mkdir
14322/*
14323 * "mkdir()" function
14324 */
14325 static void
14326f_mkdir(argvars, rettv)
14327 typval_T *argvars;
14328 typval_T *rettv;
14329{
14330 char_u *dir;
14331 char_u buf[NUMBUFLEN];
14332 int prot = 0755;
14333
14334 rettv->vval.v_number = FAIL;
14335 if (check_restricted() || check_secure())
14336 return;
14337
14338 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014339 if (*dir == NUL)
14340 rettv->vval.v_number = FAIL;
14341 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014342 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014343 if (*gettail(dir) == NUL)
14344 /* remove trailing slashes */
14345 *gettail_sep(dir) = NUL;
14346
14347 if (argvars[1].v_type != VAR_UNKNOWN)
14348 {
14349 if (argvars[2].v_type != VAR_UNKNOWN)
14350 prot = get_tv_number_chk(&argvars[2], NULL);
14351 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14352 mkdir_recurse(dir, prot);
14353 }
14354 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014355 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014356}
14357#endif
14358
Bram Moolenaar0d660222005-01-07 21:51:51 +000014359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360 * "mode()" function
14361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014364 typval_T *argvars;
14365 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014367 char_u buf[3];
14368
14369 buf[1] = NUL;
14370 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371
14372#ifdef FEAT_VISUAL
14373 if (VIsual_active)
14374 {
14375 if (VIsual_select)
14376 buf[0] = VIsual_mode + 's' - 'v';
14377 else
14378 buf[0] = VIsual_mode;
14379 }
14380 else
14381#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014382 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14383 || State == CONFIRM)
14384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014386 if (State == ASKMORE)
14387 buf[1] = 'm';
14388 else if (State == CONFIRM)
14389 buf[1] = '?';
14390 }
14391 else if (State == EXTERNCMD)
14392 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393 else if (State & INSERT)
14394 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014395#ifdef FEAT_VREPLACE
14396 if (State & VREPLACE_FLAG)
14397 {
14398 buf[0] = 'R';
14399 buf[1] = 'v';
14400 }
14401 else
14402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014403 if (State & REPLACE_FLAG)
14404 buf[0] = 'R';
14405 else
14406 buf[0] = 'i';
14407 }
14408 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014409 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014411 if (exmode_active)
14412 buf[1] = 'v';
14413 }
14414 else if (exmode_active)
14415 {
14416 buf[0] = 'c';
14417 buf[1] = 'e';
14418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014420 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014421 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014422 if (finish_op)
14423 buf[1] = 'o';
14424 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014426 /* Clear out the minor mode when the argument is not a non-zero number or
14427 * non-empty string. */
14428 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014429 buf[1] = NUL;
14430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014431 rettv->vval.v_string = vim_strsave(buf);
14432 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433}
14434
Bram Moolenaar429fa852013-04-15 12:27:36 +020014435#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014436/*
14437 * "mzeval()" function
14438 */
14439 static void
14440f_mzeval(argvars, rettv)
14441 typval_T *argvars;
14442 typval_T *rettv;
14443{
14444 char_u *str;
14445 char_u buf[NUMBUFLEN];
14446
14447 str = get_tv_string_buf(&argvars[0], buf);
14448 do_mzeval(str, rettv);
14449}
Bram Moolenaar75676462013-01-30 14:55:42 +010014450
14451 void
14452mzscheme_call_vim(name, args, rettv)
14453 char_u *name;
14454 typval_T *args;
14455 typval_T *rettv;
14456{
14457 typval_T argvars[3];
14458
14459 argvars[0].v_type = VAR_STRING;
14460 argvars[0].vval.v_string = name;
14461 copy_tv(args, &argvars[1]);
14462 argvars[2].v_type = VAR_UNKNOWN;
14463 f_call(argvars, rettv);
14464 clear_tv(&argvars[1]);
14465}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014466#endif
14467
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014469 * "nextnonblank()" function
14470 */
14471 static void
14472f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014473 typval_T *argvars;
14474 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014475{
14476 linenr_T lnum;
14477
14478 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14479 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014480 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014481 {
14482 lnum = 0;
14483 break;
14484 }
14485 if (*skipwhite(ml_get(lnum)) != NUL)
14486 break;
14487 }
14488 rettv->vval.v_number = lnum;
14489}
14490
14491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014492 * "nr2char()" function
14493 */
14494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014495f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014496 typval_T *argvars;
14497 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498{
14499 char_u buf[NUMBUFLEN];
14500
14501#ifdef FEAT_MBYTE
14502 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014503 {
14504 int utf8 = 0;
14505
14506 if (argvars[1].v_type != VAR_UNKNOWN)
14507 utf8 = get_tv_number_chk(&argvars[1], NULL);
14508 if (utf8)
14509 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14510 else
14511 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 else
14514#endif
14515 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014516 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 buf[1] = NUL;
14518 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014519 rettv->v_type = VAR_STRING;
14520 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014521}
14522
14523/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014524 * "or(expr, expr)" function
14525 */
14526 static void
14527f_or(argvars, rettv)
14528 typval_T *argvars;
14529 typval_T *rettv;
14530{
14531 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14532 | get_tv_number_chk(&argvars[1], NULL);
14533}
14534
14535/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014536 * "pathshorten()" function
14537 */
14538 static void
14539f_pathshorten(argvars, rettv)
14540 typval_T *argvars;
14541 typval_T *rettv;
14542{
14543 char_u *p;
14544
14545 rettv->v_type = VAR_STRING;
14546 p = get_tv_string_chk(&argvars[0]);
14547 if (p == NULL)
14548 rettv->vval.v_string = NULL;
14549 else
14550 {
14551 p = vim_strsave(p);
14552 rettv->vval.v_string = p;
14553 if (p != NULL)
14554 shorten_dir(p);
14555 }
14556}
14557
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014558#ifdef FEAT_FLOAT
14559/*
14560 * "pow()" function
14561 */
14562 static void
14563f_pow(argvars, rettv)
14564 typval_T *argvars;
14565 typval_T *rettv;
14566{
14567 float_T fx, fy;
14568
14569 rettv->v_type = VAR_FLOAT;
14570 if (get_float_arg(argvars, &fx) == OK
14571 && get_float_arg(&argvars[1], &fy) == OK)
14572 rettv->vval.v_float = pow(fx, fy);
14573 else
14574 rettv->vval.v_float = 0.0;
14575}
14576#endif
14577
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014578/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014579 * "prevnonblank()" function
14580 */
14581 static void
14582f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014583 typval_T *argvars;
14584 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014585{
14586 linenr_T lnum;
14587
14588 lnum = get_tv_lnum(argvars);
14589 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14590 lnum = 0;
14591 else
14592 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14593 --lnum;
14594 rettv->vval.v_number = lnum;
14595}
14596
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014597#ifdef HAVE_STDARG_H
14598/* This dummy va_list is here because:
14599 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14600 * - locally in the function results in a "used before set" warning
14601 * - using va_start() to initialize it gives "function with fixed args" error */
14602static va_list ap;
14603#endif
14604
Bram Moolenaar8c711452005-01-14 21:53:12 +000014605/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014606 * "printf()" function
14607 */
14608 static void
14609f_printf(argvars, rettv)
14610 typval_T *argvars;
14611 typval_T *rettv;
14612{
14613 rettv->v_type = VAR_STRING;
14614 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014615#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014616 {
14617 char_u buf[NUMBUFLEN];
14618 int len;
14619 char_u *s;
14620 int saved_did_emsg = did_emsg;
14621 char *fmt;
14622
14623 /* Get the required length, allocate the buffer and do it for real. */
14624 did_emsg = FALSE;
14625 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014626 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014627 if (!did_emsg)
14628 {
14629 s = alloc(len + 1);
14630 if (s != NULL)
14631 {
14632 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014633 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014634 }
14635 }
14636 did_emsg |= saved_did_emsg;
14637 }
14638#endif
14639}
14640
14641/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014642 * "pumvisible()" function
14643 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014644 static void
14645f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014646 typval_T *argvars UNUSED;
14647 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014648{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014649#ifdef FEAT_INS_EXPAND
14650 if (pum_visible())
14651 rettv->vval.v_number = 1;
14652#endif
14653}
14654
Bram Moolenaardb913952012-06-29 12:54:53 +020014655#ifdef FEAT_PYTHON3
14656/*
14657 * "py3eval()" function
14658 */
14659 static void
14660f_py3eval(argvars, rettv)
14661 typval_T *argvars;
14662 typval_T *rettv;
14663{
14664 char_u *str;
14665 char_u buf[NUMBUFLEN];
14666
14667 str = get_tv_string_buf(&argvars[0], buf);
14668 do_py3eval(str, rettv);
14669}
14670#endif
14671
14672#ifdef FEAT_PYTHON
14673/*
14674 * "pyeval()" function
14675 */
14676 static void
14677f_pyeval(argvars, rettv)
14678 typval_T *argvars;
14679 typval_T *rettv;
14680{
14681 char_u *str;
14682 char_u buf[NUMBUFLEN];
14683
14684 str = get_tv_string_buf(&argvars[0], buf);
14685 do_pyeval(str, rettv);
14686}
14687#endif
14688
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014689/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014690 * "range()" function
14691 */
14692 static void
14693f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014694 typval_T *argvars;
14695 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014696{
14697 long start;
14698 long end;
14699 long stride = 1;
14700 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014701 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014702
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014703 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014704 if (argvars[1].v_type == VAR_UNKNOWN)
14705 {
14706 end = start - 1;
14707 start = 0;
14708 }
14709 else
14710 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014711 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014712 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014713 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014714 }
14715
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014716 if (error)
14717 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014718 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014719 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014720 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014721 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014722 else
14723 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014724 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014725 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014726 if (list_append_number(rettv->vval.v_list,
14727 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014728 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014729 }
14730}
14731
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014732/*
14733 * "readfile()" function
14734 */
14735 static void
14736f_readfile(argvars, rettv)
14737 typval_T *argvars;
14738 typval_T *rettv;
14739{
14740 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014741 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014742 char_u *fname;
14743 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014744 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14745 int io_size = sizeof(buf);
14746 int readlen; /* size of last fread() */
14747 char_u *prev = NULL; /* previously read bytes, if any */
14748 long prevlen = 0; /* length of data in prev */
14749 long prevsize = 0; /* size of prev buffer */
14750 long maxline = MAXLNUM;
14751 long cnt = 0;
14752 char_u *p; /* position in buf */
14753 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014754
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014755 if (argvars[1].v_type != VAR_UNKNOWN)
14756 {
14757 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14758 binary = TRUE;
14759 if (argvars[2].v_type != VAR_UNKNOWN)
14760 maxline = get_tv_number(&argvars[2]);
14761 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014762
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014763 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014764 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014765
14766 /* Always open the file in binary mode, library functions have a mind of
14767 * their own about CR-LF conversion. */
14768 fname = get_tv_string(&argvars[0]);
14769 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14770 {
14771 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14772 return;
14773 }
14774
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014775 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014776 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014777 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014778
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014779 /* This for loop processes what was read, but is also entered at end
14780 * of file so that either:
14781 * - an incomplete line gets written
14782 * - a "binary" file gets an empty line at the end if it ends in a
14783 * newline. */
14784 for (p = buf, start = buf;
14785 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14786 ++p)
14787 {
14788 if (*p == '\n' || readlen <= 0)
14789 {
14790 listitem_T *li;
14791 char_u *s = NULL;
14792 long_u len = p - start;
14793
14794 /* Finished a line. Remove CRs before NL. */
14795 if (readlen > 0 && !binary)
14796 {
14797 while (len > 0 && start[len - 1] == '\r')
14798 --len;
14799 /* removal may cross back to the "prev" string */
14800 if (len == 0)
14801 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14802 --prevlen;
14803 }
14804 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014805 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014806 else
14807 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014808 /* Change "prev" buffer to be the right size. This way
14809 * the bytes are only copied once, and very long lines are
14810 * allocated only once. */
14811 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014812 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014813 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014814 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014815 prev = NULL; /* the list will own the string */
14816 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014817 }
14818 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014819 if (s == NULL)
14820 {
14821 do_outofmem_msg((long_u) prevlen + len + 1);
14822 failed = TRUE;
14823 break;
14824 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014825
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014826 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014827 {
14828 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014829 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014830 break;
14831 }
14832 li->li_tv.v_type = VAR_STRING;
14833 li->li_tv.v_lock = 0;
14834 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014835 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014836
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014837 start = p + 1; /* step over newline */
14838 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014839 break;
14840 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014841 else if (*p == NUL)
14842 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014843#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014844 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14845 * when finding the BF and check the previous two bytes. */
14846 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014847 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014848 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14849 * + 1, these may be in the "prev" string. */
14850 char_u back1 = p >= buf + 1 ? p[-1]
14851 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14852 char_u back2 = p >= buf + 2 ? p[-2]
14853 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14854 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014855
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014856 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014857 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014858 char_u *dest = p - 2;
14859
14860 /* Usually a BOM is at the beginning of a file, and so at
14861 * the beginning of a line; then we can just step over it.
14862 */
14863 if (start == dest)
14864 start = p + 1;
14865 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014866 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014867 /* have to shuffle buf to close gap */
14868 int adjust_prevlen = 0;
14869
14870 if (dest < buf)
14871 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014872 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014873 dest = buf;
14874 }
14875 if (readlen > p - buf + 1)
14876 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14877 readlen -= 3 - adjust_prevlen;
14878 prevlen -= adjust_prevlen;
14879 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014880 }
14881 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014882 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014883#endif
14884 } /* for */
14885
14886 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14887 break;
14888 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014889 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014890 /* There's part of a line in buf, store it in "prev". */
14891 if (p - start + prevlen >= prevsize)
14892 {
14893 /* need bigger "prev" buffer */
14894 char_u *newprev;
14895
14896 /* A common use case is ordinary text files and "prev" gets a
14897 * fragment of a line, so the first allocation is made
14898 * small, to avoid repeatedly 'allocing' large and
14899 * 'reallocing' small. */
14900 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014901 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014902 else
14903 {
14904 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014905 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014906 prevsize = grow50pc > growmin ? grow50pc : growmin;
14907 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014908 newprev = prev == NULL ? alloc(prevsize)
14909 : vim_realloc(prev, prevsize);
14910 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014911 {
14912 do_outofmem_msg((long_u)prevsize);
14913 failed = TRUE;
14914 break;
14915 }
14916 prev = newprev;
14917 }
14918 /* Add the line part to end of "prev". */
14919 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014920 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014921 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014922 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014923
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014924 /*
14925 * For a negative line count use only the lines at the end of the file,
14926 * free the rest.
14927 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014928 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014929 while (cnt > -maxline)
14930 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014931 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014932 --cnt;
14933 }
14934
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014935 if (failed)
14936 {
14937 list_free(rettv->vval.v_list, TRUE);
14938 /* readfile doc says an empty list is returned on error */
14939 rettv->vval.v_list = list_alloc();
14940 }
14941
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014942 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014943 fclose(fd);
14944}
14945
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014946#if defined(FEAT_RELTIME)
14947static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14948
14949/*
14950 * Convert a List to proftime_T.
14951 * Return FAIL when there is something wrong.
14952 */
14953 static int
14954list2proftime(arg, tm)
14955 typval_T *arg;
14956 proftime_T *tm;
14957{
14958 long n1, n2;
14959 int error = FALSE;
14960
14961 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14962 || arg->vval.v_list->lv_len != 2)
14963 return FAIL;
14964 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14965 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14966# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014967 tm->HighPart = n1;
14968 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014969# else
14970 tm->tv_sec = n1;
14971 tm->tv_usec = n2;
14972# endif
14973 return error ? FAIL : OK;
14974}
14975#endif /* FEAT_RELTIME */
14976
14977/*
14978 * "reltime()" function
14979 */
14980 static void
14981f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014982 typval_T *argvars UNUSED;
14983 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014984{
14985#ifdef FEAT_RELTIME
14986 proftime_T res;
14987 proftime_T start;
14988
14989 if (argvars[0].v_type == VAR_UNKNOWN)
14990 {
14991 /* No arguments: get current time. */
14992 profile_start(&res);
14993 }
14994 else if (argvars[1].v_type == VAR_UNKNOWN)
14995 {
14996 if (list2proftime(&argvars[0], &res) == FAIL)
14997 return;
14998 profile_end(&res);
14999 }
15000 else
15001 {
15002 /* Two arguments: compute the difference. */
15003 if (list2proftime(&argvars[0], &start) == FAIL
15004 || list2proftime(&argvars[1], &res) == FAIL)
15005 return;
15006 profile_sub(&res, &start);
15007 }
15008
15009 if (rettv_list_alloc(rettv) == OK)
15010 {
15011 long n1, n2;
15012
15013# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000015014 n1 = res.HighPart;
15015 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015016# else
15017 n1 = res.tv_sec;
15018 n2 = res.tv_usec;
15019# endif
15020 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
15021 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
15022 }
15023#endif
15024}
15025
15026/*
15027 * "reltimestr()" function
15028 */
15029 static void
15030f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010015031 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000015032 typval_T *rettv;
15033{
15034#ifdef FEAT_RELTIME
15035 proftime_T tm;
15036#endif
15037
15038 rettv->v_type = VAR_STRING;
15039 rettv->vval.v_string = NULL;
15040#ifdef FEAT_RELTIME
15041 if (list2proftime(&argvars[0], &tm) == OK)
15042 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15043#endif
15044}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015045
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15047static void make_connection __ARGS((void));
15048static int check_connection __ARGS((void));
15049
15050 static void
15051make_connection()
15052{
15053 if (X_DISPLAY == NULL
15054# ifdef FEAT_GUI
15055 && !gui.in_use
15056# endif
15057 )
15058 {
15059 x_force_connect = TRUE;
15060 setup_term_clip();
15061 x_force_connect = FALSE;
15062 }
15063}
15064
15065 static int
15066check_connection()
15067{
15068 make_connection();
15069 if (X_DISPLAY == NULL)
15070 {
15071 EMSG(_("E240: No connection to Vim server"));
15072 return FAIL;
15073 }
15074 return OK;
15075}
15076#endif
15077
15078#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015079static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015080
15081 static void
15082remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015083 typval_T *argvars;
15084 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085 int expr;
15086{
15087 char_u *server_name;
15088 char_u *keys;
15089 char_u *r = NULL;
15090 char_u buf[NUMBUFLEN];
15091# ifdef WIN32
15092 HWND w;
15093# else
15094 Window w;
15095# endif
15096
15097 if (check_restricted() || check_secure())
15098 return;
15099
15100# ifdef FEAT_X11
15101 if (check_connection() == FAIL)
15102 return;
15103# endif
15104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015105 server_name = get_tv_string_chk(&argvars[0]);
15106 if (server_name == NULL)
15107 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015108 keys = get_tv_string_buf(&argvars[1], buf);
15109# ifdef WIN32
15110 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15111# else
15112 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15113 < 0)
15114# endif
15115 {
15116 if (r != NULL)
15117 EMSG(r); /* sending worked but evaluation failed */
15118 else
15119 EMSG2(_("E241: Unable to send to %s"), server_name);
15120 return;
15121 }
15122
15123 rettv->vval.v_string = r;
15124
15125 if (argvars[2].v_type != VAR_UNKNOWN)
15126 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015127 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015128 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015129 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015130
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015131 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015132 v.di_tv.v_type = VAR_STRING;
15133 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015134 idvar = get_tv_string_chk(&argvars[2]);
15135 if (idvar != NULL)
15136 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015137 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015138 }
15139}
15140#endif
15141
15142/*
15143 * "remote_expr()" function
15144 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015145 static void
15146f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015147 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015148 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015149{
15150 rettv->v_type = VAR_STRING;
15151 rettv->vval.v_string = NULL;
15152#ifdef FEAT_CLIENTSERVER
15153 remote_common(argvars, rettv, TRUE);
15154#endif
15155}
15156
15157/*
15158 * "remote_foreground()" function
15159 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015160 static void
15161f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015162 typval_T *argvars UNUSED;
15163 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015164{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015165#ifdef FEAT_CLIENTSERVER
15166# ifdef WIN32
15167 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015168 {
15169 char_u *server_name = get_tv_string_chk(&argvars[0]);
15170
15171 if (server_name != NULL)
15172 serverForeground(server_name);
15173 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015174# else
15175 /* Send a foreground() expression to the server. */
15176 argvars[1].v_type = VAR_STRING;
15177 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15178 argvars[2].v_type = VAR_UNKNOWN;
15179 remote_common(argvars, rettv, TRUE);
15180 vim_free(argvars[1].vval.v_string);
15181# endif
15182#endif
15183}
15184
Bram Moolenaar0d660222005-01-07 21:51:51 +000015185 static void
15186f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015187 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015188 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015189{
15190#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015191 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015192 char_u *s = NULL;
15193# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015194 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015195# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015196 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015197
15198 if (check_restricted() || check_secure())
15199 {
15200 rettv->vval.v_number = -1;
15201 return;
15202 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015203 serverid = get_tv_string_chk(&argvars[0]);
15204 if (serverid == NULL)
15205 {
15206 rettv->vval.v_number = -1;
15207 return; /* type error; errmsg already given */
15208 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015209# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015210 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015211 if (n == 0)
15212 rettv->vval.v_number = -1;
15213 else
15214 {
15215 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15216 rettv->vval.v_number = (s != NULL);
15217 }
15218# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015219 if (check_connection() == FAIL)
15220 return;
15221
15222 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015223 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015224# endif
15225
15226 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015228 char_u *retvar;
15229
Bram Moolenaar33570922005-01-25 22:26:29 +000015230 v.di_tv.v_type = VAR_STRING;
15231 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015232 retvar = get_tv_string_chk(&argvars[1]);
15233 if (retvar != NULL)
15234 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015235 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015236 }
15237#else
15238 rettv->vval.v_number = -1;
15239#endif
15240}
15241
Bram Moolenaar0d660222005-01-07 21:51:51 +000015242 static void
15243f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015244 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015245 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015246{
15247 char_u *r = NULL;
15248
15249#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015250 char_u *serverid = get_tv_string_chk(&argvars[0]);
15251
15252 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015253 {
15254# ifdef WIN32
15255 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015256 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015257
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015258 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015259 if (n != 0)
15260 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15261 if (r == NULL)
15262# else
15263 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015264 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015265# endif
15266 EMSG(_("E277: Unable to read a server reply"));
15267 }
15268#endif
15269 rettv->v_type = VAR_STRING;
15270 rettv->vval.v_string = r;
15271}
15272
15273/*
15274 * "remote_send()" function
15275 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015276 static void
15277f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015278 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015279 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015280{
15281 rettv->v_type = VAR_STRING;
15282 rettv->vval.v_string = NULL;
15283#ifdef FEAT_CLIENTSERVER
15284 remote_common(argvars, rettv, FALSE);
15285#endif
15286}
15287
15288/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015289 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015290 */
15291 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015292f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015293 typval_T *argvars;
15294 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015295{
Bram Moolenaar33570922005-01-25 22:26:29 +000015296 list_T *l;
15297 listitem_T *item, *item2;
15298 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015299 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015300 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015301 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015302 dict_T *d;
15303 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015304 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015305
Bram Moolenaar8c711452005-01-14 21:53:12 +000015306 if (argvars[0].v_type == VAR_DICT)
15307 {
15308 if (argvars[2].v_type != VAR_UNKNOWN)
15309 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015310 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015311 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015312 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015313 key = get_tv_string_chk(&argvars[1]);
15314 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015316 di = dict_find(d, key, -1);
15317 if (di == NULL)
15318 EMSG2(_(e_dictkey), key);
15319 else
15320 {
15321 *rettv = di->di_tv;
15322 init_tv(&di->di_tv);
15323 dictitem_remove(d, di);
15324 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015326 }
15327 }
15328 else if (argvars[0].v_type != VAR_LIST)
15329 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015330 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015331 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015332 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015333 int error = FALSE;
15334
15335 idx = get_tv_number_chk(&argvars[1], &error);
15336 if (error)
15337 ; /* type error: do nothing, errmsg already given */
15338 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015339 EMSGN(_(e_listidx), idx);
15340 else
15341 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015342 if (argvars[2].v_type == VAR_UNKNOWN)
15343 {
15344 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015345 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015346 *rettv = item->li_tv;
15347 vim_free(item);
15348 }
15349 else
15350 {
15351 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015352 end = get_tv_number_chk(&argvars[2], &error);
15353 if (error)
15354 ; /* type error: do nothing */
15355 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015356 EMSGN(_(e_listidx), end);
15357 else
15358 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015359 int cnt = 0;
15360
15361 for (li = item; li != NULL; li = li->li_next)
15362 {
15363 ++cnt;
15364 if (li == item2)
15365 break;
15366 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015367 if (li == NULL) /* didn't find "item2" after "item" */
15368 EMSG(_(e_invrange));
15369 else
15370 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015371 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015372 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015373 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015374 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015375 l->lv_first = item;
15376 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015377 item->li_prev = NULL;
15378 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015379 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015380 }
15381 }
15382 }
15383 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015384 }
15385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386}
15387
15388/*
15389 * "rename({from}, {to})" function
15390 */
15391 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015392f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015393 typval_T *argvars;
15394 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395{
15396 char_u buf[NUMBUFLEN];
15397
15398 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015399 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015401 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15402 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015403}
15404
15405/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015406 * "repeat()" function
15407 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015408 static void
15409f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015410 typval_T *argvars;
15411 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015412{
15413 char_u *p;
15414 int n;
15415 int slen;
15416 int len;
15417 char_u *r;
15418 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015419
15420 n = get_tv_number(&argvars[1]);
15421 if (argvars[0].v_type == VAR_LIST)
15422 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015423 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015424 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015425 if (list_extend(rettv->vval.v_list,
15426 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015427 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015428 }
15429 else
15430 {
15431 p = get_tv_string(&argvars[0]);
15432 rettv->v_type = VAR_STRING;
15433 rettv->vval.v_string = NULL;
15434
15435 slen = (int)STRLEN(p);
15436 len = slen * n;
15437 if (len <= 0)
15438 return;
15439
15440 r = alloc(len + 1);
15441 if (r != NULL)
15442 {
15443 for (i = 0; i < n; i++)
15444 mch_memmove(r + i * slen, p, (size_t)slen);
15445 r[len] = NUL;
15446 }
15447
15448 rettv->vval.v_string = r;
15449 }
15450}
15451
15452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453 * "resolve()" function
15454 */
15455 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015456f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015457 typval_T *argvars;
15458 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459{
15460 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015461#ifdef HAVE_READLINK
15462 char_u *buf = NULL;
15463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015465 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466#ifdef FEAT_SHORTCUT
15467 {
15468 char_u *v = NULL;
15469
15470 v = mch_resolve_shortcut(p);
15471 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015472 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015474 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015475 }
15476#else
15477# ifdef HAVE_READLINK
15478 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015479 char_u *cpy;
15480 int len;
15481 char_u *remain = NULL;
15482 char_u *q;
15483 int is_relative_to_current = FALSE;
15484 int has_trailing_pathsep = FALSE;
15485 int limit = 100;
15486
15487 p = vim_strsave(p);
15488
15489 if (p[0] == '.' && (vim_ispathsep(p[1])
15490 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15491 is_relative_to_current = TRUE;
15492
15493 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015494 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015497 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499
15500 q = getnextcomp(p);
15501 if (*q != NUL)
15502 {
15503 /* Separate the first path component in "p", and keep the
15504 * remainder (beginning with the path separator). */
15505 remain = vim_strsave(q - 1);
15506 q[-1] = NUL;
15507 }
15508
Bram Moolenaard9462e32011-04-11 21:35:11 +020015509 buf = alloc(MAXPATHL + 1);
15510 if (buf == NULL)
15511 goto fail;
15512
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 for (;;)
15514 {
15515 for (;;)
15516 {
15517 len = readlink((char *)p, (char *)buf, MAXPATHL);
15518 if (len <= 0)
15519 break;
15520 buf[len] = NUL;
15521
15522 if (limit-- == 0)
15523 {
15524 vim_free(p);
15525 vim_free(remain);
15526 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015527 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015528 goto fail;
15529 }
15530
15531 /* Ensure that the result will have a trailing path separator
15532 * if the argument has one. */
15533 if (remain == NULL && has_trailing_pathsep)
15534 add_pathsep(buf);
15535
15536 /* Separate the first path component in the link value and
15537 * concatenate the remainders. */
15538 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15539 if (*q != NUL)
15540 {
15541 if (remain == NULL)
15542 remain = vim_strsave(q - 1);
15543 else
15544 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015545 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015546 if (cpy != NULL)
15547 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548 vim_free(remain);
15549 remain = cpy;
15550 }
15551 }
15552 q[-1] = NUL;
15553 }
15554
15555 q = gettail(p);
15556 if (q > p && *q == NUL)
15557 {
15558 /* Ignore trailing path separator. */
15559 q[-1] = NUL;
15560 q = gettail(p);
15561 }
15562 if (q > p && !mch_isFullName(buf))
15563 {
15564 /* symlink is relative to directory of argument */
15565 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15566 if (cpy != NULL)
15567 {
15568 STRCPY(cpy, p);
15569 STRCPY(gettail(cpy), buf);
15570 vim_free(p);
15571 p = cpy;
15572 }
15573 }
15574 else
15575 {
15576 vim_free(p);
15577 p = vim_strsave(buf);
15578 }
15579 }
15580
15581 if (remain == NULL)
15582 break;
15583
15584 /* Append the first path component of "remain" to "p". */
15585 q = getnextcomp(remain + 1);
15586 len = q - remain - (*q != NUL);
15587 cpy = vim_strnsave(p, STRLEN(p) + len);
15588 if (cpy != NULL)
15589 {
15590 STRNCAT(cpy, remain, len);
15591 vim_free(p);
15592 p = cpy;
15593 }
15594 /* Shorten "remain". */
15595 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015596 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597 else
15598 {
15599 vim_free(remain);
15600 remain = NULL;
15601 }
15602 }
15603
15604 /* If the result is a relative path name, make it explicitly relative to
15605 * the current directory if and only if the argument had this form. */
15606 if (!vim_ispathsep(*p))
15607 {
15608 if (is_relative_to_current
15609 && *p != NUL
15610 && !(p[0] == '.'
15611 && (p[1] == NUL
15612 || vim_ispathsep(p[1])
15613 || (p[1] == '.'
15614 && (p[2] == NUL
15615 || vim_ispathsep(p[2]))))))
15616 {
15617 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015618 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015619 if (cpy != NULL)
15620 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621 vim_free(p);
15622 p = cpy;
15623 }
15624 }
15625 else if (!is_relative_to_current)
15626 {
15627 /* Strip leading "./". */
15628 q = p;
15629 while (q[0] == '.' && vim_ispathsep(q[1]))
15630 q += 2;
15631 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015632 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633 }
15634 }
15635
15636 /* Ensure that the result will have no trailing path separator
15637 * if the argument had none. But keep "/" or "//". */
15638 if (!has_trailing_pathsep)
15639 {
15640 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015641 if (after_pathsep(p, q))
15642 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643 }
15644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015645 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646 }
15647# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015648 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649# endif
15650#endif
15651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015652 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015653
15654#ifdef HAVE_READLINK
15655fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015656 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015658 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015659}
15660
15661/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015662 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663 */
15664 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015665f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015666 typval_T *argvars;
15667 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668{
Bram Moolenaar33570922005-01-25 22:26:29 +000015669 list_T *l;
15670 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671
Bram Moolenaar0d660222005-01-07 21:51:51 +000015672 if (argvars[0].v_type != VAR_LIST)
15673 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015674 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015675 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015676 {
15677 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015678 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015679 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015680 while (li != NULL)
15681 {
15682 ni = li->li_prev;
15683 list_append(l, li);
15684 li = ni;
15685 }
15686 rettv->vval.v_list = l;
15687 rettv->v_type = VAR_LIST;
15688 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015689 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015691}
15692
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015693#define SP_NOMOVE 0x01 /* don't move cursor */
15694#define SP_REPEAT 0x02 /* repeat to find outer pair */
15695#define SP_RETCOUNT 0x04 /* return matchcount */
15696#define SP_SETPCMARK 0x08 /* set previous context mark */
15697#define SP_START 0x10 /* accept match at start position */
15698#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15699#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015700
Bram Moolenaar33570922005-01-25 22:26:29 +000015701static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015702
15703/*
15704 * Get flags for a search function.
15705 * Possibly sets "p_ws".
15706 * Returns BACKWARD, FORWARD or zero (for an error).
15707 */
15708 static int
15709get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015710 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015711 int *flagsp;
15712{
15713 int dir = FORWARD;
15714 char_u *flags;
15715 char_u nbuf[NUMBUFLEN];
15716 int mask;
15717
15718 if (varp->v_type != VAR_UNKNOWN)
15719 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015720 flags = get_tv_string_buf_chk(varp, nbuf);
15721 if (flags == NULL)
15722 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015723 while (*flags != NUL)
15724 {
15725 switch (*flags)
15726 {
15727 case 'b': dir = BACKWARD; break;
15728 case 'w': p_ws = TRUE; break;
15729 case 'W': p_ws = FALSE; break;
15730 default: mask = 0;
15731 if (flagsp != NULL)
15732 switch (*flags)
15733 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015734 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015735 case 'e': mask = SP_END; break;
15736 case 'm': mask = SP_RETCOUNT; break;
15737 case 'n': mask = SP_NOMOVE; break;
15738 case 'p': mask = SP_SUBPAT; break;
15739 case 'r': mask = SP_REPEAT; break;
15740 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015741 }
15742 if (mask == 0)
15743 {
15744 EMSG2(_(e_invarg2), flags);
15745 dir = 0;
15746 }
15747 else
15748 *flagsp |= mask;
15749 }
15750 if (dir == 0)
15751 break;
15752 ++flags;
15753 }
15754 }
15755 return dir;
15756}
15757
Bram Moolenaar071d4272004-06-13 20:20:40 +000015758/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015759 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015760 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015761 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015762search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015763 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015764 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015765 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015767 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768 char_u *pat;
15769 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015770 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015771 int save_p_ws = p_ws;
15772 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015773 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015774 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015775 proftime_T tm;
15776#ifdef FEAT_RELTIME
15777 long time_limit = 0;
15778#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015779 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015780 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015782 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015783 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015784 if (dir == 0)
15785 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015786 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015787 if (flags & SP_START)
15788 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015789 if (flags & SP_END)
15790 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015791
Bram Moolenaar76929292008-01-06 19:07:36 +000015792 /* Optional arguments: line number to stop searching and timeout. */
15793 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015794 {
15795 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15796 if (lnum_stop < 0)
15797 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015798#ifdef FEAT_RELTIME
15799 if (argvars[3].v_type != VAR_UNKNOWN)
15800 {
15801 time_limit = get_tv_number_chk(&argvars[3], NULL);
15802 if (time_limit < 0)
15803 goto theend;
15804 }
15805#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015806 }
15807
Bram Moolenaar76929292008-01-06 19:07:36 +000015808#ifdef FEAT_RELTIME
15809 /* Set the time limit, if there is one. */
15810 profile_setlimit(time_limit, &tm);
15811#endif
15812
Bram Moolenaar231334e2005-07-25 20:46:57 +000015813 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015814 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015815 * Check to make sure only those flags are set.
15816 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15817 * flags cannot be set. Check for that condition also.
15818 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015819 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015820 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015821 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015822 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015823 goto theend;
15824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015825
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015826 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015827 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015828 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015829 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015830 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015831 if (flags & SP_SUBPAT)
15832 retval = subpatnum;
15833 else
15834 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015835 if (flags & SP_SETPCMARK)
15836 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015837 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015838 if (match_pos != NULL)
15839 {
15840 /* Store the match cursor position */
15841 match_pos->lnum = pos.lnum;
15842 match_pos->col = pos.col + 1;
15843 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844 /* "/$" will put the cursor after the end of the line, may need to
15845 * correct that here */
15846 check_cursor();
15847 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015848
15849 /* If 'n' flag is used: restore cursor position. */
15850 if (flags & SP_NOMOVE)
15851 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015852 else
15853 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015854theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015855 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015856
15857 return retval;
15858}
15859
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015860#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015861
15862/*
15863 * round() is not in C90, use ceil() or floor() instead.
15864 */
15865 float_T
15866vim_round(f)
15867 float_T f;
15868{
15869 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15870}
15871
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015872/*
15873 * "round({float})" function
15874 */
15875 static void
15876f_round(argvars, rettv)
15877 typval_T *argvars;
15878 typval_T *rettv;
15879{
15880 float_T f;
15881
15882 rettv->v_type = VAR_FLOAT;
15883 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015884 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015885 else
15886 rettv->vval.v_float = 0.0;
15887}
15888#endif
15889
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015890/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015891 * "screenattr()" function
15892 */
15893 static void
15894f_screenattr(argvars, rettv)
15895 typval_T *argvars UNUSED;
15896 typval_T *rettv;
15897{
15898 int row;
15899 int col;
15900 int c;
15901
15902 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15903 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15904 if (row < 0 || row >= screen_Rows
15905 || col < 0 || col >= screen_Columns)
15906 c = -1;
15907 else
15908 c = ScreenAttrs[LineOffset[row] + col];
15909 rettv->vval.v_number = c;
15910}
15911
15912/*
15913 * "screenchar()" function
15914 */
15915 static void
15916f_screenchar(argvars, rettv)
15917 typval_T *argvars UNUSED;
15918 typval_T *rettv;
15919{
15920 int row;
15921 int col;
15922 int off;
15923 int c;
15924
15925 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15926 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15927 if (row < 0 || row >= screen_Rows
15928 || col < 0 || col >= screen_Columns)
15929 c = -1;
15930 else
15931 {
15932 off = LineOffset[row] + col;
15933#ifdef FEAT_MBYTE
15934 if (enc_utf8 && ScreenLinesUC[off] != 0)
15935 c = ScreenLinesUC[off];
15936 else
15937#endif
15938 c = ScreenLines[off];
15939 }
15940 rettv->vval.v_number = c;
15941}
15942
15943/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015944 * "screencol()" function
15945 *
15946 * First column is 1 to be consistent with virtcol().
15947 */
15948 static void
15949f_screencol(argvars, rettv)
15950 typval_T *argvars UNUSED;
15951 typval_T *rettv;
15952{
15953 rettv->vval.v_number = screen_screencol() + 1;
15954}
15955
15956/*
15957 * "screenrow()" function
15958 */
15959 static void
15960f_screenrow(argvars, rettv)
15961 typval_T *argvars UNUSED;
15962 typval_T *rettv;
15963{
15964 rettv->vval.v_number = screen_screenrow() + 1;
15965}
15966
15967/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015968 * "search()" function
15969 */
15970 static void
15971f_search(argvars, rettv)
15972 typval_T *argvars;
15973 typval_T *rettv;
15974{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015975 int flags = 0;
15976
15977 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978}
15979
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015981 * "searchdecl()" function
15982 */
15983 static void
15984f_searchdecl(argvars, rettv)
15985 typval_T *argvars;
15986 typval_T *rettv;
15987{
15988 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015989 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015990 int error = FALSE;
15991 char_u *name;
15992
15993 rettv->vval.v_number = 1; /* default: FAIL */
15994
15995 name = get_tv_string_chk(&argvars[0]);
15996 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015997 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015998 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015999 if (!error && argvars[2].v_type != VAR_UNKNOWN)
16000 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
16001 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016002 if (!error && name != NULL)
16003 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000016004 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000016005}
16006
16007/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016008 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000016009 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016010 static int
16011searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000016012 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016013 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016014{
16015 char_u *spat, *mpat, *epat;
16016 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016017 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016018 int dir;
16019 int flags = 0;
16020 char_u nbuf1[NUMBUFLEN];
16021 char_u nbuf2[NUMBUFLEN];
16022 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016023 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016024 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000016025 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016026
Bram Moolenaar071d4272004-06-13 20:20:40 +000016027 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016028 spat = get_tv_string_chk(&argvars[0]);
16029 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
16030 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
16031 if (spat == NULL || mpat == NULL || epat == NULL)
16032 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016033
Bram Moolenaar071d4272004-06-13 20:20:40 +000016034 /* Handle the optional fourth argument: flags */
16035 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016036 if (dir == 0)
16037 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016038
16039 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016040 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16041 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016042 if ((flags & (SP_END | SP_SUBPAT)) != 0
16043 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016044 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016045 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016046 goto theend;
16047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016048
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016049 /* Using 'r' implies 'W', otherwise it doesn't work. */
16050 if (flags & SP_REPEAT)
16051 p_ws = FALSE;
16052
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016053 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016054 if (argvars[3].v_type == VAR_UNKNOWN
16055 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016056 skip = (char_u *)"";
16057 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016058 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016059 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016060 if (argvars[5].v_type != VAR_UNKNOWN)
16061 {
16062 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16063 if (lnum_stop < 0)
16064 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016065#ifdef FEAT_RELTIME
16066 if (argvars[6].v_type != VAR_UNKNOWN)
16067 {
16068 time_limit = get_tv_number_chk(&argvars[6], NULL);
16069 if (time_limit < 0)
16070 goto theend;
16071 }
16072#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016073 }
16074 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016075 if (skip == NULL)
16076 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016077
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016078 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016079 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016080
16081theend:
16082 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016083
16084 return retval;
16085}
16086
16087/*
16088 * "searchpair()" function
16089 */
16090 static void
16091f_searchpair(argvars, rettv)
16092 typval_T *argvars;
16093 typval_T *rettv;
16094{
16095 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16096}
16097
16098/*
16099 * "searchpairpos()" function
16100 */
16101 static void
16102f_searchpairpos(argvars, rettv)
16103 typval_T *argvars;
16104 typval_T *rettv;
16105{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016106 pos_T match_pos;
16107 int lnum = 0;
16108 int col = 0;
16109
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016110 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016111 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016112
16113 if (searchpair_cmn(argvars, &match_pos) > 0)
16114 {
16115 lnum = match_pos.lnum;
16116 col = match_pos.col;
16117 }
16118
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016119 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16120 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016121}
16122
16123/*
16124 * Search for a start/middle/end thing.
16125 * Used by searchpair(), see its documentation for the details.
16126 * Returns 0 or -1 for no match,
16127 */
16128 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016129do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16130 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016131 char_u *spat; /* start pattern */
16132 char_u *mpat; /* middle pattern */
16133 char_u *epat; /* end pattern */
16134 int dir; /* BACKWARD or FORWARD */
16135 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016136 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016137 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016138 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016139 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016140{
16141 char_u *save_cpo;
16142 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16143 long retval = 0;
16144 pos_T pos;
16145 pos_T firstpos;
16146 pos_T foundpos;
16147 pos_T save_cursor;
16148 pos_T save_pos;
16149 int n;
16150 int r;
16151 int nest = 1;
16152 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016153 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016154 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016155
16156 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16157 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016158 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016159
Bram Moolenaar76929292008-01-06 19:07:36 +000016160#ifdef FEAT_RELTIME
16161 /* Set the time limit, if there is one. */
16162 profile_setlimit(time_limit, &tm);
16163#endif
16164
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016165 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16166 * start/middle/end (pat3, for the top pair). */
16167 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16168 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16169 if (pat2 == NULL || pat3 == NULL)
16170 goto theend;
16171 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16172 if (*mpat == NUL)
16173 STRCPY(pat3, pat2);
16174 else
16175 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16176 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016177 if (flags & SP_START)
16178 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016179
Bram Moolenaar071d4272004-06-13 20:20:40 +000016180 save_cursor = curwin->w_cursor;
16181 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016182 clearpos(&firstpos);
16183 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016184 pat = pat3;
16185 for (;;)
16186 {
16187 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016188 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016189 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16190 /* didn't find it or found the first match again: FAIL */
16191 break;
16192
16193 if (firstpos.lnum == 0)
16194 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016195 if (equalpos(pos, foundpos))
16196 {
16197 /* Found the same position again. Can happen with a pattern that
16198 * has "\zs" at the end and searching backwards. Advance one
16199 * character and try again. */
16200 if (dir == BACKWARD)
16201 decl(&pos);
16202 else
16203 incl(&pos);
16204 }
16205 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016206
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016207 /* clear the start flag to avoid getting stuck here */
16208 options &= ~SEARCH_START;
16209
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210 /* If the skip pattern matches, ignore this match. */
16211 if (*skip != NUL)
16212 {
16213 save_pos = curwin->w_cursor;
16214 curwin->w_cursor = pos;
16215 r = eval_to_bool(skip, &err, NULL, FALSE);
16216 curwin->w_cursor = save_pos;
16217 if (err)
16218 {
16219 /* Evaluating {skip} caused an error, break here. */
16220 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016221 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016222 break;
16223 }
16224 if (r)
16225 continue;
16226 }
16227
16228 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16229 {
16230 /* Found end when searching backwards or start when searching
16231 * forward: nested pair. */
16232 ++nest;
16233 pat = pat2; /* nested, don't search for middle */
16234 }
16235 else
16236 {
16237 /* Found end when searching forward or start when searching
16238 * backward: end of (nested) pair; or found middle in outer pair. */
16239 if (--nest == 1)
16240 pat = pat3; /* outer level, search for middle */
16241 }
16242
16243 if (nest == 0)
16244 {
16245 /* Found the match: return matchcount or line number. */
16246 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016247 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016248 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016249 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016250 if (flags & SP_SETPCMARK)
16251 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 curwin->w_cursor = pos;
16253 if (!(flags & SP_REPEAT))
16254 break;
16255 nest = 1; /* search for next unmatched */
16256 }
16257 }
16258
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016259 if (match_pos != NULL)
16260 {
16261 /* Store the match cursor position */
16262 match_pos->lnum = curwin->w_cursor.lnum;
16263 match_pos->col = curwin->w_cursor.col + 1;
16264 }
16265
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016267 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016268 curwin->w_cursor = save_cursor;
16269
16270theend:
16271 vim_free(pat2);
16272 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016273 if (p_cpo == empty_option)
16274 p_cpo = save_cpo;
16275 else
16276 /* Darn, evaluating the {skip} expression changed the value. */
16277 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016278
16279 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016280}
16281
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016282/*
16283 * "searchpos()" function
16284 */
16285 static void
16286f_searchpos(argvars, rettv)
16287 typval_T *argvars;
16288 typval_T *rettv;
16289{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016290 pos_T match_pos;
16291 int lnum = 0;
16292 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016293 int n;
16294 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016295
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016296 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016297 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016298
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016299 n = search_cmn(argvars, &match_pos, &flags);
16300 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016301 {
16302 lnum = match_pos.lnum;
16303 col = match_pos.col;
16304 }
16305
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016306 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16307 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016308 if (flags & SP_SUBPAT)
16309 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016310}
16311
16312
Bram Moolenaar0d660222005-01-07 21:51:51 +000016313 static void
16314f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016315 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016316 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016317{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016318#ifdef FEAT_CLIENTSERVER
16319 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016320 char_u *server = get_tv_string_chk(&argvars[0]);
16321 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322
Bram Moolenaar0d660222005-01-07 21:51:51 +000016323 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016324 if (server == NULL || reply == NULL)
16325 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016326 if (check_restricted() || check_secure())
16327 return;
16328# ifdef FEAT_X11
16329 if (check_connection() == FAIL)
16330 return;
16331# endif
16332
16333 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016334 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016335 EMSG(_("E258: Unable to send to client"));
16336 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016338 rettv->vval.v_number = 0;
16339#else
16340 rettv->vval.v_number = -1;
16341#endif
16342}
16343
Bram Moolenaar0d660222005-01-07 21:51:51 +000016344 static void
16345f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016346 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016347 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016348{
16349 char_u *r = NULL;
16350
16351#ifdef FEAT_CLIENTSERVER
16352# ifdef WIN32
16353 r = serverGetVimNames();
16354# else
16355 make_connection();
16356 if (X_DISPLAY != NULL)
16357 r = serverGetVimNames(X_DISPLAY);
16358# endif
16359#endif
16360 rettv->v_type = VAR_STRING;
16361 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362}
16363
16364/*
16365 * "setbufvar()" function
16366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016368f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016369 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016370 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016371{
16372 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016375 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016376 char_u nbuf[NUMBUFLEN];
16377
16378 if (check_restricted() || check_secure())
16379 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016380 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16381 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016382 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016383 varp = &argvars[2];
16384
16385 if (buf != NULL && varname != NULL && varp != NULL)
16386 {
16387 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016388 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016389
16390 if (*varname == '&')
16391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016392 long numval;
16393 char_u *strval;
16394 int error = FALSE;
16395
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016397 numval = get_tv_number_chk(varp, &error);
16398 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016399 if (!error && strval != NULL)
16400 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016401 }
16402 else
16403 {
16404 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16405 if (bufvarname != NULL)
16406 {
16407 STRCPY(bufvarname, "b:");
16408 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016409 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016410 vim_free(bufvarname);
16411 }
16412 }
16413
16414 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417}
16418
16419/*
16420 * "setcmdpos()" function
16421 */
16422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016423f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016424 typval_T *argvars;
16425 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016427 int pos = (int)get_tv_number(&argvars[0]) - 1;
16428
16429 if (pos >= 0)
16430 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016431}
16432
16433/*
16434 * "setline()" function
16435 */
16436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016437f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016438 typval_T *argvars;
16439 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016440{
16441 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016442 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016443 list_T *l = NULL;
16444 listitem_T *li = NULL;
16445 long added = 0;
16446 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016447
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016448 lnum = get_tv_lnum(&argvars[0]);
16449 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016450 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016451 l = argvars[1].vval.v_list;
16452 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016453 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016454 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016455 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016456
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016457 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016458 for (;;)
16459 {
16460 if (l != NULL)
16461 {
16462 /* list argument, get next string */
16463 if (li == NULL)
16464 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016465 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016466 li = li->li_next;
16467 }
16468
16469 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016470 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016471 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016472
16473 /* When coming here from Insert mode, sync undo, so that this can be
16474 * undone separately from what was previously inserted. */
16475 if (u_sync_once == 2)
16476 {
16477 u_sync_once = 1; /* notify that u_sync() was called */
16478 u_sync(TRUE);
16479 }
16480
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016481 if (lnum <= curbuf->b_ml.ml_line_count)
16482 {
16483 /* existing line, replace it */
16484 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16485 {
16486 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016487 if (lnum == curwin->w_cursor.lnum)
16488 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016489 rettv->vval.v_number = 0; /* OK */
16490 }
16491 }
16492 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16493 {
16494 /* lnum is one past the last line, append the line */
16495 ++added;
16496 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16497 rettv->vval.v_number = 0; /* OK */
16498 }
16499
16500 if (l == NULL) /* only one string argument */
16501 break;
16502 ++lnum;
16503 }
16504
16505 if (added > 0)
16506 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016507}
16508
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016509static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16510
Bram Moolenaar071d4272004-06-13 20:20:40 +000016511/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016512 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016513 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016514 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016515set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016516 win_T *wp UNUSED;
16517 typval_T *list_arg UNUSED;
16518 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016519 typval_T *rettv;
16520{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016521#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016522 char_u *act;
16523 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016524#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016525
Bram Moolenaar2641f772005-03-25 21:58:17 +000016526 rettv->vval.v_number = -1;
16527
16528#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016529 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016530 EMSG(_(e_listreq));
16531 else
16532 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016533 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016534
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016535 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016536 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016537 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016538 if (act == NULL)
16539 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016540 if (*act == 'a' || *act == 'r')
16541 action = *act;
16542 }
16543
Bram Moolenaar81484f42012-12-05 15:16:47 +010016544 if (l != NULL && set_errorlist(wp, l, action,
16545 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016546 rettv->vval.v_number = 0;
16547 }
16548#endif
16549}
16550
16551/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016552 * "setloclist()" function
16553 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016554 static void
16555f_setloclist(argvars, rettv)
16556 typval_T *argvars;
16557 typval_T *rettv;
16558{
16559 win_T *win;
16560
16561 rettv->vval.v_number = -1;
16562
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016563 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016564 if (win != NULL)
16565 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16566}
16567
16568/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016569 * "setmatches()" function
16570 */
16571 static void
16572f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016573 typval_T *argvars UNUSED;
16574 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016575{
16576#ifdef FEAT_SEARCH_EXTRA
16577 list_T *l;
16578 listitem_T *li;
16579 dict_T *d;
16580
16581 rettv->vval.v_number = -1;
16582 if (argvars[0].v_type != VAR_LIST)
16583 {
16584 EMSG(_(e_listreq));
16585 return;
16586 }
16587 if ((l = argvars[0].vval.v_list) != NULL)
16588 {
16589
16590 /* To some extent make sure that we are dealing with a list from
16591 * "getmatches()". */
16592 li = l->lv_first;
16593 while (li != NULL)
16594 {
16595 if (li->li_tv.v_type != VAR_DICT
16596 || (d = li->li_tv.vval.v_dict) == NULL)
16597 {
16598 EMSG(_(e_invarg));
16599 return;
16600 }
16601 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16602 && dict_find(d, (char_u *)"pattern", -1) != NULL
16603 && dict_find(d, (char_u *)"priority", -1) != NULL
16604 && dict_find(d, (char_u *)"id", -1) != NULL))
16605 {
16606 EMSG(_(e_invarg));
16607 return;
16608 }
16609 li = li->li_next;
16610 }
16611
16612 clear_matches(curwin);
16613 li = l->lv_first;
16614 while (li != NULL)
16615 {
16616 d = li->li_tv.vval.v_dict;
16617 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16618 get_dict_string(d, (char_u *)"pattern", FALSE),
16619 (int)get_dict_number(d, (char_u *)"priority"),
16620 (int)get_dict_number(d, (char_u *)"id"));
16621 li = li->li_next;
16622 }
16623 rettv->vval.v_number = 0;
16624 }
16625#endif
16626}
16627
16628/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016629 * "setpos()" function
16630 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016631 static void
16632f_setpos(argvars, rettv)
16633 typval_T *argvars;
16634 typval_T *rettv;
16635{
16636 pos_T pos;
16637 int fnum;
16638 char_u *name;
16639
Bram Moolenaar08250432008-02-13 11:42:46 +000016640 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016641 name = get_tv_string_chk(argvars);
16642 if (name != NULL)
16643 {
16644 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16645 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016646 if (--pos.col < 0)
16647 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016648 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016649 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016650 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016651 if (fnum == curbuf->b_fnum)
16652 {
16653 curwin->w_cursor = pos;
16654 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016655 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016656 }
16657 else
16658 EMSG(_(e_invarg));
16659 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016660 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16661 {
16662 /* set mark */
16663 if (setmark_pos(name[1], &pos, fnum) == OK)
16664 rettv->vval.v_number = 0;
16665 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016666 else
16667 EMSG(_(e_invarg));
16668 }
16669 }
16670}
16671
16672/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016673 * "setqflist()" function
16674 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016675 static void
16676f_setqflist(argvars, rettv)
16677 typval_T *argvars;
16678 typval_T *rettv;
16679{
16680 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16681}
16682
16683/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 * "setreg()" function
16685 */
16686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016687f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016688 typval_T *argvars;
16689 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016690{
16691 int regname;
16692 char_u *strregname;
16693 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016694 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695 int append;
16696 char_u yank_type;
16697 long block_len;
16698
16699 block_len = -1;
16700 yank_type = MAUTO;
16701 append = FALSE;
16702
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016703 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016704 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016705
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016706 if (strregname == NULL)
16707 return; /* type error; errmsg already given */
16708 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709 if (regname == 0 || regname == '@')
16710 regname = '"';
16711 else if (regname == '=')
16712 return;
16713
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016714 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016716 stropt = get_tv_string_chk(&argvars[2]);
16717 if (stropt == NULL)
16718 return; /* type error */
16719 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016720 switch (*stropt)
16721 {
16722 case 'a': case 'A': /* append */
16723 append = TRUE;
16724 break;
16725 case 'v': case 'c': /* character-wise selection */
16726 yank_type = MCHAR;
16727 break;
16728 case 'V': case 'l': /* line-wise selection */
16729 yank_type = MLINE;
16730 break;
16731#ifdef FEAT_VISUAL
16732 case 'b': case Ctrl_V: /* block-wise selection */
16733 yank_type = MBLOCK;
16734 if (VIM_ISDIGIT(stropt[1]))
16735 {
16736 ++stropt;
16737 block_len = getdigits(&stropt) - 1;
16738 --stropt;
16739 }
16740 break;
16741#endif
16742 }
16743 }
16744
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016745 strval = get_tv_string_chk(&argvars[1]);
16746 if (strval != NULL)
16747 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016748 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016749 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016750}
16751
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016752/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016753 * "settabvar()" function
16754 */
16755 static void
16756f_settabvar(argvars, rettv)
16757 typval_T *argvars;
16758 typval_T *rettv;
16759{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016760#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016761 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016762 tabpage_T *tp;
16763#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016764 char_u *varname, *tabvarname;
16765 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016766
16767 rettv->vval.v_number = 0;
16768
16769 if (check_restricted() || check_secure())
16770 return;
16771
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016772#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016773 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016774#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016775 varname = get_tv_string_chk(&argvars[1]);
16776 varp = &argvars[2];
16777
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016778 if (varname != NULL && varp != NULL
16779#ifdef FEAT_WINDOWS
16780 && tp != NULL
16781#endif
16782 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016783 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016784#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016785 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016786 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016787#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016788
16789 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16790 if (tabvarname != NULL)
16791 {
16792 STRCPY(tabvarname, "t:");
16793 STRCPY(tabvarname + 2, varname);
16794 set_var(tabvarname, varp, TRUE);
16795 vim_free(tabvarname);
16796 }
16797
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016798#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016799 /* Restore current tabpage */
16800 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016801 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016802#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016803 }
16804}
16805
16806/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016807 * "settabwinvar()" function
16808 */
16809 static void
16810f_settabwinvar(argvars, rettv)
16811 typval_T *argvars;
16812 typval_T *rettv;
16813{
16814 setwinvar(argvars, rettv, 1);
16815}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816
16817/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016818 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016819 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016821f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016822 typval_T *argvars;
16823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016825 setwinvar(argvars, rettv, 0);
16826}
16827
16828/*
16829 * "setwinvar()" and "settabwinvar()" functions
16830 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016831
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016832 static void
16833setwinvar(argvars, rettv, off)
16834 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016835 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016836 int off;
16837{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 win_T *win;
16839#ifdef FEAT_WINDOWS
16840 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016841 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016842#endif
16843 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016844 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016846 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016847
16848 if (check_restricted() || check_secure())
16849 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016850
16851#ifdef FEAT_WINDOWS
16852 if (off == 1)
16853 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16854 else
16855 tp = curtab;
16856#endif
16857 win = find_win_by_nr(&argvars[off], tp);
16858 varname = get_tv_string_chk(&argvars[off + 1]);
16859 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860
16861 if (win != NULL && varname != NULL && varp != NULL)
16862 {
16863#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016864 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016865 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016866#endif
16867
16868 if (*varname == '&')
16869 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016870 long numval;
16871 char_u *strval;
16872 int error = FALSE;
16873
Bram Moolenaar071d4272004-06-13 20:20:40 +000016874 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016875 numval = get_tv_number_chk(varp, &error);
16876 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016877 if (!error && strval != NULL)
16878 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016879 }
16880 else
16881 {
16882 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16883 if (winvarname != NULL)
16884 {
16885 STRCPY(winvarname, "w:");
16886 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016887 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016888 vim_free(winvarname);
16889 }
16890 }
16891
16892#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016893 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894#endif
16895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896}
16897
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016898#ifdef FEAT_CRYPT
16899/*
16900 * "sha256({string})" function
16901 */
16902 static void
16903f_sha256(argvars, rettv)
16904 typval_T *argvars;
16905 typval_T *rettv;
16906{
16907 char_u *p;
16908
16909 p = get_tv_string(&argvars[0]);
16910 rettv->vval.v_string = vim_strsave(
16911 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16912 rettv->v_type = VAR_STRING;
16913}
16914#endif /* FEAT_CRYPT */
16915
Bram Moolenaar071d4272004-06-13 20:20:40 +000016916/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016917 * "shellescape({string})" function
16918 */
16919 static void
16920f_shellescape(argvars, rettv)
16921 typval_T *argvars;
16922 typval_T *rettv;
16923{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016924 rettv->vval.v_string = vim_strsave_shellescape(
16925 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016926 rettv->v_type = VAR_STRING;
16927}
16928
16929/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016930 * shiftwidth() function
16931 */
16932 static void
16933f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016934 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016935 typval_T *rettv;
16936{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010016937 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016938}
16939
16940/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016941 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016942 */
16943 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016944f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016945 typval_T *argvars;
16946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016947{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016948 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949
Bram Moolenaar0d660222005-01-07 21:51:51 +000016950 p = get_tv_string(&argvars[0]);
16951 rettv->vval.v_string = vim_strsave(p);
16952 simplify_filename(rettv->vval.v_string); /* simplify in place */
16953 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016954}
16955
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016956#ifdef FEAT_FLOAT
16957/*
16958 * "sin()" function
16959 */
16960 static void
16961f_sin(argvars, rettv)
16962 typval_T *argvars;
16963 typval_T *rettv;
16964{
16965 float_T f;
16966
16967 rettv->v_type = VAR_FLOAT;
16968 if (get_float_arg(argvars, &f) == OK)
16969 rettv->vval.v_float = sin(f);
16970 else
16971 rettv->vval.v_float = 0.0;
16972}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016973
16974/*
16975 * "sinh()" function
16976 */
16977 static void
16978f_sinh(argvars, rettv)
16979 typval_T *argvars;
16980 typval_T *rettv;
16981{
16982 float_T f;
16983
16984 rettv->v_type = VAR_FLOAT;
16985 if (get_float_arg(argvars, &f) == OK)
16986 rettv->vval.v_float = sinh(f);
16987 else
16988 rettv->vval.v_float = 0.0;
16989}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016990#endif
16991
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992static int
16993#ifdef __BORLANDC__
16994 _RTLENTRYF
16995#endif
16996 item_compare __ARGS((const void *s1, const void *s2));
16997static int
16998#ifdef __BORLANDC__
16999 _RTLENTRYF
17000#endif
17001 item_compare2 __ARGS((const void *s1, const void *s2));
17002
17003static int item_compare_ic;
17004static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017005static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017006static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017007#define ITEM_COMPARE_FAIL 999
17008
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017010 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017011 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017012 static int
17013#ifdef __BORLANDC__
17014_RTLENTRYF
17015#endif
17016item_compare(s1, s2)
17017 const void *s1;
17018 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017019{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017020 char_u *p1, *p2;
17021 char_u *tofree1, *tofree2;
17022 int res;
17023 char_u numbuf1[NUMBUFLEN];
17024 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017026 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
17027 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017028 if (p1 == NULL)
17029 p1 = (char_u *)"";
17030 if (p2 == NULL)
17031 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017032 if (item_compare_ic)
17033 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017035 res = STRCMP(p1, p2);
17036 vim_free(tofree1);
17037 vim_free(tofree2);
17038 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017039}
17040
17041 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017042#ifdef __BORLANDC__
17043_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017045item_compare2(s1, s2)
17046 const void *s1;
17047 const void *s2;
17048{
17049 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017050 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017051 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017052 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017053
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017054 /* shortcut after failure in previous call; compare all items equal */
17055 if (item_compare_func_err)
17056 return 0;
17057
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017058 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17059 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017060 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17061 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017062
17063 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017064 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017065 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17066 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017067 clear_tv(&argv[0]);
17068 clear_tv(&argv[1]);
17069
17070 if (res == FAIL)
17071 res = ITEM_COMPARE_FAIL;
17072 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017073 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17074 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017075 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017076 clear_tv(&rettv);
17077 return res;
17078}
17079
17080/*
17081 * "sort({list})" function
17082 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017083 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017084f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017085 typval_T *argvars;
17086 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087{
Bram Moolenaar33570922005-01-25 22:26:29 +000017088 list_T *l;
17089 listitem_T *li;
17090 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017091 long len;
17092 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017093
Bram Moolenaar0d660222005-01-07 21:51:51 +000017094 if (argvars[0].v_type != VAR_LIST)
17095 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096 else
17097 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017098 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017099 if (l == NULL || tv_check_lock(l->lv_lock,
17100 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017101 return;
17102 rettv->vval.v_list = l;
17103 rettv->v_type = VAR_LIST;
17104 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017105
Bram Moolenaar0d660222005-01-07 21:51:51 +000017106 len = list_len(l);
17107 if (len <= 1)
17108 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017109
Bram Moolenaar0d660222005-01-07 21:51:51 +000017110 item_compare_ic = FALSE;
17111 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017112 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017113 if (argvars[1].v_type != VAR_UNKNOWN)
17114 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017115 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017117 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017118 else
17119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017120 int error = FALSE;
17121
17122 i = get_tv_number_chk(&argvars[1], &error);
17123 if (error)
17124 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017125 if (i == 1)
17126 item_compare_ic = TRUE;
17127 else
17128 item_compare_func = get_tv_string(&argvars[1]);
17129 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017130
17131 if (argvars[2].v_type != VAR_UNKNOWN)
17132 {
17133 /* optional third argument: {dict} */
17134 if (argvars[2].v_type != VAR_DICT)
17135 {
17136 EMSG(_(e_dictreq));
17137 return;
17138 }
17139 item_compare_selfdict = argvars[2].vval.v_dict;
17140 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017142
Bram Moolenaar0d660222005-01-07 21:51:51 +000017143 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017144 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017145 if (ptrs == NULL)
17146 return;
17147 i = 0;
17148 for (li = l->lv_first; li != NULL; li = li->li_next)
17149 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017150
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017151 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017152 /* test the compare function */
17153 if (item_compare_func != NULL
17154 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17155 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017156 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017157 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017158 {
17159 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017160 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017161 item_compare_func == NULL ? item_compare : item_compare2);
17162
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017163 if (!item_compare_func_err)
17164 {
17165 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017166 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017167 l->lv_len = 0;
17168 for (i = 0; i < len; ++i)
17169 list_append(l, ptrs[i]);
17170 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017171 }
17172
17173 vim_free(ptrs);
17174 }
17175}
17176
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017177/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017178 * "soundfold({word})" function
17179 */
17180 static void
17181f_soundfold(argvars, rettv)
17182 typval_T *argvars;
17183 typval_T *rettv;
17184{
17185 char_u *s;
17186
17187 rettv->v_type = VAR_STRING;
17188 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017189#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017190 rettv->vval.v_string = eval_soundfold(s);
17191#else
17192 rettv->vval.v_string = vim_strsave(s);
17193#endif
17194}
17195
17196/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017197 * "spellbadword()" function
17198 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017199 static void
17200f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017201 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017202 typval_T *rettv;
17203{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017204 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017205 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017206 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017207
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017208 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017209 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017210
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017211#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017212 if (argvars[0].v_type == VAR_UNKNOWN)
17213 {
17214 /* Find the start and length of the badly spelled word. */
17215 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17216 if (len != 0)
17217 word = ml_get_cursor();
17218 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017219 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017220 {
17221 char_u *str = get_tv_string_chk(&argvars[0]);
17222 int capcol = -1;
17223
17224 if (str != NULL)
17225 {
17226 /* Check the argument for spelling. */
17227 while (*str != NUL)
17228 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017229 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017230 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017231 {
17232 word = str;
17233 break;
17234 }
17235 str += len;
17236 }
17237 }
17238 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017239#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017240
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017241 list_append_string(rettv->vval.v_list, word, len);
17242 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017243 attr == HLF_SPB ? "bad" :
17244 attr == HLF_SPR ? "rare" :
17245 attr == HLF_SPL ? "local" :
17246 attr == HLF_SPC ? "caps" :
17247 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017248}
17249
17250/*
17251 * "spellsuggest()" function
17252 */
17253 static void
17254f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017255 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017256 typval_T *rettv;
17257{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017258#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017259 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017260 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017261 int maxcount;
17262 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017263 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017264 listitem_T *li;
17265 int need_capital = FALSE;
17266#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017267
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017268 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017269 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017270
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017271#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017272 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017273 {
17274 str = get_tv_string(&argvars[0]);
17275 if (argvars[1].v_type != VAR_UNKNOWN)
17276 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017277 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017278 if (maxcount <= 0)
17279 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017280 if (argvars[2].v_type != VAR_UNKNOWN)
17281 {
17282 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17283 if (typeerr)
17284 return;
17285 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017286 }
17287 else
17288 maxcount = 25;
17289
Bram Moolenaar4770d092006-01-12 23:22:24 +000017290 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017291
17292 for (i = 0; i < ga.ga_len; ++i)
17293 {
17294 str = ((char_u **)ga.ga_data)[i];
17295
17296 li = listitem_alloc();
17297 if (li == NULL)
17298 vim_free(str);
17299 else
17300 {
17301 li->li_tv.v_type = VAR_STRING;
17302 li->li_tv.v_lock = 0;
17303 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017304 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017305 }
17306 }
17307 ga_clear(&ga);
17308 }
17309#endif
17310}
17311
Bram Moolenaar0d660222005-01-07 21:51:51 +000017312 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017313f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017314 typval_T *argvars;
17315 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017316{
17317 char_u *str;
17318 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017319 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017320 regmatch_T regmatch;
17321 char_u patbuf[NUMBUFLEN];
17322 char_u *save_cpo;
17323 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017324 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017325 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017326 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017327
17328 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17329 save_cpo = p_cpo;
17330 p_cpo = (char_u *)"";
17331
17332 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017333 if (argvars[1].v_type != VAR_UNKNOWN)
17334 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017335 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17336 if (pat == NULL)
17337 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017338 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017339 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017340 }
17341 if (pat == NULL || *pat == NUL)
17342 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017343
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017344 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017345 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017346 if (typeerr)
17347 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348
Bram Moolenaar0d660222005-01-07 21:51:51 +000017349 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17350 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017352 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017353 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017354 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017355 if (*str == NUL)
17356 match = FALSE; /* empty item at the end */
17357 else
17358 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017359 if (match)
17360 end = regmatch.startp[0];
17361 else
17362 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017363 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17364 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017365 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017366 if (list_append_string(rettv->vval.v_list, str,
17367 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017368 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017369 }
17370 if (!match)
17371 break;
17372 /* Advance to just after the match. */
17373 if (regmatch.endp[0] > str)
17374 col = 0;
17375 else
17376 {
17377 /* Don't get stuck at the same match. */
17378#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017379 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017380#else
17381 col = 1;
17382#endif
17383 }
17384 str = regmatch.endp[0];
17385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386
Bram Moolenaar473de612013-06-08 18:19:48 +020017387 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389
Bram Moolenaar0d660222005-01-07 21:51:51 +000017390 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017391}
17392
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017393#ifdef FEAT_FLOAT
17394/*
17395 * "sqrt()" function
17396 */
17397 static void
17398f_sqrt(argvars, rettv)
17399 typval_T *argvars;
17400 typval_T *rettv;
17401{
17402 float_T f;
17403
17404 rettv->v_type = VAR_FLOAT;
17405 if (get_float_arg(argvars, &f) == OK)
17406 rettv->vval.v_float = sqrt(f);
17407 else
17408 rettv->vval.v_float = 0.0;
17409}
17410
17411/*
17412 * "str2float()" function
17413 */
17414 static void
17415f_str2float(argvars, rettv)
17416 typval_T *argvars;
17417 typval_T *rettv;
17418{
17419 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17420
17421 if (*p == '+')
17422 p = skipwhite(p + 1);
17423 (void)string2float(p, &rettv->vval.v_float);
17424 rettv->v_type = VAR_FLOAT;
17425}
17426#endif
17427
Bram Moolenaar2c932302006-03-18 21:42:09 +000017428/*
17429 * "str2nr()" function
17430 */
17431 static void
17432f_str2nr(argvars, rettv)
17433 typval_T *argvars;
17434 typval_T *rettv;
17435{
17436 int base = 10;
17437 char_u *p;
17438 long n;
17439
17440 if (argvars[1].v_type != VAR_UNKNOWN)
17441 {
17442 base = get_tv_number(&argvars[1]);
17443 if (base != 8 && base != 10 && base != 16)
17444 {
17445 EMSG(_(e_invarg));
17446 return;
17447 }
17448 }
17449
17450 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017451 if (*p == '+')
17452 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017453 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17454 rettv->vval.v_number = n;
17455}
17456
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457#ifdef HAVE_STRFTIME
17458/*
17459 * "strftime({format}[, {time}])" function
17460 */
17461 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017462f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017463 typval_T *argvars;
17464 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465{
17466 char_u result_buf[256];
17467 struct tm *curtime;
17468 time_t seconds;
17469 char_u *p;
17470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017471 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017473 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017474 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475 seconds = time(NULL);
17476 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017477 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 curtime = localtime(&seconds);
17479 /* MSVC returns NULL for an invalid value of seconds. */
17480 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017481 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482 else
17483 {
17484# ifdef FEAT_MBYTE
17485 vimconv_T conv;
17486 char_u *enc;
17487
17488 conv.vc_type = CONV_NONE;
17489 enc = enc_locale();
17490 convert_setup(&conv, p_enc, enc);
17491 if (conv.vc_type != CONV_NONE)
17492 p = string_convert(&conv, p, NULL);
17493# endif
17494 if (p != NULL)
17495 (void)strftime((char *)result_buf, sizeof(result_buf),
17496 (char *)p, curtime);
17497 else
17498 result_buf[0] = NUL;
17499
17500# ifdef FEAT_MBYTE
17501 if (conv.vc_type != CONV_NONE)
17502 vim_free(p);
17503 convert_setup(&conv, enc, p_enc);
17504 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017505 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506 else
17507# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017508 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017509
17510# ifdef FEAT_MBYTE
17511 /* Release conversion descriptors */
17512 convert_setup(&conv, NULL, NULL);
17513 vim_free(enc);
17514# endif
17515 }
17516}
17517#endif
17518
17519/*
17520 * "stridx()" function
17521 */
17522 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017523f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017524 typval_T *argvars;
17525 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526{
17527 char_u buf[NUMBUFLEN];
17528 char_u *needle;
17529 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017530 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017532 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017534 needle = get_tv_string_chk(&argvars[1]);
17535 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017536 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017537 if (needle == NULL || haystack == NULL)
17538 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539
Bram Moolenaar33570922005-01-25 22:26:29 +000017540 if (argvars[2].v_type != VAR_UNKNOWN)
17541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017542 int error = FALSE;
17543
17544 start_idx = get_tv_number_chk(&argvars[2], &error);
17545 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017546 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017547 if (start_idx >= 0)
17548 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017549 }
17550
17551 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17552 if (pos != NULL)
17553 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554}
17555
17556/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017557 * "string()" function
17558 */
17559 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017560f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017561 typval_T *argvars;
17562 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017563{
17564 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017565 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017566
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017567 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017568 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017569 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017570 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017571 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572}
17573
17574/*
17575 * "strlen()" function
17576 */
17577 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017578f_strlen(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{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017582 rettv->vval.v_number = (varnumber_T)(STRLEN(
17583 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017584}
17585
17586/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017587 * "strchars()" function
17588 */
17589 static void
17590f_strchars(argvars, rettv)
17591 typval_T *argvars;
17592 typval_T *rettv;
17593{
17594 char_u *s = get_tv_string(&argvars[0]);
17595#ifdef FEAT_MBYTE
17596 varnumber_T len = 0;
17597
17598 while (*s != NUL)
17599 {
17600 mb_cptr2char_adv(&s);
17601 ++len;
17602 }
17603 rettv->vval.v_number = len;
17604#else
17605 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17606#endif
17607}
17608
17609/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017610 * "strdisplaywidth()" function
17611 */
17612 static void
17613f_strdisplaywidth(argvars, rettv)
17614 typval_T *argvars;
17615 typval_T *rettv;
17616{
17617 char_u *s = get_tv_string(&argvars[0]);
17618 int col = 0;
17619
17620 if (argvars[1].v_type != VAR_UNKNOWN)
17621 col = get_tv_number(&argvars[1]);
17622
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017623 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017624}
17625
17626/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017627 * "strwidth()" function
17628 */
17629 static void
17630f_strwidth(argvars, rettv)
17631 typval_T *argvars;
17632 typval_T *rettv;
17633{
17634 char_u *s = get_tv_string(&argvars[0]);
17635
17636 rettv->vval.v_number = (varnumber_T)(
17637#ifdef FEAT_MBYTE
17638 mb_string2cells(s, -1)
17639#else
17640 STRLEN(s)
17641#endif
17642 );
17643}
17644
17645/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646 * "strpart()" function
17647 */
17648 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017649f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017650 typval_T *argvars;
17651 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652{
17653 char_u *p;
17654 int n;
17655 int len;
17656 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017657 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017659 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 slen = (int)STRLEN(p);
17661
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017662 n = get_tv_number_chk(&argvars[1], &error);
17663 if (error)
17664 len = 0;
17665 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017666 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667 else
17668 len = slen - n; /* default len: all bytes that are available. */
17669
17670 /*
17671 * Only return the overlap between the specified part and the actual
17672 * string.
17673 */
17674 if (n < 0)
17675 {
17676 len += n;
17677 n = 0;
17678 }
17679 else if (n > slen)
17680 n = slen;
17681 if (len < 0)
17682 len = 0;
17683 else if (n + len > slen)
17684 len = slen - n;
17685
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017686 rettv->v_type = VAR_STRING;
17687 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017688}
17689
17690/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017691 * "strridx()" function
17692 */
17693 static void
17694f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017695 typval_T *argvars;
17696 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017697{
17698 char_u buf[NUMBUFLEN];
17699 char_u *needle;
17700 char_u *haystack;
17701 char_u *rest;
17702 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017703 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017705 needle = get_tv_string_chk(&argvars[1]);
17706 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017707
17708 rettv->vval.v_number = -1;
17709 if (needle == NULL || haystack == NULL)
17710 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017711
17712 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017713 if (argvars[2].v_type != VAR_UNKNOWN)
17714 {
17715 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017716 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017717 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017718 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017719 }
17720 else
17721 end_idx = haystack_len;
17722
Bram Moolenaar0d660222005-01-07 21:51:51 +000017723 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017724 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017725 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017726 lastmatch = haystack + end_idx;
17727 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017728 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017729 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017730 for (rest = haystack; *rest != '\0'; ++rest)
17731 {
17732 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017733 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017734 break;
17735 lastmatch = rest;
17736 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017737 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017738
17739 if (lastmatch == NULL)
17740 rettv->vval.v_number = -1;
17741 else
17742 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17743}
17744
17745/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 * "strtrans()" function
17747 */
17748 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017749f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017750 typval_T *argvars;
17751 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017753 rettv->v_type = VAR_STRING;
17754 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755}
17756
17757/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017758 * "submatch()" function
17759 */
17760 static void
17761f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017762 typval_T *argvars;
17763 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017764{
17765 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017766 rettv->vval.v_string =
17767 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017768}
17769
17770/*
17771 * "substitute()" function
17772 */
17773 static void
17774f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017775 typval_T *argvars;
17776 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017777{
17778 char_u patbuf[NUMBUFLEN];
17779 char_u subbuf[NUMBUFLEN];
17780 char_u flagsbuf[NUMBUFLEN];
17781
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017782 char_u *str = get_tv_string_chk(&argvars[0]);
17783 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17784 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17785 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17786
Bram Moolenaar0d660222005-01-07 21:51:51 +000017787 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017788 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17789 rettv->vval.v_string = NULL;
17790 else
17791 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017792}
17793
17794/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017795 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017798f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017799 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017801{
17802 int id = 0;
17803#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017804 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805 long col;
17806 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017807 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017809 lnum = get_tv_lnum(argvars); /* -1 on type error */
17810 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17811 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017813 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017814 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017815 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816#endif
17817
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017818 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819}
17820
17821/*
17822 * "synIDattr(id, what [, mode])" function
17823 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017824 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017825f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017826 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017827 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017828{
17829 char_u *p = NULL;
17830#ifdef FEAT_SYN_HL
17831 int id;
17832 char_u *what;
17833 char_u *mode;
17834 char_u modebuf[NUMBUFLEN];
17835 int modec;
17836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017837 id = get_tv_number(&argvars[0]);
17838 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017839 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017841 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017842 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017843 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017844 modec = 0; /* replace invalid with current */
17845 }
17846 else
17847 {
17848#ifdef FEAT_GUI
17849 if (gui.in_use)
17850 modec = 'g';
17851 else
17852#endif
17853 if (t_colors > 1)
17854 modec = 'c';
17855 else
17856 modec = 't';
17857 }
17858
17859
17860 switch (TOLOWER_ASC(what[0]))
17861 {
17862 case 'b':
17863 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17864 p = highlight_color(id, what, modec);
17865 else /* bold */
17866 p = highlight_has_attr(id, HL_BOLD, modec);
17867 break;
17868
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017869 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870 p = highlight_color(id, what, modec);
17871 break;
17872
17873 case 'i':
17874 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17875 p = highlight_has_attr(id, HL_INVERSE, modec);
17876 else /* italic */
17877 p = highlight_has_attr(id, HL_ITALIC, modec);
17878 break;
17879
17880 case 'n': /* name */
17881 p = get_highlight_name(NULL, id - 1);
17882 break;
17883
17884 case 'r': /* reverse */
17885 p = highlight_has_attr(id, HL_INVERSE, modec);
17886 break;
17887
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017888 case 's':
17889 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17890 p = highlight_color(id, what, modec);
17891 else /* standout */
17892 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893 break;
17894
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017895 case 'u':
17896 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17897 /* underline */
17898 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17899 else
17900 /* undercurl */
17901 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017902 break;
17903 }
17904
17905 if (p != NULL)
17906 p = vim_strsave(p);
17907#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017908 rettv->v_type = VAR_STRING;
17909 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910}
17911
17912/*
17913 * "synIDtrans(id)" function
17914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017916f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017917 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017919{
17920 int id;
17921
17922#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017923 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017924
17925 if (id > 0)
17926 id = syn_get_final_id(id);
17927 else
17928#endif
17929 id = 0;
17930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017931 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017932}
17933
17934/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017935 * "synconcealed(lnum, col)" function
17936 */
17937 static void
17938f_synconcealed(argvars, rettv)
17939 typval_T *argvars UNUSED;
17940 typval_T *rettv;
17941{
17942#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17943 long lnum;
17944 long col;
17945 int syntax_flags = 0;
17946 int cchar;
17947 int matchid = 0;
17948 char_u str[NUMBUFLEN];
17949#endif
17950
17951 rettv->v_type = VAR_LIST;
17952 rettv->vval.v_list = NULL;
17953
17954#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17955 lnum = get_tv_lnum(argvars); /* -1 on type error */
17956 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17957
17958 vim_memset(str, NUL, sizeof(str));
17959
17960 if (rettv_list_alloc(rettv) != FAIL)
17961 {
17962 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17963 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17964 && curwin->w_p_cole > 0)
17965 {
17966 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17967 syntax_flags = get_syntax_info(&matchid);
17968
17969 /* get the conceal character */
17970 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17971 {
17972 cchar = syn_get_sub_char();
17973 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17974 cchar = lcs_conceal;
17975 if (cchar != NUL)
17976 {
17977# ifdef FEAT_MBYTE
17978 if (has_mbyte)
17979 (*mb_char2bytes)(cchar, str);
17980 else
17981# endif
17982 str[0] = cchar;
17983 }
17984 }
17985 }
17986
17987 list_append_number(rettv->vval.v_list,
17988 (syntax_flags & HL_CONCEAL) != 0);
17989 /* -1 to auto-determine strlen */
17990 list_append_string(rettv->vval.v_list, str, -1);
17991 list_append_number(rettv->vval.v_list, matchid);
17992 }
17993#endif
17994}
17995
17996/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017997 * "synstack(lnum, col)" function
17998 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017999 static void
18000f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018001 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018002 typval_T *rettv;
18003{
18004#ifdef FEAT_SYN_HL
18005 long lnum;
18006 long col;
18007 int i;
18008 int id;
18009#endif
18010
18011 rettv->v_type = VAR_LIST;
18012 rettv->vval.v_list = NULL;
18013
18014#ifdef FEAT_SYN_HL
18015 lnum = get_tv_lnum(argvars); /* -1 on type error */
18016 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
18017
18018 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020018019 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018020 && rettv_list_alloc(rettv) != FAIL)
18021 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000018022 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000018023 for (i = 0; ; ++i)
18024 {
18025 id = syn_get_stack_item(i);
18026 if (id < 0)
18027 break;
18028 if (list_append_number(rettv->vval.v_list, id) == FAIL)
18029 break;
18030 }
18031 }
18032#endif
18033}
18034
18035/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 * "system()" function
18037 */
18038 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018039f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018040 typval_T *argvars;
18041 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018042{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018043 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018044 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018045 char_u *infile = NULL;
18046 char_u buf[NUMBUFLEN];
18047 int err = FALSE;
18048 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018050 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018051 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018052
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018053 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018054 {
18055 /*
18056 * Write the string to a temp file, to be used for input of the shell
18057 * command.
18058 */
18059 if ((infile = vim_tempname('i')) == NULL)
18060 {
18061 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018062 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018063 }
18064
18065 fd = mch_fopen((char *)infile, WRITEBIN);
18066 if (fd == NULL)
18067 {
18068 EMSG2(_(e_notopen), infile);
18069 goto done;
18070 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018071 p = get_tv_string_buf_chk(&argvars[1], buf);
18072 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018073 {
18074 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018075 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018076 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018077 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18078 err = TRUE;
18079 if (fclose(fd) != 0)
18080 err = TRUE;
18081 if (err)
18082 {
18083 EMSG(_("E677: Error writing temp file"));
18084 goto done;
18085 }
18086 }
18087
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018088 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18089 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018090
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091#ifdef USE_CR
18092 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018093 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094 {
18095 char_u *s;
18096
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018097 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018098 {
18099 if (*s == CAR)
18100 *s = NL;
18101 }
18102 }
18103#else
18104# ifdef USE_CRNL
18105 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018106 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018107 {
18108 char_u *s, *d;
18109
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018110 d = res;
18111 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018112 {
18113 if (s[0] == CAR && s[1] == NL)
18114 ++s;
18115 *d++ = *s;
18116 }
18117 *d = NUL;
18118 }
18119# endif
18120#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018121
18122done:
18123 if (infile != NULL)
18124 {
18125 mch_remove(infile);
18126 vim_free(infile);
18127 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018128 rettv->v_type = VAR_STRING;
18129 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018130}
18131
18132/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018133 * "tabpagebuflist()" function
18134 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018135 static void
18136f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018137 typval_T *argvars UNUSED;
18138 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018139{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018140#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018141 tabpage_T *tp;
18142 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018143
18144 if (argvars[0].v_type == VAR_UNKNOWN)
18145 wp = firstwin;
18146 else
18147 {
18148 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18149 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018150 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018151 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018152 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018153 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018154 for (; wp != NULL; wp = wp->w_next)
18155 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018156 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018157 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018158 }
18159#endif
18160}
18161
18162
18163/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018164 * "tabpagenr()" function
18165 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018166 static void
18167f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018168 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018169 typval_T *rettv;
18170{
18171 int nr = 1;
18172#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018173 char_u *arg;
18174
18175 if (argvars[0].v_type != VAR_UNKNOWN)
18176 {
18177 arg = get_tv_string_chk(&argvars[0]);
18178 nr = 0;
18179 if (arg != NULL)
18180 {
18181 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018182 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018183 else
18184 EMSG2(_(e_invexpr2), arg);
18185 }
18186 }
18187 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018188 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018189#endif
18190 rettv->vval.v_number = nr;
18191}
18192
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018193
18194#ifdef FEAT_WINDOWS
18195static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18196
18197/*
18198 * Common code for tabpagewinnr() and winnr().
18199 */
18200 static int
18201get_winnr(tp, argvar)
18202 tabpage_T *tp;
18203 typval_T *argvar;
18204{
18205 win_T *twin;
18206 int nr = 1;
18207 win_T *wp;
18208 char_u *arg;
18209
18210 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18211 if (argvar->v_type != VAR_UNKNOWN)
18212 {
18213 arg = get_tv_string_chk(argvar);
18214 if (arg == NULL)
18215 nr = 0; /* type error; errmsg already given */
18216 else if (STRCMP(arg, "$") == 0)
18217 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18218 else if (STRCMP(arg, "#") == 0)
18219 {
18220 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18221 if (twin == NULL)
18222 nr = 0;
18223 }
18224 else
18225 {
18226 EMSG2(_(e_invexpr2), arg);
18227 nr = 0;
18228 }
18229 }
18230
18231 if (nr > 0)
18232 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18233 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018234 {
18235 if (wp == NULL)
18236 {
18237 /* didn't find it in this tabpage */
18238 nr = 0;
18239 break;
18240 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018241 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018242 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018243 return nr;
18244}
18245#endif
18246
18247/*
18248 * "tabpagewinnr()" function
18249 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018250 static void
18251f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018252 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018253 typval_T *rettv;
18254{
18255 int nr = 1;
18256#ifdef FEAT_WINDOWS
18257 tabpage_T *tp;
18258
18259 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18260 if (tp == NULL)
18261 nr = 0;
18262 else
18263 nr = get_winnr(tp, &argvars[1]);
18264#endif
18265 rettv->vval.v_number = nr;
18266}
18267
18268
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018269/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018270 * "tagfiles()" function
18271 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018272 static void
18273f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018274 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018275 typval_T *rettv;
18276{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018277 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018278 tagname_T tn;
18279 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018280
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018281 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018282 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018283 fname = alloc(MAXPATHL);
18284 if (fname == NULL)
18285 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018286
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018287 for (first = TRUE; ; first = FALSE)
18288 if (get_tagfname(&tn, first, fname) == FAIL
18289 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018290 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018291 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018292 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018293}
18294
18295/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018296 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018297 */
18298 static void
18299f_taglist(argvars, rettv)
18300 typval_T *argvars;
18301 typval_T *rettv;
18302{
18303 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018304
18305 tag_pattern = get_tv_string(&argvars[0]);
18306
18307 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018308 if (*tag_pattern == NUL)
18309 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018310
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018311 if (rettv_list_alloc(rettv) == OK)
18312 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018313}
18314
18315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018316 * "tempname()" function
18317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018319f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018320 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322{
18323 static int x = 'A';
18324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018325 rettv->v_type = VAR_STRING;
18326 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327
18328 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18329 * names. Skip 'I' and 'O', they are used for shell redirection. */
18330 do
18331 {
18332 if (x == 'Z')
18333 x = '0';
18334 else if (x == '9')
18335 x = 'A';
18336 else
18337 {
18338#ifdef EBCDIC
18339 if (x == 'I')
18340 x = 'J';
18341 else if (x == 'R')
18342 x = 'S';
18343 else
18344#endif
18345 ++x;
18346 }
18347 } while (x == 'I' || x == 'O');
18348}
18349
18350/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018351 * "test(list)" function: Just checking the walls...
18352 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018353 static void
18354f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018355 typval_T *argvars UNUSED;
18356 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018357{
18358 /* Used for unit testing. Change the code below to your liking. */
18359#if 0
18360 listitem_T *li;
18361 list_T *l;
18362 char_u *bad, *good;
18363
18364 if (argvars[0].v_type != VAR_LIST)
18365 return;
18366 l = argvars[0].vval.v_list;
18367 if (l == NULL)
18368 return;
18369 li = l->lv_first;
18370 if (li == NULL)
18371 return;
18372 bad = get_tv_string(&li->li_tv);
18373 li = li->li_next;
18374 if (li == NULL)
18375 return;
18376 good = get_tv_string(&li->li_tv);
18377 rettv->vval.v_number = test_edit_score(bad, good);
18378#endif
18379}
18380
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018381#ifdef FEAT_FLOAT
18382/*
18383 * "tan()" function
18384 */
18385 static void
18386f_tan(argvars, rettv)
18387 typval_T *argvars;
18388 typval_T *rettv;
18389{
18390 float_T f;
18391
18392 rettv->v_type = VAR_FLOAT;
18393 if (get_float_arg(argvars, &f) == OK)
18394 rettv->vval.v_float = tan(f);
18395 else
18396 rettv->vval.v_float = 0.0;
18397}
18398
18399/*
18400 * "tanh()" function
18401 */
18402 static void
18403f_tanh(argvars, rettv)
18404 typval_T *argvars;
18405 typval_T *rettv;
18406{
18407 float_T f;
18408
18409 rettv->v_type = VAR_FLOAT;
18410 if (get_float_arg(argvars, &f) == OK)
18411 rettv->vval.v_float = tanh(f);
18412 else
18413 rettv->vval.v_float = 0.0;
18414}
18415#endif
18416
Bram Moolenaard52d9742005-08-21 22:20:28 +000018417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018418 * "tolower(string)" function
18419 */
18420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018421f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018422 typval_T *argvars;
18423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018424{
18425 char_u *p;
18426
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018427 p = vim_strsave(get_tv_string(&argvars[0]));
18428 rettv->v_type = VAR_STRING;
18429 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018430
18431 if (p != NULL)
18432 while (*p != NUL)
18433 {
18434#ifdef FEAT_MBYTE
18435 int l;
18436
18437 if (enc_utf8)
18438 {
18439 int c, lc;
18440
18441 c = utf_ptr2char(p);
18442 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018443 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018444 /* TODO: reallocate string when byte count changes. */
18445 if (utf_char2len(lc) == l)
18446 utf_char2bytes(lc, p);
18447 p += l;
18448 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018449 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450 p += l; /* skip multi-byte character */
18451 else
18452#endif
18453 {
18454 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18455 ++p;
18456 }
18457 }
18458}
18459
18460/*
18461 * "toupper(string)" function
18462 */
18463 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018464f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018465 typval_T *argvars;
18466 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018467{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018468 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018469 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470}
18471
18472/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018473 * "tr(string, fromstr, tostr)" function
18474 */
18475 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018476f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018477 typval_T *argvars;
18478 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018479{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018480 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018481 char_u *fromstr;
18482 char_u *tostr;
18483 char_u *p;
18484#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018485 int inlen;
18486 int fromlen;
18487 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018488 int idx;
18489 char_u *cpstr;
18490 int cplen;
18491 int first = TRUE;
18492#endif
18493 char_u buf[NUMBUFLEN];
18494 char_u buf2[NUMBUFLEN];
18495 garray_T ga;
18496
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018497 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018498 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18499 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018500
18501 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018502 rettv->v_type = VAR_STRING;
18503 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018504 if (fromstr == NULL || tostr == NULL)
18505 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018506 ga_init2(&ga, (int)sizeof(char), 80);
18507
18508#ifdef FEAT_MBYTE
18509 if (!has_mbyte)
18510#endif
18511 /* not multi-byte: fromstr and tostr must be the same length */
18512 if (STRLEN(fromstr) != STRLEN(tostr))
18513 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018514#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018515error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018516#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018517 EMSG2(_(e_invarg2), fromstr);
18518 ga_clear(&ga);
18519 return;
18520 }
18521
18522 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018523 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018524 {
18525#ifdef FEAT_MBYTE
18526 if (has_mbyte)
18527 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018528 inlen = (*mb_ptr2len)(in_str);
18529 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018530 cplen = inlen;
18531 idx = 0;
18532 for (p = fromstr; *p != NUL; p += fromlen)
18533 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018534 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018535 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018536 {
18537 for (p = tostr; *p != NUL; p += tolen)
18538 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018539 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018540 if (idx-- == 0)
18541 {
18542 cplen = tolen;
18543 cpstr = p;
18544 break;
18545 }
18546 }
18547 if (*p == NUL) /* tostr is shorter than fromstr */
18548 goto error;
18549 break;
18550 }
18551 ++idx;
18552 }
18553
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018554 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018555 {
18556 /* Check that fromstr and tostr have the same number of
18557 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018558 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018559 first = FALSE;
18560 for (p = tostr; *p != NUL; p += tolen)
18561 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018562 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018563 --idx;
18564 }
18565 if (idx != 0)
18566 goto error;
18567 }
18568
18569 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018570 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018571 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018572
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018573 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018574 }
18575 else
18576#endif
18577 {
18578 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018579 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018580 if (p != NULL)
18581 ga_append(&ga, tostr[p - fromstr]);
18582 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018583 ga_append(&ga, *in_str);
18584 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018585 }
18586 }
18587
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018588 /* add a terminating NUL */
18589 ga_grow(&ga, 1);
18590 ga_append(&ga, NUL);
18591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018592 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018593}
18594
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018595#ifdef FEAT_FLOAT
18596/*
18597 * "trunc({float})" function
18598 */
18599 static void
18600f_trunc(argvars, rettv)
18601 typval_T *argvars;
18602 typval_T *rettv;
18603{
18604 float_T f;
18605
18606 rettv->v_type = VAR_FLOAT;
18607 if (get_float_arg(argvars, &f) == OK)
18608 /* trunc() is not in C90, use floor() or ceil() instead. */
18609 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18610 else
18611 rettv->vval.v_float = 0.0;
18612}
18613#endif
18614
Bram Moolenaar8299df92004-07-10 09:47:34 +000018615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018616 * "type(expr)" function
18617 */
18618 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018619f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018620 typval_T *argvars;
18621 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018623 int n;
18624
18625 switch (argvars[0].v_type)
18626 {
18627 case VAR_NUMBER: n = 0; break;
18628 case VAR_STRING: n = 1; break;
18629 case VAR_FUNC: n = 2; break;
18630 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018631 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018632#ifdef FEAT_FLOAT
18633 case VAR_FLOAT: n = 5; break;
18634#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018635 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18636 }
18637 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638}
18639
18640/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018641 * "undofile(name)" function
18642 */
18643 static void
18644f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018645 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018646 typval_T *rettv;
18647{
18648 rettv->v_type = VAR_STRING;
18649#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018650 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018651 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018652
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018653 if (*fname == NUL)
18654 {
18655 /* If there is no file name there will be no undo file. */
18656 rettv->vval.v_string = NULL;
18657 }
18658 else
18659 {
18660 char_u *ffname = FullName_save(fname, FALSE);
18661
18662 if (ffname != NULL)
18663 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18664 vim_free(ffname);
18665 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018666 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018667#else
18668 rettv->vval.v_string = NULL;
18669#endif
18670}
18671
18672/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018673 * "undotree()" function
18674 */
18675 static void
18676f_undotree(argvars, rettv)
18677 typval_T *argvars UNUSED;
18678 typval_T *rettv;
18679{
18680 if (rettv_dict_alloc(rettv) == OK)
18681 {
18682 dict_T *dict = rettv->vval.v_dict;
18683 list_T *list;
18684
Bram Moolenaar730cde92010-06-27 05:18:54 +020018685 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018686 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018687 dict_add_nr_str(dict, "save_last",
18688 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018689 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18690 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018691 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018692
18693 list = list_alloc();
18694 if (list != NULL)
18695 {
18696 u_eval_tree(curbuf->b_u_oldhead, list);
18697 dict_add_list(dict, "entries", list);
18698 }
18699 }
18700}
18701
18702/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018703 * "values(dict)" function
18704 */
18705 static void
18706f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018707 typval_T *argvars;
18708 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018709{
18710 dict_list(argvars, rettv, 1);
18711}
18712
18713/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018714 * "virtcol(string)" function
18715 */
18716 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018717f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018718 typval_T *argvars;
18719 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720{
18721 colnr_T vcol = 0;
18722 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018723 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018725 fp = var2fpos(&argvars[0], FALSE, &fnum);
18726 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18727 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018728 {
18729 getvvcol(curwin, fp, NULL, NULL, &vcol);
18730 ++vcol;
18731 }
18732
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018733 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734}
18735
18736/*
18737 * "visualmode()" function
18738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018740f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018741 typval_T *argvars UNUSED;
18742 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743{
18744#ifdef FEAT_VISUAL
18745 char_u str[2];
18746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018747 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 str[0] = curbuf->b_visual_mode_eval;
18749 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018750 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751
18752 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018753 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018754 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755#endif
18756}
18757
18758/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018759 * "wildmenumode()" function
18760 */
18761 static void
18762f_wildmenumode(argvars, rettv)
18763 typval_T *argvars UNUSED;
18764 typval_T *rettv UNUSED;
18765{
18766#ifdef FEAT_WILDMENU
18767 if (wild_menu_showing)
18768 rettv->vval.v_number = 1;
18769#endif
18770}
18771
18772/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773 * "winbufnr(nr)" function
18774 */
18775 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018776f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018777 typval_T *argvars;
18778 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779{
18780 win_T *wp;
18781
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018782 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018783 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018784 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018786 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787}
18788
18789/*
18790 * "wincol()" function
18791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018793f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018794 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018795 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796{
18797 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018798 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799}
18800
18801/*
18802 * "winheight(nr)" function
18803 */
18804 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018805f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018806 typval_T *argvars;
18807 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808{
18809 win_T *wp;
18810
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018811 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018812 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018813 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018815 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018816}
18817
18818/*
18819 * "winline()" function
18820 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018822f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018823 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825{
18826 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018827 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828}
18829
18830/*
18831 * "winnr()" function
18832 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018833 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018834f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018835 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018836 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837{
18838 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018839
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018841 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018843 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844}
18845
18846/*
18847 * "winrestcmd()" function
18848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018850f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018851 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018852 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853{
18854#ifdef FEAT_WINDOWS
18855 win_T *wp;
18856 int winnr = 1;
18857 garray_T ga;
18858 char_u buf[50];
18859
18860 ga_init2(&ga, (int)sizeof(char), 70);
18861 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18862 {
18863 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18864 ga_concat(&ga, buf);
18865# ifdef FEAT_VERTSPLIT
18866 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18867 ga_concat(&ga, buf);
18868# endif
18869 ++winnr;
18870 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018871 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018873 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018875 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018877 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018878}
18879
18880/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018881 * "winrestview()" function
18882 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018883 static void
18884f_winrestview(argvars, rettv)
18885 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018886 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018887{
18888 dict_T *dict;
18889
18890 if (argvars[0].v_type != VAR_DICT
18891 || (dict = argvars[0].vval.v_dict) == NULL)
18892 EMSG(_(e_invarg));
18893 else
18894 {
18895 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18896 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18897#ifdef FEAT_VIRTUALEDIT
18898 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18899#endif
18900 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018901 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018902
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018903 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018904#ifdef FEAT_DIFF
18905 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18906#endif
18907 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18908 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18909
18910 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018911 win_new_height(curwin, curwin->w_height);
18912# ifdef FEAT_VERTSPLIT
18913 win_new_width(curwin, W_WIDTH(curwin));
18914# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018915 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018916
18917 if (curwin->w_topline == 0)
18918 curwin->w_topline = 1;
18919 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18920 curwin->w_topline = curbuf->b_ml.ml_line_count;
18921#ifdef FEAT_DIFF
18922 check_topfill(curwin, TRUE);
18923#endif
18924 }
18925}
18926
18927/*
18928 * "winsaveview()" function
18929 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018930 static void
18931f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018932 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018933 typval_T *rettv;
18934{
18935 dict_T *dict;
18936
Bram Moolenaara800b422010-06-27 01:15:55 +020018937 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018938 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018939 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018940
18941 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18942 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18943#ifdef FEAT_VIRTUALEDIT
18944 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18945#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018946 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018947 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18948
18949 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18950#ifdef FEAT_DIFF
18951 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18952#endif
18953 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18954 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18955}
18956
18957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018958 * "winwidth(nr)" function
18959 */
18960 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018961f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018962 typval_T *argvars;
18963 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018964{
18965 win_T *wp;
18966
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018967 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018968 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018969 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018970 else
18971#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018972 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018973#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018974 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018975#endif
18976}
18977
Bram Moolenaar071d4272004-06-13 20:20:40 +000018978/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018979 * "writefile()" function
18980 */
18981 static void
18982f_writefile(argvars, rettv)
18983 typval_T *argvars;
18984 typval_T *rettv;
18985{
18986 int binary = FALSE;
18987 char_u *fname;
18988 FILE *fd;
18989 listitem_T *li;
18990 char_u *s;
18991 int ret = 0;
18992 int c;
18993
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018994 if (check_restricted() || check_secure())
18995 return;
18996
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018997 if (argvars[0].v_type != VAR_LIST)
18998 {
18999 EMSG2(_(e_listarg), "writefile()");
19000 return;
19001 }
19002 if (argvars[0].vval.v_list == NULL)
19003 return;
19004
19005 if (argvars[2].v_type != VAR_UNKNOWN
19006 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
19007 binary = TRUE;
19008
19009 /* Always open the file in binary mode, library functions have a mind of
19010 * their own about CR-LF conversion. */
19011 fname = get_tv_string(&argvars[1]);
19012 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
19013 {
19014 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
19015 ret = -1;
19016 }
19017 else
19018 {
19019 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
19020 li = li->li_next)
19021 {
19022 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
19023 {
19024 if (*s == '\n')
19025 c = putc(NUL, fd);
19026 else
19027 c = putc(*s, fd);
19028 if (c == EOF)
19029 {
19030 ret = -1;
19031 break;
19032 }
19033 }
19034 if (!binary || li->li_next != NULL)
19035 if (putc('\n', fd) == EOF)
19036 {
19037 ret = -1;
19038 break;
19039 }
19040 if (ret < 0)
19041 {
19042 EMSG(_(e_write));
19043 break;
19044 }
19045 }
19046 fclose(fd);
19047 }
19048
19049 rettv->vval.v_number = ret;
19050}
19051
19052/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019053 * "xor(expr, expr)" function
19054 */
19055 static void
19056f_xor(argvars, rettv)
19057 typval_T *argvars;
19058 typval_T *rettv;
19059{
19060 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19061 ^ get_tv_number_chk(&argvars[1], NULL);
19062}
19063
19064
19065/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019066 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019067 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019068 */
19069 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019070var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019071 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019072 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019073 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019074{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019075 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019076 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019077 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078
Bram Moolenaara5525202006-03-02 22:52:09 +000019079 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019080 if (varp->v_type == VAR_LIST)
19081 {
19082 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019083 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019084 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019085 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019086
19087 l = varp->vval.v_list;
19088 if (l == NULL)
19089 return NULL;
19090
19091 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019092 pos.lnum = list_find_nr(l, 0L, &error);
19093 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019094 return NULL; /* invalid line number */
19095
19096 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019097 pos.col = list_find_nr(l, 1L, &error);
19098 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019099 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019100 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019101
19102 /* We accept "$" for the column number: last column. */
19103 li = list_find(l, 1L);
19104 if (li != NULL && li->li_tv.v_type == VAR_STRING
19105 && li->li_tv.vval.v_string != NULL
19106 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19107 pos.col = len + 1;
19108
Bram Moolenaara5525202006-03-02 22:52:09 +000019109 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019110 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019111 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019112 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019113
Bram Moolenaara5525202006-03-02 22:52:09 +000019114#ifdef FEAT_VIRTUALEDIT
19115 /* Get the virtual offset. Defaults to zero. */
19116 pos.coladd = list_find_nr(l, 2L, &error);
19117 if (error)
19118 pos.coladd = 0;
19119#endif
19120
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019121 return &pos;
19122 }
19123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019124 name = get_tv_string_chk(varp);
19125 if (name == NULL)
19126 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019127 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019128 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019129#ifdef FEAT_VISUAL
19130 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19131 {
19132 if (VIsual_active)
19133 return &VIsual;
19134 return &curwin->w_cursor;
19135 }
19136#endif
19137 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019138 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019139 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019140 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19141 return NULL;
19142 return pp;
19143 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019144
19145#ifdef FEAT_VIRTUALEDIT
19146 pos.coladd = 0;
19147#endif
19148
Bram Moolenaar477933c2007-07-17 14:32:23 +000019149 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019150 {
19151 pos.col = 0;
19152 if (name[1] == '0') /* "w0": first visible line */
19153 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019154 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019155 pos.lnum = curwin->w_topline;
19156 return &pos;
19157 }
19158 else if (name[1] == '$') /* "w$": last visible line */
19159 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019160 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019161 pos.lnum = curwin->w_botline - 1;
19162 return &pos;
19163 }
19164 }
19165 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019166 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019167 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019168 {
19169 pos.lnum = curbuf->b_ml.ml_line_count;
19170 pos.col = 0;
19171 }
19172 else
19173 {
19174 pos.lnum = curwin->w_cursor.lnum;
19175 pos.col = (colnr_T)STRLEN(ml_get_curline());
19176 }
19177 return &pos;
19178 }
19179 return NULL;
19180}
19181
19182/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019183 * Convert list in "arg" into a position and optional file number.
19184 * When "fnump" is NULL there is no file number, only 3 items.
19185 * Note that the column is passed on as-is, the caller may want to decrement
19186 * it to use 1 for the first column.
19187 * Return FAIL when conversion is not possible, doesn't check the position for
19188 * validity.
19189 */
19190 static int
19191list2fpos(arg, posp, fnump)
19192 typval_T *arg;
19193 pos_T *posp;
19194 int *fnump;
19195{
19196 list_T *l = arg->vval.v_list;
19197 long i = 0;
19198 long n;
19199
Bram Moolenaarbde35262006-07-23 20:12:24 +000019200 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19201 * when "fnump" isn't NULL and "coladd" is optional. */
19202 if (arg->v_type != VAR_LIST
19203 || l == NULL
19204 || l->lv_len < (fnump == NULL ? 2 : 3)
19205 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019206 return FAIL;
19207
19208 if (fnump != NULL)
19209 {
19210 n = list_find_nr(l, i++, NULL); /* fnum */
19211 if (n < 0)
19212 return FAIL;
19213 if (n == 0)
19214 n = curbuf->b_fnum; /* current buffer */
19215 *fnump = n;
19216 }
19217
19218 n = list_find_nr(l, i++, NULL); /* lnum */
19219 if (n < 0)
19220 return FAIL;
19221 posp->lnum = n;
19222
19223 n = list_find_nr(l, i++, NULL); /* col */
19224 if (n < 0)
19225 return FAIL;
19226 posp->col = n;
19227
19228#ifdef FEAT_VIRTUALEDIT
19229 n = list_find_nr(l, i, NULL);
19230 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019231 posp->coladd = 0;
19232 else
19233 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019234#endif
19235
19236 return OK;
19237}
19238
19239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019240 * Get the length of an environment variable name.
19241 * Advance "arg" to the first character after the name.
19242 * Return 0 for error.
19243 */
19244 static int
19245get_env_len(arg)
19246 char_u **arg;
19247{
19248 char_u *p;
19249 int len;
19250
19251 for (p = *arg; vim_isIDc(*p); ++p)
19252 ;
19253 if (p == *arg) /* no name found */
19254 return 0;
19255
19256 len = (int)(p - *arg);
19257 *arg = p;
19258 return len;
19259}
19260
19261/*
19262 * Get the length of the name of a function or internal variable.
19263 * "arg" is advanced to the first non-white character after the name.
19264 * Return 0 if something is wrong.
19265 */
19266 static int
19267get_id_len(arg)
19268 char_u **arg;
19269{
19270 char_u *p;
19271 int len;
19272
19273 /* Find the end of the name. */
19274 for (p = *arg; eval_isnamec(*p); ++p)
19275 ;
19276 if (p == *arg) /* no name found */
19277 return 0;
19278
19279 len = (int)(p - *arg);
19280 *arg = skipwhite(p);
19281
19282 return len;
19283}
19284
19285/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019286 * Get the length of the name of a variable or function.
19287 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019289 * Return -1 if curly braces expansion failed.
19290 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291 * If the name contains 'magic' {}'s, expand them and return the
19292 * expanded name in an allocated string via 'alias' - caller must free.
19293 */
19294 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019295get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296 char_u **arg;
19297 char_u **alias;
19298 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019299 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019300{
19301 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019302 char_u *p;
19303 char_u *expr_start;
19304 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305
19306 *alias = NULL; /* default to no alias */
19307
19308 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19309 && (*arg)[2] == (int)KE_SNR)
19310 {
19311 /* hard coded <SNR>, already translated */
19312 *arg += 3;
19313 return get_id_len(arg) + 3;
19314 }
19315 len = eval_fname_script(*arg);
19316 if (len > 0)
19317 {
19318 /* literal "<SID>", "s:" or "<SNR>" */
19319 *arg += len;
19320 }
19321
Bram Moolenaar071d4272004-06-13 20:20:40 +000019322 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019323 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019325 p = find_name_end(*arg, &expr_start, &expr_end,
19326 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019327 if (expr_start != NULL)
19328 {
19329 char_u *temp_string;
19330
19331 if (!evaluate)
19332 {
19333 len += (int)(p - *arg);
19334 *arg = skipwhite(p);
19335 return len;
19336 }
19337
19338 /*
19339 * Include any <SID> etc in the expanded string:
19340 * Thus the -len here.
19341 */
19342 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19343 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019344 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019345 *alias = temp_string;
19346 *arg = skipwhite(p);
19347 return (int)STRLEN(temp_string);
19348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019349
19350 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019351 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019352 EMSG2(_(e_invexpr2), *arg);
19353
19354 return len;
19355}
19356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019357/*
19358 * Find the end of a variable or function name, taking care of magic braces.
19359 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19360 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019361 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019362 * Return a pointer to just after the name. Equal to "arg" if there is no
19363 * valid name.
19364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019365 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019366find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 char_u *arg;
19368 char_u **expr_start;
19369 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019370 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019371{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019372 int mb_nest = 0;
19373 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019374 char_u *p;
19375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019376 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019378 *expr_start = NULL;
19379 *expr_end = NULL;
19380 }
19381
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019382 /* Quick check for valid starting character. */
19383 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19384 return arg;
19385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019386 for (p = arg; *p != NUL
19387 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019388 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019389 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019390 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019391 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019392 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019393 if (*p == '\'')
19394 {
19395 /* skip over 'string' to avoid counting [ and ] inside it. */
19396 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19397 ;
19398 if (*p == NUL)
19399 break;
19400 }
19401 else if (*p == '"')
19402 {
19403 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19404 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19405 if (*p == '\\' && p[1] != NUL)
19406 ++p;
19407 if (*p == NUL)
19408 break;
19409 }
19410
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019411 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019413 if (*p == '[')
19414 ++br_nest;
19415 else if (*p == ']')
19416 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019418
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019419 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019421 if (*p == '{')
19422 {
19423 mb_nest++;
19424 if (expr_start != NULL && *expr_start == NULL)
19425 *expr_start = p;
19426 }
19427 else if (*p == '}')
19428 {
19429 mb_nest--;
19430 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19431 *expr_end = p;
19432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019434 }
19435
19436 return p;
19437}
19438
19439/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019440 * Expands out the 'magic' {}'s in a variable/function name.
19441 * Note that this can call itself recursively, to deal with
19442 * constructs like foo{bar}{baz}{bam}
19443 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19444 * "in_start" ^
19445 * "expr_start" ^
19446 * "expr_end" ^
19447 * "in_end" ^
19448 *
19449 * Returns a new allocated string, which the caller must free.
19450 * Returns NULL for failure.
19451 */
19452 static char_u *
19453make_expanded_name(in_start, expr_start, expr_end, in_end)
19454 char_u *in_start;
19455 char_u *expr_start;
19456 char_u *expr_end;
19457 char_u *in_end;
19458{
19459 char_u c1;
19460 char_u *retval = NULL;
19461 char_u *temp_result;
19462 char_u *nextcmd = NULL;
19463
19464 if (expr_end == NULL || in_end == NULL)
19465 return NULL;
19466 *expr_start = NUL;
19467 *expr_end = NUL;
19468 c1 = *in_end;
19469 *in_end = NUL;
19470
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019471 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019472 if (temp_result != NULL && nextcmd == NULL)
19473 {
19474 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19475 + (in_end - expr_end) + 1));
19476 if (retval != NULL)
19477 {
19478 STRCPY(retval, in_start);
19479 STRCAT(retval, temp_result);
19480 STRCAT(retval, expr_end + 1);
19481 }
19482 }
19483 vim_free(temp_result);
19484
19485 *in_end = c1; /* put char back for error messages */
19486 *expr_start = '{';
19487 *expr_end = '}';
19488
19489 if (retval != NULL)
19490 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019491 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019492 if (expr_start != NULL)
19493 {
19494 /* Further expansion! */
19495 temp_result = make_expanded_name(retval, expr_start,
19496 expr_end, temp_result);
19497 vim_free(retval);
19498 retval = temp_result;
19499 }
19500 }
19501
19502 return retval;
19503}
19504
19505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019507 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508 */
19509 static int
19510eval_isnamec(c)
19511 int c;
19512{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019513 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19514}
19515
19516/*
19517 * Return TRUE if character "c" can be used as the first character in a
19518 * variable or function name (excluding '{' and '}').
19519 */
19520 static int
19521eval_isnamec1(c)
19522 int c;
19523{
19524 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019525}
19526
19527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019528 * Set number v: variable to "val".
19529 */
19530 void
19531set_vim_var_nr(idx, val)
19532 int idx;
19533 long val;
19534{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019535 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019536}
19537
19538/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019539 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540 */
19541 long
19542get_vim_var_nr(idx)
19543 int idx;
19544{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019545 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546}
19547
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019548/*
19549 * Get string v: variable value. Uses a static buffer, can only be used once.
19550 */
19551 char_u *
19552get_vim_var_str(idx)
19553 int idx;
19554{
19555 return get_tv_string(&vimvars[idx].vv_tv);
19556}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019557
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019559 * Get List v: variable value. Caller must take care of reference count when
19560 * needed.
19561 */
19562 list_T *
19563get_vim_var_list(idx)
19564 int idx;
19565{
19566 return vimvars[idx].vv_list;
19567}
19568
19569/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019570 * Set v:char to character "c".
19571 */
19572 void
19573set_vim_var_char(c)
19574 int c;
19575{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019576 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019577
19578#ifdef FEAT_MBYTE
19579 if (has_mbyte)
19580 buf[(*mb_char2bytes)(c, buf)] = NUL;
19581 else
19582#endif
19583 {
19584 buf[0] = c;
19585 buf[1] = NUL;
19586 }
19587 set_vim_var_string(VV_CHAR, buf, -1);
19588}
19589
19590/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019591 * Set v:count to "count" and v:count1 to "count1".
19592 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 */
19594 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019595set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 long count;
19597 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019598 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019600 if (set_prevcount)
19601 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019602 vimvars[VV_COUNT].vv_nr = count;
19603 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604}
19605
19606/*
19607 * Set string v: variable to a copy of "val".
19608 */
19609 void
19610set_vim_var_string(idx, val, len)
19611 int idx;
19612 char_u *val;
19613 int len; /* length of "val" to use or -1 (whole string) */
19614{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019615 /* Need to do this (at least) once, since we can't initialize a union.
19616 * Will always be invoked when "v:progname" is set. */
19617 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19618
Bram Moolenaare9a41262005-01-15 22:18:47 +000019619 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019621 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019623 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019625 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019626}
19627
19628/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019629 * Set List v: variable to "val".
19630 */
19631 void
19632set_vim_var_list(idx, val)
19633 int idx;
19634 list_T *val;
19635{
19636 list_unref(vimvars[idx].vv_list);
19637 vimvars[idx].vv_list = val;
19638 if (val != NULL)
19639 ++val->lv_refcount;
19640}
19641
19642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643 * Set v:register if needed.
19644 */
19645 void
19646set_reg_var(c)
19647 int c;
19648{
19649 char_u regname;
19650
19651 if (c == 0 || c == ' ')
19652 regname = '"';
19653 else
19654 regname = c;
19655 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019656 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657 set_vim_var_string(VV_REG, &regname, 1);
19658}
19659
19660/*
19661 * Get or set v:exception. If "oldval" == NULL, return the current value.
19662 * Otherwise, restore the value to "oldval" and return NULL.
19663 * Must always be called in pairs to save and restore v:exception! Does not
19664 * take care of memory allocations.
19665 */
19666 char_u *
19667v_exception(oldval)
19668 char_u *oldval;
19669{
19670 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019671 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019672
Bram Moolenaare9a41262005-01-15 22:18:47 +000019673 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019674 return NULL;
19675}
19676
19677/*
19678 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19679 * Otherwise, restore the value to "oldval" and return NULL.
19680 * Must always be called in pairs to save and restore v:throwpoint! Does not
19681 * take care of memory allocations.
19682 */
19683 char_u *
19684v_throwpoint(oldval)
19685 char_u *oldval;
19686{
19687 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019688 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689
Bram Moolenaare9a41262005-01-15 22:18:47 +000019690 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019691 return NULL;
19692}
19693
19694#if defined(FEAT_AUTOCMD) || defined(PROTO)
19695/*
19696 * Set v:cmdarg.
19697 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19698 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19699 * Must always be called in pairs!
19700 */
19701 char_u *
19702set_cmdarg(eap, oldarg)
19703 exarg_T *eap;
19704 char_u *oldarg;
19705{
19706 char_u *oldval;
19707 char_u *newval;
19708 unsigned len;
19709
Bram Moolenaare9a41262005-01-15 22:18:47 +000019710 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019711 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019712 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019713 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019714 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019715 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019716 }
19717
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019718 if (eap->force_bin == FORCE_BIN)
19719 len = 6;
19720 else if (eap->force_bin == FORCE_NOBIN)
19721 len = 8;
19722 else
19723 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019724
19725 if (eap->read_edit)
19726 len += 7;
19727
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019728 if (eap->force_ff != 0)
19729 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19730# ifdef FEAT_MBYTE
19731 if (eap->force_enc != 0)
19732 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019733 if (eap->bad_char != 0)
19734 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019735# endif
19736
19737 newval = alloc(len + 1);
19738 if (newval == NULL)
19739 return NULL;
19740
19741 if (eap->force_bin == FORCE_BIN)
19742 sprintf((char *)newval, " ++bin");
19743 else if (eap->force_bin == FORCE_NOBIN)
19744 sprintf((char *)newval, " ++nobin");
19745 else
19746 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019747
19748 if (eap->read_edit)
19749 STRCAT(newval, " ++edit");
19750
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019751 if (eap->force_ff != 0)
19752 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19753 eap->cmd + eap->force_ff);
19754# ifdef FEAT_MBYTE
19755 if (eap->force_enc != 0)
19756 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19757 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019758 if (eap->bad_char == BAD_KEEP)
19759 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19760 else if (eap->bad_char == BAD_DROP)
19761 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19762 else if (eap->bad_char != 0)
19763 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019764# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019765 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019766 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767}
19768#endif
19769
19770/*
19771 * Get the value of internal variable "name".
19772 * Return OK or FAIL.
19773 */
19774 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019775get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776 char_u *name;
19777 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019778 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019779 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780{
19781 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019782 typval_T *tv = NULL;
19783 typval_T atv;
19784 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786
19787 /* truncate the name, so that we can use strcmp() */
19788 cc = name[len];
19789 name[len] = NUL;
19790
19791 /*
19792 * Check for "b:changedtick".
19793 */
19794 if (STRCMP(name, "b:changedtick") == 0)
19795 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019796 atv.v_type = VAR_NUMBER;
19797 atv.vval.v_number = curbuf->b_changedtick;
19798 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019799 }
19800
19801 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019802 * Check for user-defined variables.
19803 */
19804 else
19805 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019806 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019808 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019809 }
19810
Bram Moolenaare9a41262005-01-15 22:18:47 +000019811 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019812 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019813 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019814 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 ret = FAIL;
19816 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019817 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019818 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019819
19820 name[len] = cc;
19821
19822 return ret;
19823}
19824
19825/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019826 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19827 * Also handle function call with Funcref variable: func(expr)
19828 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19829 */
19830 static int
19831handle_subscript(arg, rettv, evaluate, verbose)
19832 char_u **arg;
19833 typval_T *rettv;
19834 int evaluate; /* do more than finding the end */
19835 int verbose; /* give error messages */
19836{
19837 int ret = OK;
19838 dict_T *selfdict = NULL;
19839 char_u *s;
19840 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019841 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019842
19843 while (ret == OK
19844 && (**arg == '['
19845 || (**arg == '.' && rettv->v_type == VAR_DICT)
19846 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19847 && !vim_iswhite(*(*arg - 1)))
19848 {
19849 if (**arg == '(')
19850 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019851 /* need to copy the funcref so that we can clear rettv */
19852 functv = *rettv;
19853 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019854
19855 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019856 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019857 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019858 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19859 &len, evaluate, selfdict);
19860
19861 /* Clear the funcref afterwards, so that deleting it while
19862 * evaluating the arguments is possible (see test55). */
19863 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019864
19865 /* Stop the expression evaluation when immediately aborting on
19866 * error, or when an interrupt occurred or an exception was thrown
19867 * but not caught. */
19868 if (aborting())
19869 {
19870 if (ret == OK)
19871 clear_tv(rettv);
19872 ret = FAIL;
19873 }
19874 dict_unref(selfdict);
19875 selfdict = NULL;
19876 }
19877 else /* **arg == '[' || **arg == '.' */
19878 {
19879 dict_unref(selfdict);
19880 if (rettv->v_type == VAR_DICT)
19881 {
19882 selfdict = rettv->vval.v_dict;
19883 if (selfdict != NULL)
19884 ++selfdict->dv_refcount;
19885 }
19886 else
19887 selfdict = NULL;
19888 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19889 {
19890 clear_tv(rettv);
19891 ret = FAIL;
19892 }
19893 }
19894 }
19895 dict_unref(selfdict);
19896 return ret;
19897}
19898
19899/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019900 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019901 * value).
19902 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019903 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019904alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019905{
Bram Moolenaar33570922005-01-25 22:26:29 +000019906 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019907}
19908
19909/*
19910 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911 * The string "s" must have been allocated, it is consumed.
19912 * Return NULL for out of memory, the variable otherwise.
19913 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019914 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019915alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019916 char_u *s;
19917{
Bram Moolenaar33570922005-01-25 22:26:29 +000019918 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019920 rettv = alloc_tv();
19921 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019923 rettv->v_type = VAR_STRING;
19924 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 }
19926 else
19927 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019928 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929}
19930
19931/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019932 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019934 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019935free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019936 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937{
19938 if (varp != NULL)
19939 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019940 switch (varp->v_type)
19941 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019942 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019943 func_unref(varp->vval.v_string);
19944 /*FALLTHROUGH*/
19945 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019946 vim_free(varp->vval.v_string);
19947 break;
19948 case VAR_LIST:
19949 list_unref(varp->vval.v_list);
19950 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019951 case VAR_DICT:
19952 dict_unref(varp->vval.v_dict);
19953 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019954 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019955#ifdef FEAT_FLOAT
19956 case VAR_FLOAT:
19957#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019958 case VAR_UNKNOWN:
19959 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019960 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019961 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019962 break;
19963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 vim_free(varp);
19965 }
19966}
19967
19968/*
19969 * Free the memory for a variable value and set the value to NULL or 0.
19970 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019971 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019972clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019973 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974{
19975 if (varp != NULL)
19976 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019977 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019979 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019980 func_unref(varp->vval.v_string);
19981 /*FALLTHROUGH*/
19982 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019983 vim_free(varp->vval.v_string);
19984 varp->vval.v_string = NULL;
19985 break;
19986 case VAR_LIST:
19987 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019988 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019989 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019990 case VAR_DICT:
19991 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019992 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019993 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019994 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019995 varp->vval.v_number = 0;
19996 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019997#ifdef FEAT_FLOAT
19998 case VAR_FLOAT:
19999 varp->vval.v_float = 0.0;
20000 break;
20001#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020002 case VAR_UNKNOWN:
20003 break;
20004 default:
20005 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000020006 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020007 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008 }
20009}
20010
20011/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020012 * Set the value of a variable to NULL without freeing items.
20013 */
20014 static void
20015init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020016 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020017{
20018 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020019 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020020}
20021
20022/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020023 * Get the number value of a variable.
20024 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020025 * For incompatible types, return 0.
20026 * get_tv_number_chk() is similar to get_tv_number(), but informs the
20027 * caller of incompatible types: it sets *denote to TRUE if "denote"
20028 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020029 */
20030 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020031get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020032 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020034 int error = FALSE;
20035
20036 return get_tv_number_chk(varp, &error); /* return 0L on error */
20037}
20038
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020039 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020040get_tv_number_chk(varp, denote)
20041 typval_T *varp;
20042 int *denote;
20043{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020044 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020045
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020046 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020047 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020048 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020049 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020050#ifdef FEAT_FLOAT
20051 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020052 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020053 break;
20054#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020055 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020056 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020057 break;
20058 case VAR_STRING:
20059 if (varp->vval.v_string != NULL)
20060 vim_str2nr(varp->vval.v_string, NULL, NULL,
20061 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020062 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020063 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020064 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020065 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020066 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020067 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020068 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020069 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020070 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020071 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020072 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020073 if (denote == NULL) /* useful for values that must be unsigned */
20074 n = -1;
20075 else
20076 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020077 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078}
20079
20080/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020081 * Get the lnum from the first argument.
20082 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020083 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020084 */
20085 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020086get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020087 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020088{
Bram Moolenaar33570922005-01-25 22:26:29 +000020089 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020090 linenr_T lnum;
20091
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020092 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 if (lnum == 0) /* no valid number, try using line() */
20094 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020095 rettv.v_type = VAR_NUMBER;
20096 f_line(argvars, &rettv);
20097 lnum = rettv.vval.v_number;
20098 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099 }
20100 return lnum;
20101}
20102
20103/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020104 * Get the lnum from the first argument.
20105 * Also accepts "$", then "buf" is used.
20106 * Returns 0 on error.
20107 */
20108 static linenr_T
20109get_tv_lnum_buf(argvars, buf)
20110 typval_T *argvars;
20111 buf_T *buf;
20112{
20113 if (argvars[0].v_type == VAR_STRING
20114 && argvars[0].vval.v_string != NULL
20115 && argvars[0].vval.v_string[0] == '$'
20116 && buf != NULL)
20117 return buf->b_ml.ml_line_count;
20118 return get_tv_number_chk(&argvars[0], NULL);
20119}
20120
20121/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020122 * Get the string value of a variable.
20123 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020124 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20125 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020126 * If the String variable has never been set, return an empty string.
20127 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020128 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20129 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 */
20131 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020132get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020133 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020134{
20135 static char_u mybuf[NUMBUFLEN];
20136
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020137 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020138}
20139
20140 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020141get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020142 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020143 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020144{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020145 char_u *res = get_tv_string_buf_chk(varp, buf);
20146
20147 return res != NULL ? res : (char_u *)"";
20148}
20149
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020150 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020151get_tv_string_chk(varp)
20152 typval_T *varp;
20153{
20154 static char_u mybuf[NUMBUFLEN];
20155
20156 return get_tv_string_buf_chk(varp, mybuf);
20157}
20158
20159 static char_u *
20160get_tv_string_buf_chk(varp, buf)
20161 typval_T *varp;
20162 char_u *buf;
20163{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020164 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020166 case VAR_NUMBER:
20167 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20168 return buf;
20169 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020170 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020171 break;
20172 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020173 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020174 break;
20175 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020176 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020177 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020178#ifdef FEAT_FLOAT
20179 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020180 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020181 break;
20182#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020183 case VAR_STRING:
20184 if (varp->vval.v_string != NULL)
20185 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020186 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020187 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020188 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020189 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020190 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020191 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192}
20193
20194/*
20195 * Find variable "name" in the list of variables.
20196 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020197 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020198 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020199 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020200 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020201 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020202find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020203 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020204 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020205{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020207 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208
Bram Moolenaara7043832005-01-21 11:56:39 +000020209 ht = find_var_ht(name, &varname);
20210 if (htp != NULL)
20211 *htp = ht;
20212 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020213 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020214 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020215}
20216
20217/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020218 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020219 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020221 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020222find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020224 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020225 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020226 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020227{
Bram Moolenaar33570922005-01-25 22:26:29 +000020228 hashitem_T *hi;
20229
20230 if (*varname == NUL)
20231 {
20232 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020233 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020234 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020235 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020236 case 'g': return &globvars_var;
20237 case 'v': return &vimvars_var;
20238 case 'b': return &curbuf->b_bufvar;
20239 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020240#ifdef FEAT_WINDOWS
20241 case 't': return &curtab->tp_winvar;
20242#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020243 case 'l': return current_funccal == NULL
20244 ? NULL : &current_funccal->l_vars_var;
20245 case 'a': return current_funccal == NULL
20246 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020247 }
20248 return NULL;
20249 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020250
20251 hi = hash_find(ht, varname);
20252 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020253 {
20254 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020255 * worked find the variable again. Don't auto-load a script if it was
20256 * loaded already, otherwise it would be loaded every time when
20257 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020258 if (ht == &globvarht && !writing)
20259 {
20260 /* Note: script_autoload() may make "hi" invalid. It must either
20261 * be obtained again or not used. */
20262 if (!script_autoload(varname, FALSE) || aborting())
20263 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020264 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020265 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020266 if (HASHITEM_EMPTY(hi))
20267 return NULL;
20268 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020269 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020270}
20271
20272/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020273 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020274 * Set "varname" to the start of name without ':'.
20275 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020276 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020277find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020278 char_u *name;
20279 char_u **varname;
20280{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020281 hashitem_T *hi;
20282
Bram Moolenaar071d4272004-06-13 20:20:40 +000020283 if (name[1] != ':')
20284 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020285 /* The name must not start with a colon or #. */
20286 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020287 return NULL;
20288 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020289
20290 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020291 hi = hash_find(&compat_hashtab, name);
20292 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020293 return &compat_hashtab;
20294
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020296 return &globvarht; /* global variable */
20297 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298 }
20299 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020300 if (*name == 'g') /* global variable */
20301 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020302 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20303 */
20304 if (vim_strchr(name + 2, ':') != NULL
20305 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020306 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020308 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020310 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020311#ifdef FEAT_WINDOWS
20312 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020313 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020314#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020315 if (*name == 'v') /* v: variable */
20316 return &vimvarht;
20317 if (*name == 'a' && current_funccal != NULL) /* function argument */
20318 return &current_funccal->l_avars.dv_hashtab;
20319 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20320 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 if (*name == 's' /* script variable */
20322 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20323 return &SCRIPT_VARS(current_SID);
20324 return NULL;
20325}
20326
20327/*
20328 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020329 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 * Returns NULL when it doesn't exist.
20331 */
20332 char_u *
20333get_var_value(name)
20334 char_u *name;
20335{
Bram Moolenaar33570922005-01-25 22:26:29 +000020336 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020337
Bram Moolenaara7043832005-01-21 11:56:39 +000020338 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 if (v == NULL)
20340 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020341 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342}
20343
20344/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020345 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020346 * sourcing this script and when executing functions defined in the script.
20347 */
20348 void
20349new_script_vars(id)
20350 scid_T id;
20351{
Bram Moolenaara7043832005-01-21 11:56:39 +000020352 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020353 hashtab_T *ht;
20354 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020355
Bram Moolenaar071d4272004-06-13 20:20:40 +000020356 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20357 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020358 /* Re-allocating ga_data means that an ht_array pointing to
20359 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020360 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020361 for (i = 1; i <= ga_scripts.ga_len; ++i)
20362 {
20363 ht = &SCRIPT_VARS(i);
20364 if (ht->ht_mask == HT_INIT_SIZE - 1)
20365 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020366 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020367 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020368 }
20369
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370 while (ga_scripts.ga_len < id)
20371 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020372 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020373 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020374 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020375 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376 }
20377 }
20378}
20379
20380/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020381 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20382 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 */
20384 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020385init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020386 dict_T *dict;
20387 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020388 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020389{
Bram Moolenaar33570922005-01-25 22:26:29 +000020390 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020391 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020392 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020393 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020394 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020395 dict_var->di_tv.vval.v_dict = dict;
20396 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020397 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020398 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20399 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020400}
20401
20402/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020403 * Unreference a dictionary initialized by init_var_dict().
20404 */
20405 void
20406unref_var_dict(dict)
20407 dict_T *dict;
20408{
20409 /* Now the dict needs to be freed if no one else is using it, go back to
20410 * normal reference counting. */
20411 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20412 dict_unref(dict);
20413}
20414
20415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020417 * Frees all allocated variables and the value they contain.
20418 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020419 */
20420 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020421vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020422 hashtab_T *ht;
20423{
20424 vars_clear_ext(ht, TRUE);
20425}
20426
20427/*
20428 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20429 */
20430 static void
20431vars_clear_ext(ht, free_val)
20432 hashtab_T *ht;
20433 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434{
Bram Moolenaara7043832005-01-21 11:56:39 +000020435 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020436 hashitem_T *hi;
20437 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020438
Bram Moolenaar33570922005-01-25 22:26:29 +000020439 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020440 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020441 for (hi = ht->ht_array; todo > 0; ++hi)
20442 {
20443 if (!HASHITEM_EMPTY(hi))
20444 {
20445 --todo;
20446
Bram Moolenaar33570922005-01-25 22:26:29 +000020447 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020448 * ht_array might change then. hash_clear() takes care of it
20449 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020450 v = HI2DI(hi);
20451 if (free_val)
20452 clear_tv(&v->di_tv);
20453 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20454 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020455 }
20456 }
20457 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020458 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459}
20460
Bram Moolenaara7043832005-01-21 11:56:39 +000020461/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020462 * Delete a variable from hashtab "ht" at item "hi".
20463 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020465 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020466delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020467 hashtab_T *ht;
20468 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469{
Bram Moolenaar33570922005-01-25 22:26:29 +000020470 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020471
20472 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020473 clear_tv(&di->di_tv);
20474 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475}
20476
20477/*
20478 * List the value of one internal variable.
20479 */
20480 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020481list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020482 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020483 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020484 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020485{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020486 char_u *tofree;
20487 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020488 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020489
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020490 current_copyID += COPYID_INC;
20491 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020492 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020493 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020494 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495}
20496
Bram Moolenaar071d4272004-06-13 20:20:40 +000020497 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020498list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499 char_u *prefix;
20500 char_u *name;
20501 int type;
20502 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020503 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020504{
Bram Moolenaar31859182007-08-14 20:41:13 +000020505 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20506 msg_start();
20507 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 if (name != NULL) /* "a:" vars don't have a name stored */
20509 msg_puts(name);
20510 msg_putchar(' ');
20511 msg_advance(22);
20512 if (type == VAR_NUMBER)
20513 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020514 else if (type == VAR_FUNC)
20515 msg_putchar('*');
20516 else if (type == VAR_LIST)
20517 {
20518 msg_putchar('[');
20519 if (*string == '[')
20520 ++string;
20521 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020522 else if (type == VAR_DICT)
20523 {
20524 msg_putchar('{');
20525 if (*string == '{')
20526 ++string;
20527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020528 else
20529 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020530
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020532
20533 if (type == VAR_FUNC)
20534 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020535 if (*first)
20536 {
20537 msg_clr_eos();
20538 *first = FALSE;
20539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020540}
20541
20542/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020543 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020544 * If the variable already exists, the value is updated.
20545 * Otherwise the variable is created.
20546 */
20547 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020548set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020549 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020550 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020551 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552{
Bram Moolenaar33570922005-01-25 22:26:29 +000020553 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020555 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020557 ht = find_var_ht(name, &varname);
20558 if (ht == NULL || *varname == NUL)
20559 {
20560 EMSG2(_(e_illvar), name);
20561 return;
20562 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020563 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020564
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020565 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20566 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020567
Bram Moolenaar33570922005-01-25 22:26:29 +000020568 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020569 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020570 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020571 if (var_check_ro(v->di_flags, name)
20572 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020573 return;
20574 if (v->di_tv.v_type != tv->v_type
20575 && !((v->di_tv.v_type == VAR_STRING
20576 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020577 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020578 || tv->v_type == VAR_NUMBER))
20579#ifdef FEAT_FLOAT
20580 && !((v->di_tv.v_type == VAR_NUMBER
20581 || v->di_tv.v_type == VAR_FLOAT)
20582 && (tv->v_type == VAR_NUMBER
20583 || tv->v_type == VAR_FLOAT))
20584#endif
20585 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020586 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020587 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020588 return;
20589 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020590
20591 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020592 * Handle setting internal v: variables separately: we don't change
20593 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020594 */
20595 if (ht == &vimvarht)
20596 {
20597 if (v->di_tv.v_type == VAR_STRING)
20598 {
20599 vim_free(v->di_tv.vval.v_string);
20600 if (copy || tv->v_type != VAR_STRING)
20601 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20602 else
20603 {
20604 /* Take over the string to avoid an extra alloc/free. */
20605 v->di_tv.vval.v_string = tv->vval.v_string;
20606 tv->vval.v_string = NULL;
20607 }
20608 }
20609 else if (v->di_tv.v_type != VAR_NUMBER)
20610 EMSG2(_(e_intern2), "set_var()");
20611 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020612 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020613 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020614 if (STRCMP(varname, "searchforward") == 0)
20615 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20616 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020617 return;
20618 }
20619
20620 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621 }
20622 else /* add a new variable */
20623 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020624 /* Can't add "v:" variable. */
20625 if (ht == &vimvarht)
20626 {
20627 EMSG2(_(e_illvar), name);
20628 return;
20629 }
20630
Bram Moolenaar92124a32005-06-17 22:03:40 +000020631 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020632 if (!valid_varname(varname))
20633 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020634
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020635 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20636 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020637 if (v == NULL)
20638 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020639 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020640 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020641 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020642 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020643 return;
20644 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020645 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020647
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020648 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020649 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020650 else
20651 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020652 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020653 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020654 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656}
20657
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020658/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020659 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020660 * Also give an error message.
20661 */
20662 static int
20663var_check_ro(flags, name)
20664 int flags;
20665 char_u *name;
20666{
20667 if (flags & DI_FLAGS_RO)
20668 {
20669 EMSG2(_(e_readonlyvar), name);
20670 return TRUE;
20671 }
20672 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20673 {
20674 EMSG2(_(e_readonlysbx), name);
20675 return TRUE;
20676 }
20677 return FALSE;
20678}
20679
20680/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020681 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20682 * Also give an error message.
20683 */
20684 static int
20685var_check_fixed(flags, name)
20686 int flags;
20687 char_u *name;
20688{
20689 if (flags & DI_FLAGS_FIX)
20690 {
20691 EMSG2(_("E795: Cannot delete variable %s"), name);
20692 return TRUE;
20693 }
20694 return FALSE;
20695}
20696
20697/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020698 * Check if a funcref is assigned to a valid variable name.
20699 * Return TRUE and give an error if not.
20700 */
20701 static int
20702var_check_func_name(name, new_var)
20703 char_u *name; /* points to start of variable name */
20704 int new_var; /* TRUE when creating the variable */
20705{
20706 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20707 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20708 ? name[2] : name[0]))
20709 {
20710 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20711 name);
20712 return TRUE;
20713 }
20714 /* Don't allow hiding a function. When "v" is not NULL we might be
20715 * assigning another function to the same var, the type is checked
20716 * below. */
20717 if (new_var && function_exists(name))
20718 {
20719 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20720 name);
20721 return TRUE;
20722 }
20723 return FALSE;
20724}
20725
20726/*
20727 * Check if a variable name is valid.
20728 * Return FALSE and give an error if not.
20729 */
20730 static int
20731valid_varname(varname)
20732 char_u *varname;
20733{
20734 char_u *p;
20735
20736 for (p = varname; *p != NUL; ++p)
20737 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20738 && *p != AUTOLOAD_CHAR)
20739 {
20740 EMSG2(_(e_illvar), varname);
20741 return FALSE;
20742 }
20743 return TRUE;
20744}
20745
20746/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020747 * Return TRUE if typeval "tv" is set to be locked (immutable).
20748 * Also give an error message, using "name".
20749 */
20750 static int
20751tv_check_lock(lock, name)
20752 int lock;
20753 char_u *name;
20754{
20755 if (lock & VAR_LOCKED)
20756 {
20757 EMSG2(_("E741: Value is locked: %s"),
20758 name == NULL ? (char_u *)_("Unknown") : name);
20759 return TRUE;
20760 }
20761 if (lock & VAR_FIXED)
20762 {
20763 EMSG2(_("E742: Cannot change value of %s"),
20764 name == NULL ? (char_u *)_("Unknown") : name);
20765 return TRUE;
20766 }
20767 return FALSE;
20768}
20769
20770/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020771 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020772 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020773 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020774 * It is OK for "from" and "to" to point to the same item. This is used to
20775 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020776 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020777 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020778copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020779 typval_T *from;
20780 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020781{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020782 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020783 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020784 switch (from->v_type)
20785 {
20786 case VAR_NUMBER:
20787 to->vval.v_number = from->vval.v_number;
20788 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020789#ifdef FEAT_FLOAT
20790 case VAR_FLOAT:
20791 to->vval.v_float = from->vval.v_float;
20792 break;
20793#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020794 case VAR_STRING:
20795 case VAR_FUNC:
20796 if (from->vval.v_string == NULL)
20797 to->vval.v_string = NULL;
20798 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020799 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020800 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020801 if (from->v_type == VAR_FUNC)
20802 func_ref(to->vval.v_string);
20803 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020804 break;
20805 case VAR_LIST:
20806 if (from->vval.v_list == NULL)
20807 to->vval.v_list = NULL;
20808 else
20809 {
20810 to->vval.v_list = from->vval.v_list;
20811 ++to->vval.v_list->lv_refcount;
20812 }
20813 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020814 case VAR_DICT:
20815 if (from->vval.v_dict == NULL)
20816 to->vval.v_dict = NULL;
20817 else
20818 {
20819 to->vval.v_dict = from->vval.v_dict;
20820 ++to->vval.v_dict->dv_refcount;
20821 }
20822 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020823 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020824 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020825 break;
20826 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020827}
20828
20829/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020830 * Make a copy of an item.
20831 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020832 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20833 * reference to an already copied list/dict can be used.
20834 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020835 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020836 static int
20837item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020838 typval_T *from;
20839 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020840 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020841 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020842{
20843 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020844 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020845
Bram Moolenaar33570922005-01-25 22:26:29 +000020846 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020847 {
20848 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020849 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020850 }
20851 ++recurse;
20852
20853 switch (from->v_type)
20854 {
20855 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020856#ifdef FEAT_FLOAT
20857 case VAR_FLOAT:
20858#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020859 case VAR_STRING:
20860 case VAR_FUNC:
20861 copy_tv(from, to);
20862 break;
20863 case VAR_LIST:
20864 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020865 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020866 if (from->vval.v_list == NULL)
20867 to->vval.v_list = NULL;
20868 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20869 {
20870 /* use the copy made earlier */
20871 to->vval.v_list = from->vval.v_list->lv_copylist;
20872 ++to->vval.v_list->lv_refcount;
20873 }
20874 else
20875 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20876 if (to->vval.v_list == NULL)
20877 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020878 break;
20879 case VAR_DICT:
20880 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020881 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020882 if (from->vval.v_dict == NULL)
20883 to->vval.v_dict = NULL;
20884 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20885 {
20886 /* use the copy made earlier */
20887 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20888 ++to->vval.v_dict->dv_refcount;
20889 }
20890 else
20891 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20892 if (to->vval.v_dict == NULL)
20893 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020894 break;
20895 default:
20896 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020897 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020898 }
20899 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020900 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020901}
20902
20903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 * ":echo expr1 ..." print each argument separated with a space, add a
20905 * newline at the end.
20906 * ":echon expr1 ..." print each argument plain.
20907 */
20908 void
20909ex_echo(eap)
20910 exarg_T *eap;
20911{
20912 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020913 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020914 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020915 char_u *p;
20916 int needclr = TRUE;
20917 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020918 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919
20920 if (eap->skip)
20921 ++emsg_skip;
20922 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20923 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020924 /* If eval1() causes an error message the text from the command may
20925 * still need to be cleared. E.g., "echo 22,44". */
20926 need_clr_eos = needclr;
20927
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020929 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 {
20931 /*
20932 * Report the invalid expression unless the expression evaluation
20933 * has been cancelled due to an aborting error, an interrupt, or an
20934 * exception.
20935 */
20936 if (!aborting())
20937 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020938 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 break;
20940 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020941 need_clr_eos = FALSE;
20942
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943 if (!eap->skip)
20944 {
20945 if (atstart)
20946 {
20947 atstart = FALSE;
20948 /* Call msg_start() after eval1(), evaluating the expression
20949 * may cause a message to appear. */
20950 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020951 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020952 /* Mark the saved text as finishing the line, so that what
20953 * follows is displayed on a new line when scrolling back
20954 * at the more prompt. */
20955 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 }
20959 else if (eap->cmdidx == CMD_echo)
20960 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020961 current_copyID += COPYID_INC;
20962 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020963 if (p != NULL)
20964 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020966 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020968 if (*p != TAB && needclr)
20969 {
20970 /* remove any text still there from the command */
20971 msg_clr_eos();
20972 needclr = FALSE;
20973 }
20974 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020975 }
20976 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020977 {
20978#ifdef FEAT_MBYTE
20979 if (has_mbyte)
20980 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020981 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020982
20983 (void)msg_outtrans_len_attr(p, i, echo_attr);
20984 p += i - 1;
20985 }
20986 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020987#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020988 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020991 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020993 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 arg = skipwhite(arg);
20995 }
20996 eap->nextcmd = check_nextcmd(arg);
20997
20998 if (eap->skip)
20999 --emsg_skip;
21000 else
21001 {
21002 /* remove text that may still be there from the command */
21003 if (needclr)
21004 msg_clr_eos();
21005 if (eap->cmdidx == CMD_echo)
21006 msg_end();
21007 }
21008}
21009
21010/*
21011 * ":echohl {name}".
21012 */
21013 void
21014ex_echohl(eap)
21015 exarg_T *eap;
21016{
21017 int id;
21018
21019 id = syn_name2id(eap->arg);
21020 if (id == 0)
21021 echo_attr = 0;
21022 else
21023 echo_attr = syn_id2attr(id);
21024}
21025
21026/*
21027 * ":execute expr1 ..." execute the result of an expression.
21028 * ":echomsg expr1 ..." Print a message
21029 * ":echoerr expr1 ..." Print an error
21030 * Each gets spaces around each argument and a newline at the end for
21031 * echo commands
21032 */
21033 void
21034ex_execute(eap)
21035 exarg_T *eap;
21036{
21037 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021038 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021039 int ret = OK;
21040 char_u *p;
21041 garray_T ga;
21042 int len;
21043 int save_did_emsg;
21044
21045 ga_init2(&ga, 1, 80);
21046
21047 if (eap->skip)
21048 ++emsg_skip;
21049 while (*arg != NUL && *arg != '|' && *arg != '\n')
21050 {
21051 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021052 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053 {
21054 /*
21055 * Report the invalid expression unless the expression evaluation
21056 * has been cancelled due to an aborting error, an interrupt, or an
21057 * exception.
21058 */
21059 if (!aborting())
21060 EMSG2(_(e_invexpr2), p);
21061 ret = FAIL;
21062 break;
21063 }
21064
21065 if (!eap->skip)
21066 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021067 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021068 len = (int)STRLEN(p);
21069 if (ga_grow(&ga, len + 2) == FAIL)
21070 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021071 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021072 ret = FAIL;
21073 break;
21074 }
21075 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078 ga.ga_len += len;
21079 }
21080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021081 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021082 arg = skipwhite(arg);
21083 }
21084
21085 if (ret != FAIL && ga.ga_data != NULL)
21086 {
21087 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021088 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021089 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021090 out_flush();
21091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021092 else if (eap->cmdidx == CMD_echoerr)
21093 {
21094 /* We don't want to abort following commands, restore did_emsg. */
21095 save_did_emsg = did_emsg;
21096 EMSG((char_u *)ga.ga_data);
21097 if (!force_abort)
21098 did_emsg = save_did_emsg;
21099 }
21100 else if (eap->cmdidx == CMD_execute)
21101 do_cmdline((char_u *)ga.ga_data,
21102 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21103 }
21104
21105 ga_clear(&ga);
21106
21107 if (eap->skip)
21108 --emsg_skip;
21109
21110 eap->nextcmd = check_nextcmd(arg);
21111}
21112
21113/*
21114 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21115 * "arg" points to the "&" or '+' when called, to "option" when returning.
21116 * Returns NULL when no option name found. Otherwise pointer to the char
21117 * after the option name.
21118 */
21119 static char_u *
21120find_option_end(arg, opt_flags)
21121 char_u **arg;
21122 int *opt_flags;
21123{
21124 char_u *p = *arg;
21125
21126 ++p;
21127 if (*p == 'g' && p[1] == ':')
21128 {
21129 *opt_flags = OPT_GLOBAL;
21130 p += 2;
21131 }
21132 else if (*p == 'l' && p[1] == ':')
21133 {
21134 *opt_flags = OPT_LOCAL;
21135 p += 2;
21136 }
21137 else
21138 *opt_flags = 0;
21139
21140 if (!ASCII_ISALPHA(*p))
21141 return NULL;
21142 *arg = p;
21143
21144 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21145 p += 4; /* termcap option */
21146 else
21147 while (ASCII_ISALPHA(*p))
21148 ++p;
21149 return p;
21150}
21151
21152/*
21153 * ":function"
21154 */
21155 void
21156ex_function(eap)
21157 exarg_T *eap;
21158{
21159 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021160 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021161 int j;
21162 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021164 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 char_u *name = NULL;
21166 char_u *p;
21167 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021168 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169 garray_T newargs;
21170 garray_T newlines;
21171 int varargs = FALSE;
21172 int mustend = FALSE;
21173 int flags = 0;
21174 ufunc_T *fp;
21175 int indent;
21176 int nesting;
21177 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021178 dictitem_T *v;
21179 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021180 static int func_nr = 0; /* number for nameless function */
21181 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021182 hashtab_T *ht;
21183 int todo;
21184 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021185 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021186
21187 /*
21188 * ":function" without argument: list functions.
21189 */
21190 if (ends_excmd(*eap->arg))
21191 {
21192 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021193 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021194 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021195 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021196 {
21197 if (!HASHITEM_EMPTY(hi))
21198 {
21199 --todo;
21200 fp = HI2UF(hi);
21201 if (!isdigit(*fp->uf_name))
21202 list_func_head(fp, FALSE);
21203 }
21204 }
21205 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206 eap->nextcmd = check_nextcmd(eap->arg);
21207 return;
21208 }
21209
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021210 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021211 * ":function /pat": list functions matching pattern.
21212 */
21213 if (*eap->arg == '/')
21214 {
21215 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21216 if (!eap->skip)
21217 {
21218 regmatch_T regmatch;
21219
21220 c = *p;
21221 *p = NUL;
21222 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21223 *p = c;
21224 if (regmatch.regprog != NULL)
21225 {
21226 regmatch.rm_ic = p_ic;
21227
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021228 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021229 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21230 {
21231 if (!HASHITEM_EMPTY(hi))
21232 {
21233 --todo;
21234 fp = HI2UF(hi);
21235 if (!isdigit(*fp->uf_name)
21236 && vim_regexec(&regmatch, fp->uf_name, 0))
21237 list_func_head(fp, FALSE);
21238 }
21239 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021240 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021241 }
21242 }
21243 if (*p == '/')
21244 ++p;
21245 eap->nextcmd = check_nextcmd(p);
21246 return;
21247 }
21248
21249 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021250 * Get the function name. There are these situations:
21251 * func normal function name
21252 * "name" == func, "fudi.fd_dict" == NULL
21253 * dict.func new dictionary entry
21254 * "name" == NULL, "fudi.fd_dict" set,
21255 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21256 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021257 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021258 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21259 * dict.func existing dict entry that's not a Funcref
21260 * "name" == NULL, "fudi.fd_dict" set,
21261 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21262 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021264 name = trans_function_name(&p, eap->skip, 0, &fudi);
21265 paren = (vim_strchr(p, '(') != NULL);
21266 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 {
21268 /*
21269 * Return on an invalid expression in braces, unless the expression
21270 * evaluation has been cancelled due to an aborting error, an
21271 * interrupt, or an exception.
21272 */
21273 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021274 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021275 if (!eap->skip && fudi.fd_newkey != NULL)
21276 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021277 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 else
21281 eap->skip = TRUE;
21282 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021283
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 /* An error in a function call during evaluation of an expression in magic
21285 * braces should not cause the function not to be defined. */
21286 saved_did_emsg = did_emsg;
21287 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021288
21289 /*
21290 * ":function func" with only function name: list function.
21291 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021292 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021293 {
21294 if (!ends_excmd(*skipwhite(p)))
21295 {
21296 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021297 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 }
21299 eap->nextcmd = check_nextcmd(p);
21300 if (eap->nextcmd != NULL)
21301 *p = NUL;
21302 if (!eap->skip && !got_int)
21303 {
21304 fp = find_func(name);
21305 if (fp != NULL)
21306 {
21307 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021308 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021310 if (FUNCLINE(fp, j) == NULL)
21311 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 msg_putchar('\n');
21313 msg_outnum((long)(j + 1));
21314 if (j < 9)
21315 msg_putchar(' ');
21316 if (j < 99)
21317 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021318 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021319 out_flush(); /* show a line at a time */
21320 ui_breakcheck();
21321 }
21322 if (!got_int)
21323 {
21324 msg_putchar('\n');
21325 msg_puts((char_u *)" endfunction");
21326 }
21327 }
21328 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021329 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021330 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021331 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021332 }
21333
21334 /*
21335 * ":function name(arg1, arg2)" Define function.
21336 */
21337 p = skipwhite(p);
21338 if (*p != '(')
21339 {
21340 if (!eap->skip)
21341 {
21342 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021343 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344 }
21345 /* attempt to continue by skipping some text */
21346 if (vim_strchr(p, '(') != NULL)
21347 p = vim_strchr(p, '(');
21348 }
21349 p = skipwhite(p + 1);
21350
21351 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21352 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21353
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021354 if (!eap->skip)
21355 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021356 /* Check the name of the function. Unless it's a dictionary function
21357 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021358 if (name != NULL)
21359 arg = name;
21360 else
21361 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021362 if (arg != NULL && (fudi.fd_di == NULL
21363 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021364 {
21365 if (*arg == K_SPECIAL)
21366 j = 3;
21367 else
21368 j = 0;
21369 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21370 : eval_isnamec(arg[j])))
21371 ++j;
21372 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021373 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021374 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021375 /* Disallow using the g: dict. */
21376 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21377 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021378 }
21379
Bram Moolenaar071d4272004-06-13 20:20:40 +000021380 /*
21381 * Isolate the arguments: "arg1, arg2, ...)"
21382 */
21383 while (*p != ')')
21384 {
21385 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21386 {
21387 varargs = TRUE;
21388 p += 3;
21389 mustend = TRUE;
21390 }
21391 else
21392 {
21393 arg = p;
21394 while (ASCII_ISALNUM(*p) || *p == '_')
21395 ++p;
21396 if (arg == p || isdigit(*arg)
21397 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21398 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21399 {
21400 if (!eap->skip)
21401 EMSG2(_("E125: Illegal argument: %s"), arg);
21402 break;
21403 }
21404 if (ga_grow(&newargs, 1) == FAIL)
21405 goto erret;
21406 c = *p;
21407 *p = NUL;
21408 arg = vim_strsave(arg);
21409 if (arg == NULL)
21410 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021411
21412 /* Check for duplicate argument name. */
21413 for (i = 0; i < newargs.ga_len; ++i)
21414 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21415 {
21416 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21417 goto erret;
21418 }
21419
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21421 *p = c;
21422 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021423 if (*p == ',')
21424 ++p;
21425 else
21426 mustend = TRUE;
21427 }
21428 p = skipwhite(p);
21429 if (mustend && *p != ')')
21430 {
21431 if (!eap->skip)
21432 EMSG2(_(e_invarg2), eap->arg);
21433 break;
21434 }
21435 }
21436 ++p; /* skip the ')' */
21437
Bram Moolenaare9a41262005-01-15 22:18:47 +000021438 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439 for (;;)
21440 {
21441 p = skipwhite(p);
21442 if (STRNCMP(p, "range", 5) == 0)
21443 {
21444 flags |= FC_RANGE;
21445 p += 5;
21446 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021447 else if (STRNCMP(p, "dict", 4) == 0)
21448 {
21449 flags |= FC_DICT;
21450 p += 4;
21451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 else if (STRNCMP(p, "abort", 5) == 0)
21453 {
21454 flags |= FC_ABORT;
21455 p += 5;
21456 }
21457 else
21458 break;
21459 }
21460
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021461 /* When there is a line break use what follows for the function body.
21462 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21463 if (*p == '\n')
21464 line_arg = p + 1;
21465 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021466 EMSG(_(e_trailing));
21467
21468 /*
21469 * Read the body of the function, until ":endfunction" is found.
21470 */
21471 if (KeyTyped)
21472 {
21473 /* Check if the function already exists, don't let the user type the
21474 * whole function before telling him it doesn't work! For a script we
21475 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021476 if (!eap->skip && !eap->forceit)
21477 {
21478 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21479 EMSG(_(e_funcdict));
21480 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021481 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021482 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021483
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021484 if (!eap->skip && did_emsg)
21485 goto erret;
21486
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487 msg_putchar('\n'); /* don't overwrite the function name */
21488 cmdline_row = msg_row;
21489 }
21490
21491 indent = 2;
21492 nesting = 0;
21493 for (;;)
21494 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021495 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021496 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021497 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021498 saved_wait_return = FALSE;
21499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021501 sourcing_lnum_off = sourcing_lnum;
21502
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021503 if (line_arg != NULL)
21504 {
21505 /* Use eap->arg, split up in parts by line breaks. */
21506 theline = line_arg;
21507 p = vim_strchr(theline, '\n');
21508 if (p == NULL)
21509 line_arg += STRLEN(line_arg);
21510 else
21511 {
21512 *p = NUL;
21513 line_arg = p + 1;
21514 }
21515 }
21516 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517 theline = getcmdline(':', 0L, indent);
21518 else
21519 theline = eap->getline(':', eap->cookie, indent);
21520 if (KeyTyped)
21521 lines_left = Rows - 1;
21522 if (theline == NULL)
21523 {
21524 EMSG(_("E126: Missing :endfunction"));
21525 goto erret;
21526 }
21527
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021528 /* Detect line continuation: sourcing_lnum increased more than one. */
21529 if (sourcing_lnum > sourcing_lnum_off + 1)
21530 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21531 else
21532 sourcing_lnum_off = 0;
21533
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 if (skip_until != NULL)
21535 {
21536 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21537 * don't check for ":endfunc". */
21538 if (STRCMP(theline, skip_until) == 0)
21539 {
21540 vim_free(skip_until);
21541 skip_until = NULL;
21542 }
21543 }
21544 else
21545 {
21546 /* skip ':' and blanks*/
21547 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21548 ;
21549
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021550 /* Check for "endfunction". */
21551 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021552 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021553 if (line_arg == NULL)
21554 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555 break;
21556 }
21557
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021558 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021559 * at "end". */
21560 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21561 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021562 else if (STRNCMP(p, "if", 2) == 0
21563 || STRNCMP(p, "wh", 2) == 0
21564 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021565 || STRNCMP(p, "try", 3) == 0)
21566 indent += 2;
21567
21568 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021569 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021570 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021571 if (*p == '!')
21572 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573 p += eval_fname_script(p);
21574 if (ASCII_ISALPHA(*p))
21575 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021576 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 if (*skipwhite(p) == '(')
21578 {
21579 ++nesting;
21580 indent += 2;
21581 }
21582 }
21583 }
21584
21585 /* Check for ":append" or ":insert". */
21586 p = skip_range(p, NULL);
21587 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21588 || (p[0] == 'i'
21589 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21590 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21591 skip_until = vim_strsave((char_u *)".");
21592
21593 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21594 arg = skipwhite(skiptowhite(p));
21595 if (arg[0] == '<' && arg[1] =='<'
21596 && ((p[0] == 'p' && p[1] == 'y'
21597 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21598 || (p[0] == 'p' && p[1] == 'e'
21599 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21600 || (p[0] == 't' && p[1] == 'c'
21601 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021602 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21603 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21605 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021606 || (p[0] == 'm' && p[1] == 'z'
21607 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 ))
21609 {
21610 /* ":python <<" continues until a dot, like ":append" */
21611 p = skipwhite(arg + 2);
21612 if (*p == NUL)
21613 skip_until = vim_strsave((char_u *)".");
21614 else
21615 skip_until = vim_strsave(p);
21616 }
21617 }
21618
21619 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021620 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021621 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021622 if (line_arg == NULL)
21623 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021624 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021625 }
21626
21627 /* Copy the line to newly allocated memory. get_one_sourceline()
21628 * allocates 250 bytes per line, this saves 80% on average. The cost
21629 * is an extra alloc/free. */
21630 p = vim_strsave(theline);
21631 if (p != NULL)
21632 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021633 if (line_arg == NULL)
21634 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021635 theline = p;
21636 }
21637
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021638 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21639
21640 /* Add NULL lines for continuation lines, so that the line count is
21641 * equal to the index in the growarray. */
21642 while (sourcing_lnum_off-- > 0)
21643 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021644
21645 /* Check for end of eap->arg. */
21646 if (line_arg != NULL && *line_arg == NUL)
21647 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021648 }
21649
21650 /* Don't define the function when skipping commands or when an error was
21651 * detected. */
21652 if (eap->skip || did_emsg)
21653 goto erret;
21654
21655 /*
21656 * If there are no errors, add the function
21657 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021658 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021659 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021660 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021661 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021662 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021663 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021664 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021665 goto erret;
21666 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021667
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021668 fp = find_func(name);
21669 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021671 if (!eap->forceit)
21672 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021673 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021674 goto erret;
21675 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021676 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021677 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021678 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021679 name);
21680 goto erret;
21681 }
21682 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021683 ga_clear_strings(&(fp->uf_args));
21684 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021685 vim_free(name);
21686 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021687 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021688 }
21689 else
21690 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021691 char numbuf[20];
21692
21693 fp = NULL;
21694 if (fudi.fd_newkey == NULL && !eap->forceit)
21695 {
21696 EMSG(_(e_funcdict));
21697 goto erret;
21698 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021699 if (fudi.fd_di == NULL)
21700 {
21701 /* Can't add a function to a locked dictionary */
21702 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21703 goto erret;
21704 }
21705 /* Can't change an existing function if it is locked */
21706 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21707 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021708
21709 /* Give the function a sequential number. Can only be used with a
21710 * Funcref! */
21711 vim_free(name);
21712 sprintf(numbuf, "%d", ++func_nr);
21713 name = vim_strsave((char_u *)numbuf);
21714 if (name == NULL)
21715 goto erret;
21716 }
21717
21718 if (fp == NULL)
21719 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021720 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021721 {
21722 int slen, plen;
21723 char_u *scriptname;
21724
21725 /* Check that the autoload name matches the script name. */
21726 j = FAIL;
21727 if (sourcing_name != NULL)
21728 {
21729 scriptname = autoload_name(name);
21730 if (scriptname != NULL)
21731 {
21732 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021733 plen = (int)STRLEN(p);
21734 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021735 if (slen > plen && fnamecmp(p,
21736 sourcing_name + slen - plen) == 0)
21737 j = OK;
21738 vim_free(scriptname);
21739 }
21740 }
21741 if (j == FAIL)
21742 {
21743 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21744 goto erret;
21745 }
21746 }
21747
21748 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749 if (fp == NULL)
21750 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021751
21752 if (fudi.fd_dict != NULL)
21753 {
21754 if (fudi.fd_di == NULL)
21755 {
21756 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021757 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021758 if (fudi.fd_di == NULL)
21759 {
21760 vim_free(fp);
21761 goto erret;
21762 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021763 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21764 {
21765 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021766 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021767 goto erret;
21768 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021769 }
21770 else
21771 /* overwrite existing dict entry */
21772 clear_tv(&fudi.fd_di->di_tv);
21773 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021774 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021775 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021776 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021777
21778 /* behave like "dict" was used */
21779 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021780 }
21781
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021783 STRCPY(fp->uf_name, name);
21784 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021786 fp->uf_args = newargs;
21787 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021788#ifdef FEAT_PROFILE
21789 fp->uf_tml_count = NULL;
21790 fp->uf_tml_total = NULL;
21791 fp->uf_tml_self = NULL;
21792 fp->uf_profiling = FALSE;
21793 if (prof_def_func())
21794 func_do_profile(fp);
21795#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021796 fp->uf_varargs = varargs;
21797 fp->uf_flags = flags;
21798 fp->uf_calls = 0;
21799 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021800 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021801
21802erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 ga_clear_strings(&newargs);
21804 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021805ret_free:
21806 vim_free(skip_until);
21807 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021808 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021810 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021811}
21812
21813/*
21814 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021815 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021816 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021817 * flags:
21818 * TFN_INT: internal function name OK
21819 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 * Advances "pp" to just after the function name (if no error).
21821 */
21822 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021823trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 char_u **pp;
21825 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021826 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021827 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021829 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830 char_u *start;
21831 char_u *end;
21832 int lead;
21833 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021834 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021835 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021836
21837 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021838 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021839 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021840
21841 /* Check for hard coded <SNR>: already translated function ID (from a user
21842 * command). */
21843 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21844 && (*pp)[2] == (int)KE_SNR)
21845 {
21846 *pp += 3;
21847 len = get_id_len(pp) + 3;
21848 return vim_strnsave(start, len);
21849 }
21850
21851 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21852 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021854 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021855 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021856
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021857 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21858 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021859 if (end == start)
21860 {
21861 if (!skip)
21862 EMSG(_("E129: Function name required"));
21863 goto theend;
21864 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021865 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021866 {
21867 /*
21868 * Report an invalid expression in braces, unless the expression
21869 * evaluation has been cancelled due to an aborting error, an
21870 * interrupt, or an exception.
21871 */
21872 if (!aborting())
21873 {
21874 if (end != NULL)
21875 EMSG2(_(e_invarg2), start);
21876 }
21877 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021878 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021879 goto theend;
21880 }
21881
21882 if (lv.ll_tv != NULL)
21883 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021884 if (fdp != NULL)
21885 {
21886 fdp->fd_dict = lv.ll_dict;
21887 fdp->fd_newkey = lv.ll_newkey;
21888 lv.ll_newkey = NULL;
21889 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021890 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021891 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21892 {
21893 name = vim_strsave(lv.ll_tv->vval.v_string);
21894 *pp = end;
21895 }
21896 else
21897 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021898 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21899 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021900 EMSG(_(e_funcref));
21901 else
21902 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021903 name = NULL;
21904 }
21905 goto theend;
21906 }
21907
21908 if (lv.ll_name == NULL)
21909 {
21910 /* Error found, but continue after the function name. */
21911 *pp = end;
21912 goto theend;
21913 }
21914
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021915 /* Check if the name is a Funcref. If so, use the value. */
21916 if (lv.ll_exp_name != NULL)
21917 {
21918 len = (int)STRLEN(lv.ll_exp_name);
21919 name = deref_func_name(lv.ll_exp_name, &len);
21920 if (name == lv.ll_exp_name)
21921 name = NULL;
21922 }
21923 else
21924 {
21925 len = (int)(end - *pp);
21926 name = deref_func_name(*pp, &len);
21927 if (name == *pp)
21928 name = NULL;
21929 }
21930 if (name != NULL)
21931 {
21932 name = vim_strsave(name);
21933 *pp = end;
21934 goto theend;
21935 }
21936
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021937 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021938 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021939 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021940 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21941 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21942 {
21943 /* When there was "s:" already or the name expanded to get a
21944 * leading "s:" then remove it. */
21945 lv.ll_name += 2;
21946 len -= 2;
21947 lead = 2;
21948 }
21949 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021950 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021951 {
21952 if (lead == 2) /* skip over "s:" */
21953 lv.ll_name += 2;
21954 len = (int)(end - lv.ll_name);
21955 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021956
21957 /*
21958 * Copy the function name to allocated memory.
21959 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21960 * Accept <SNR>123_name() outside a script.
21961 */
21962 if (skip)
21963 lead = 0; /* do nothing */
21964 else if (lead > 0)
21965 {
21966 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021967 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21968 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021969 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021970 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021971 if (current_SID <= 0)
21972 {
21973 EMSG(_(e_usingsid));
21974 goto theend;
21975 }
21976 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21977 lead += (int)STRLEN(sid_buf);
21978 }
21979 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021980 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021981 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021982 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021983 goto theend;
21984 }
21985 name = alloc((unsigned)(len + lead + 1));
21986 if (name != NULL)
21987 {
21988 if (lead > 0)
21989 {
21990 name[0] = K_SPECIAL;
21991 name[1] = KS_EXTRA;
21992 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021993 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021994 STRCPY(name + 3, sid_buf);
21995 }
21996 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21997 name[len + lead] = NUL;
21998 }
21999 *pp = end;
22000
22001theend:
22002 clear_lval(&lv);
22003 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004}
22005
22006/*
22007 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
22008 * Return 2 if "p" starts with "s:".
22009 * Return 0 otherwise.
22010 */
22011 static int
22012eval_fname_script(p)
22013 char_u *p;
22014{
22015 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
22016 || STRNICMP(p + 1, "SNR>", 4) == 0))
22017 return 5;
22018 if (p[0] == 's' && p[1] == ':')
22019 return 2;
22020 return 0;
22021}
22022
22023/*
22024 * Return TRUE if "p" starts with "<SID>" or "s:".
22025 * Only works if eval_fname_script() returned non-zero for "p"!
22026 */
22027 static int
22028eval_fname_sid(p)
22029 char_u *p;
22030{
22031 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
22032}
22033
22034/*
22035 * List the head of the function: "name(arg1, arg2)".
22036 */
22037 static void
22038list_func_head(fp, indent)
22039 ufunc_T *fp;
22040 int indent;
22041{
22042 int j;
22043
22044 msg_start();
22045 if (indent)
22046 MSG_PUTS(" ");
22047 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022048 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049 {
22050 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022051 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 }
22053 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022054 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022055 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022056 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057 {
22058 if (j)
22059 MSG_PUTS(", ");
22060 msg_puts(FUNCARG(fp, j));
22061 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022062 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 {
22064 if (j)
22065 MSG_PUTS(", ");
22066 MSG_PUTS("...");
22067 }
22068 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022069 if (fp->uf_flags & FC_ABORT)
22070 MSG_PUTS(" abort");
22071 if (fp->uf_flags & FC_RANGE)
22072 MSG_PUTS(" range");
22073 if (fp->uf_flags & FC_DICT)
22074 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022075 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022076 if (p_verbose > 0)
22077 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078}
22079
22080/*
22081 * Find a function by name, return pointer to it in ufuncs.
22082 * Return NULL for unknown function.
22083 */
22084 static ufunc_T *
22085find_func(name)
22086 char_u *name;
22087{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022088 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022089
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022090 hi = hash_find(&func_hashtab, name);
22091 if (!HASHITEM_EMPTY(hi))
22092 return HI2UF(hi);
22093 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094}
22095
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022096#if defined(EXITFREE) || defined(PROTO)
22097 void
22098free_all_functions()
22099{
22100 hashitem_T *hi;
22101
22102 /* Need to start all over every time, because func_free() may change the
22103 * hash table. */
22104 while (func_hashtab.ht_used > 0)
22105 for (hi = func_hashtab.ht_array; ; ++hi)
22106 if (!HASHITEM_EMPTY(hi))
22107 {
22108 func_free(HI2UF(hi));
22109 break;
22110 }
22111}
22112#endif
22113
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022114 int
22115translated_function_exists(name)
22116 char_u *name;
22117{
22118 if (builtin_function(name))
22119 return find_internal_func(name) >= 0;
22120 return find_func(name) != NULL;
22121}
22122
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022123/*
22124 * Return TRUE if a function "name" exists.
22125 */
22126 static int
22127function_exists(name)
22128 char_u *name;
22129{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022130 char_u *nm = name;
22131 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022132 int n = FALSE;
22133
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022134 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022135 nm = skipwhite(nm);
22136
22137 /* Only accept "funcname", "funcname ", "funcname (..." and
22138 * "funcname(...", not "funcname!...". */
22139 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022140 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022141 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022142 return n;
22143}
22144
Bram Moolenaara1544c02013-05-30 12:35:52 +020022145 char_u *
22146get_expanded_name(name, check)
22147 char_u *name;
22148 int check;
22149{
22150 char_u *nm = name;
22151 char_u *p;
22152
22153 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22154
22155 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022156 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022157 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022158
Bram Moolenaara1544c02013-05-30 12:35:52 +020022159 vim_free(p);
22160 return NULL;
22161}
22162
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022163/*
22164 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022165 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022166 */
22167 static int
22168builtin_function(name)
22169 char_u *name;
22170{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022171 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22172 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022173}
22174
Bram Moolenaar05159a02005-02-26 23:04:13 +000022175#if defined(FEAT_PROFILE) || defined(PROTO)
22176/*
22177 * Start profiling function "fp".
22178 */
22179 static void
22180func_do_profile(fp)
22181 ufunc_T *fp;
22182{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022183 int len = fp->uf_lines.ga_len;
22184
22185 if (len == 0)
22186 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022187 fp->uf_tm_count = 0;
22188 profile_zero(&fp->uf_tm_self);
22189 profile_zero(&fp->uf_tm_total);
22190 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022191 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022192 if (fp->uf_tml_total == NULL)
22193 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022194 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022195 if (fp->uf_tml_self == NULL)
22196 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022197 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022198 fp->uf_tml_idx = -1;
22199 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22200 || fp->uf_tml_self == NULL)
22201 return; /* out of memory */
22202
22203 fp->uf_profiling = TRUE;
22204}
22205
22206/*
22207 * Dump the profiling results for all functions in file "fd".
22208 */
22209 void
22210func_dump_profile(fd)
22211 FILE *fd;
22212{
22213 hashitem_T *hi;
22214 int todo;
22215 ufunc_T *fp;
22216 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022217 ufunc_T **sorttab;
22218 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022219
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022220 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022221 if (todo == 0)
22222 return; /* nothing to dump */
22223
Bram Moolenaar73830342005-02-28 22:48:19 +000022224 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22225
Bram Moolenaar05159a02005-02-26 23:04:13 +000022226 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22227 {
22228 if (!HASHITEM_EMPTY(hi))
22229 {
22230 --todo;
22231 fp = HI2UF(hi);
22232 if (fp->uf_profiling)
22233 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022234 if (sorttab != NULL)
22235 sorttab[st_len++] = fp;
22236
Bram Moolenaar05159a02005-02-26 23:04:13 +000022237 if (fp->uf_name[0] == K_SPECIAL)
22238 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22239 else
22240 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22241 if (fp->uf_tm_count == 1)
22242 fprintf(fd, "Called 1 time\n");
22243 else
22244 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22245 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22246 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22247 fprintf(fd, "\n");
22248 fprintf(fd, "count total (s) self (s)\n");
22249
22250 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22251 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022252 if (FUNCLINE(fp, i) == NULL)
22253 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022254 prof_func_line(fd, fp->uf_tml_count[i],
22255 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022256 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22257 }
22258 fprintf(fd, "\n");
22259 }
22260 }
22261 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022262
22263 if (sorttab != NULL && st_len > 0)
22264 {
22265 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22266 prof_total_cmp);
22267 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22268 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22269 prof_self_cmp);
22270 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22271 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022272
22273 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022274}
Bram Moolenaar73830342005-02-28 22:48:19 +000022275
22276 static void
22277prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22278 FILE *fd;
22279 ufunc_T **sorttab;
22280 int st_len;
22281 char *title;
22282 int prefer_self; /* when equal print only self time */
22283{
22284 int i;
22285 ufunc_T *fp;
22286
22287 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22288 fprintf(fd, "count total (s) self (s) function\n");
22289 for (i = 0; i < 20 && i < st_len; ++i)
22290 {
22291 fp = sorttab[i];
22292 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22293 prefer_self);
22294 if (fp->uf_name[0] == K_SPECIAL)
22295 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22296 else
22297 fprintf(fd, " %s()\n", fp->uf_name);
22298 }
22299 fprintf(fd, "\n");
22300}
22301
22302/*
22303 * Print the count and times for one function or function line.
22304 */
22305 static void
22306prof_func_line(fd, count, total, self, prefer_self)
22307 FILE *fd;
22308 int count;
22309 proftime_T *total;
22310 proftime_T *self;
22311 int prefer_self; /* when equal print only self time */
22312{
22313 if (count > 0)
22314 {
22315 fprintf(fd, "%5d ", count);
22316 if (prefer_self && profile_equal(total, self))
22317 fprintf(fd, " ");
22318 else
22319 fprintf(fd, "%s ", profile_msg(total));
22320 if (!prefer_self && profile_equal(total, self))
22321 fprintf(fd, " ");
22322 else
22323 fprintf(fd, "%s ", profile_msg(self));
22324 }
22325 else
22326 fprintf(fd, " ");
22327}
22328
22329/*
22330 * Compare function for total time sorting.
22331 */
22332 static int
22333#ifdef __BORLANDC__
22334_RTLENTRYF
22335#endif
22336prof_total_cmp(s1, s2)
22337 const void *s1;
22338 const void *s2;
22339{
22340 ufunc_T *p1, *p2;
22341
22342 p1 = *(ufunc_T **)s1;
22343 p2 = *(ufunc_T **)s2;
22344 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22345}
22346
22347/*
22348 * Compare function for self time sorting.
22349 */
22350 static int
22351#ifdef __BORLANDC__
22352_RTLENTRYF
22353#endif
22354prof_self_cmp(s1, s2)
22355 const void *s1;
22356 const void *s2;
22357{
22358 ufunc_T *p1, *p2;
22359
22360 p1 = *(ufunc_T **)s1;
22361 p2 = *(ufunc_T **)s2;
22362 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22363}
22364
Bram Moolenaar05159a02005-02-26 23:04:13 +000022365#endif
22366
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022367/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022368 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022369 * Return TRUE if a package was loaded.
22370 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022371 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022372script_autoload(name, reload)
22373 char_u *name;
22374 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022375{
22376 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022377 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022378 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022379 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022380
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022381 /* Return quickly when autoload disabled. */
22382 if (no_autoload)
22383 return FALSE;
22384
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022385 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022386 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022387 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022388 return FALSE;
22389
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022390 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022391
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022392 /* Find the name in the list of previously loaded package names. Skip
22393 * "autoload/", it's always the same. */
22394 for (i = 0; i < ga_loaded.ga_len; ++i)
22395 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22396 break;
22397 if (!reload && i < ga_loaded.ga_len)
22398 ret = FALSE; /* was loaded already */
22399 else
22400 {
22401 /* Remember the name if it wasn't loaded already. */
22402 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22403 {
22404 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22405 tofree = NULL;
22406 }
22407
22408 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022409 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022410 ret = TRUE;
22411 }
22412
22413 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022414 return ret;
22415}
22416
22417/*
22418 * Return the autoload script name for a function or variable name.
22419 * Returns NULL when out of memory.
22420 */
22421 static char_u *
22422autoload_name(name)
22423 char_u *name;
22424{
22425 char_u *p;
22426 char_u *scriptname;
22427
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022428 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022429 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22430 if (scriptname == NULL)
22431 return FALSE;
22432 STRCPY(scriptname, "autoload/");
22433 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022434 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022435 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022436 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022437 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022438 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022439}
22440
Bram Moolenaar071d4272004-06-13 20:20:40 +000022441#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22442
22443/*
22444 * Function given to ExpandGeneric() to obtain the list of user defined
22445 * function names.
22446 */
22447 char_u *
22448get_user_func_name(xp, idx)
22449 expand_T *xp;
22450 int idx;
22451{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022452 static long_u done;
22453 static hashitem_T *hi;
22454 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022455
22456 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022458 done = 0;
22459 hi = func_hashtab.ht_array;
22460 }
22461 if (done < func_hashtab.ht_used)
22462 {
22463 if (done++ > 0)
22464 ++hi;
22465 while (HASHITEM_EMPTY(hi))
22466 ++hi;
22467 fp = HI2UF(hi);
22468
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022469 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022470 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022471
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022472 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22473 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474
22475 cat_func_name(IObuff, fp);
22476 if (xp->xp_context != EXPAND_USER_FUNC)
22477 {
22478 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022479 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022480 STRCAT(IObuff, ")");
22481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022482 return IObuff;
22483 }
22484 return NULL;
22485}
22486
22487#endif /* FEAT_CMDL_COMPL */
22488
22489/*
22490 * Copy the function name of "fp" to buffer "buf".
22491 * "buf" must be able to hold the function name plus three bytes.
22492 * Takes care of script-local function names.
22493 */
22494 static void
22495cat_func_name(buf, fp)
22496 char_u *buf;
22497 ufunc_T *fp;
22498{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022499 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 {
22501 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022502 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503 }
22504 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022505 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506}
22507
22508/*
22509 * ":delfunction {name}"
22510 */
22511 void
22512ex_delfunction(eap)
22513 exarg_T *eap;
22514{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022515 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516 char_u *p;
22517 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022518 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519
22520 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022521 name = trans_function_name(&p, eap->skip, 0, &fudi);
22522 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022523 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022524 {
22525 if (fudi.fd_dict != NULL && !eap->skip)
22526 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022527 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022528 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529 if (!ends_excmd(*skipwhite(p)))
22530 {
22531 vim_free(name);
22532 EMSG(_(e_trailing));
22533 return;
22534 }
22535 eap->nextcmd = check_nextcmd(p);
22536 if (eap->nextcmd != NULL)
22537 *p = NUL;
22538
22539 if (!eap->skip)
22540 fp = find_func(name);
22541 vim_free(name);
22542
22543 if (!eap->skip)
22544 {
22545 if (fp == NULL)
22546 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022547 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548 return;
22549 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022550 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022551 {
22552 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22553 return;
22554 }
22555
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022556 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022558 /* Delete the dict item that refers to the function, it will
22559 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022560 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022562 else
22563 func_free(fp);
22564 }
22565}
22566
22567/*
22568 * Free a function and remove it from the list of functions.
22569 */
22570 static void
22571func_free(fp)
22572 ufunc_T *fp;
22573{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022574 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022575
22576 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022577 ga_clear_strings(&(fp->uf_args));
22578 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022579#ifdef FEAT_PROFILE
22580 vim_free(fp->uf_tml_count);
22581 vim_free(fp->uf_tml_total);
22582 vim_free(fp->uf_tml_self);
22583#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022584
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022585 /* remove the function from the function hashtable */
22586 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22587 if (HASHITEM_EMPTY(hi))
22588 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022589 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022590 hash_remove(&func_hashtab, hi);
22591
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022592 vim_free(fp);
22593}
22594
22595/*
22596 * Unreference a Function: decrement the reference count and free it when it
22597 * becomes zero. Only for numbered functions.
22598 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022599 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022600func_unref(name)
22601 char_u *name;
22602{
22603 ufunc_T *fp;
22604
22605 if (name != NULL && isdigit(*name))
22606 {
22607 fp = find_func(name);
22608 if (fp == NULL)
22609 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022610 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022611 {
22612 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022613 * when "uf_calls" becomes zero. */
22614 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022615 func_free(fp);
22616 }
22617 }
22618}
22619
22620/*
22621 * Count a reference to a Function.
22622 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022623 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022624func_ref(name)
22625 char_u *name;
22626{
22627 ufunc_T *fp;
22628
22629 if (name != NULL && isdigit(*name))
22630 {
22631 fp = find_func(name);
22632 if (fp == NULL)
22633 EMSG2(_(e_intern2), "func_ref()");
22634 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022635 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022636 }
22637}
22638
22639/*
22640 * Call a user function.
22641 */
22642 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022643call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 ufunc_T *fp; /* pointer to function */
22645 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022646 typval_T *argvars; /* arguments */
22647 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022648 linenr_T firstline; /* first line of range */
22649 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022650 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651{
Bram Moolenaar33570922005-01-25 22:26:29 +000022652 char_u *save_sourcing_name;
22653 linenr_T save_sourcing_lnum;
22654 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022655 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022656 int save_did_emsg;
22657 static int depth = 0;
22658 dictitem_T *v;
22659 int fixvar_idx = 0; /* index in fixvar[] */
22660 int i;
22661 int ai;
22662 char_u numbuf[NUMBUFLEN];
22663 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022664#ifdef FEAT_PROFILE
22665 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022666 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022667#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022668
22669 /* If depth of calling is getting too high, don't execute the function */
22670 if (depth >= p_mfd)
22671 {
22672 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022673 rettv->v_type = VAR_NUMBER;
22674 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022675 return;
22676 }
22677 ++depth;
22678
22679 line_breakcheck(); /* check for CTRL-C hit */
22680
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022681 fc = (funccall_T *)alloc(sizeof(funccall_T));
22682 fc->caller = current_funccal;
22683 current_funccal = fc;
22684 fc->func = fp;
22685 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022686 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022687 fc->linenr = 0;
22688 fc->returned = FALSE;
22689 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022691 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22692 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693
Bram Moolenaar33570922005-01-25 22:26:29 +000022694 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022695 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022696 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22697 * each argument variable and saves a lot of time.
22698 */
22699 /*
22700 * Init l: variables.
22701 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022702 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022703 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022704 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022705 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22706 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022707 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022708 name = v->di_key;
22709 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022710 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022711 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022712 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022713 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022714 v->di_tv.vval.v_dict = selfdict;
22715 ++selfdict->dv_refcount;
22716 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022717
Bram Moolenaar33570922005-01-25 22:26:29 +000022718 /*
22719 * Init a: variables.
22720 * Set a:0 to "argcount".
22721 * Set a:000 to a list with room for the "..." arguments.
22722 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022723 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022724 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022725 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022726 /* Use "name" to avoid a warning from some compiler that checks the
22727 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022728 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022729 name = v->di_key;
22730 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022731 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022732 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022733 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022734 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022735 v->di_tv.vval.v_list = &fc->l_varlist;
22736 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22737 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22738 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022739
22740 /*
22741 * Set a:firstline to "firstline" and a:lastline to "lastline".
22742 * Set a:name to named arguments.
22743 * Set a:N to the "..." arguments.
22744 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022745 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022746 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022747 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022748 (varnumber_T)lastline);
22749 for (i = 0; i < argcount; ++i)
22750 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022751 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022752 if (ai < 0)
22753 /* named argument a:name */
22754 name = FUNCARG(fp, i);
22755 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022756 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022757 /* "..." argument a:1, a:2, etc. */
22758 sprintf((char *)numbuf, "%d", ai + 1);
22759 name = numbuf;
22760 }
22761 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22762 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022763 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022764 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22765 }
22766 else
22767 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022768 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22769 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022770 if (v == NULL)
22771 break;
22772 v->di_flags = DI_FLAGS_RO;
22773 }
22774 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022775 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022776
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022777 /* Note: the values are copied directly to avoid alloc/free.
22778 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022779 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022780 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022781
22782 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22783 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022784 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22785 fc->l_listitems[ai].li_tv = argvars[i];
22786 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022787 }
22788 }
22789
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 /* Don't redraw while executing the function. */
22791 ++RedrawingDisabled;
22792 save_sourcing_name = sourcing_name;
22793 save_sourcing_lnum = sourcing_lnum;
22794 sourcing_lnum = 1;
22795 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022796 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022797 if (sourcing_name != NULL)
22798 {
22799 if (save_sourcing_name != NULL
22800 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22801 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22802 else
22803 STRCPY(sourcing_name, "function ");
22804 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22805
22806 if (p_verbose >= 12)
22807 {
22808 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022809 verbose_enter_scroll();
22810
Bram Moolenaar555b2802005-05-19 21:08:39 +000022811 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022812 if (p_verbose >= 14)
22813 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022815 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022816 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022817 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022818
22819 msg_puts((char_u *)"(");
22820 for (i = 0; i < argcount; ++i)
22821 {
22822 if (i > 0)
22823 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022824 if (argvars[i].v_type == VAR_NUMBER)
22825 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 else
22827 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022828 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22829 if (s != NULL)
22830 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022831 if (vim_strsize(s) > MSG_BUF_CLEN)
22832 {
22833 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22834 s = buf;
22835 }
22836 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022837 vim_free(tofree);
22838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839 }
22840 }
22841 msg_puts((char_u *)")");
22842 }
22843 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022844
22845 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 --no_wait_return;
22847 }
22848 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022849#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022850 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022851 {
22852 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22853 func_do_profile(fp);
22854 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022855 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022856 {
22857 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022858 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022859 profile_zero(&fp->uf_tm_children);
22860 }
22861 script_prof_save(&wait_start);
22862 }
22863#endif
22864
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022866 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 save_did_emsg = did_emsg;
22868 did_emsg = FALSE;
22869
22870 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022871 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022872 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22873
22874 --RedrawingDisabled;
22875
22876 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022877 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022879 clear_tv(rettv);
22880 rettv->v_type = VAR_NUMBER;
22881 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 }
22883
Bram Moolenaar05159a02005-02-26 23:04:13 +000022884#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022885 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022886 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022887 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022888 profile_end(&call_start);
22889 profile_sub_wait(&wait_start, &call_start);
22890 profile_add(&fp->uf_tm_total, &call_start);
22891 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022892 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022893 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022894 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22895 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022896 }
22897 }
22898#endif
22899
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 /* when being verbose, mention the return value */
22901 if (p_verbose >= 12)
22902 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022903 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022904 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022905
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022907 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022908 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022909 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022910 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022911 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022913 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022914 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022915 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022916 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022917
Bram Moolenaar555b2802005-05-19 21:08:39 +000022918 /* The value may be very long. Skip the middle part, so that we
22919 * have some idea how it starts and ends. smsg() would always
22920 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022921 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022922 if (s != NULL)
22923 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022924 if (vim_strsize(s) > MSG_BUF_CLEN)
22925 {
22926 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22927 s = buf;
22928 }
22929 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022930 vim_free(tofree);
22931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022932 }
22933 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022934
22935 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022936 --no_wait_return;
22937 }
22938
22939 vim_free(sourcing_name);
22940 sourcing_name = save_sourcing_name;
22941 sourcing_lnum = save_sourcing_lnum;
22942 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022943#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022944 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022945 script_prof_restore(&wait_start);
22946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947
22948 if (p_verbose >= 12 && sourcing_name != NULL)
22949 {
22950 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022951 verbose_enter_scroll();
22952
Bram Moolenaar555b2802005-05-19 21:08:39 +000022953 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022954 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022955
22956 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 --no_wait_return;
22958 }
22959
22960 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022961 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022963
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022964 /* If the a:000 list and the l: and a: dicts are not referenced we can
22965 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022966 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22967 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22968 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22969 {
22970 free_funccal(fc, FALSE);
22971 }
22972 else
22973 {
22974 hashitem_T *hi;
22975 listitem_T *li;
22976 int todo;
22977
22978 /* "fc" is still in use. This can happen when returning "a:000" or
22979 * assigning "l:" to a global variable.
22980 * Link "fc" in the list for garbage collection later. */
22981 fc->caller = previous_funccal;
22982 previous_funccal = fc;
22983
22984 /* Make a copy of the a: variables, since we didn't do that above. */
22985 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22986 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22987 {
22988 if (!HASHITEM_EMPTY(hi))
22989 {
22990 --todo;
22991 v = HI2DI(hi);
22992 copy_tv(&v->di_tv, &v->di_tv);
22993 }
22994 }
22995
22996 /* Make a copy of the a:000 items, since we didn't do that above. */
22997 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22998 copy_tv(&li->li_tv, &li->li_tv);
22999 }
23000}
23001
23002/*
23003 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000023004 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023005 */
23006 static int
23007can_free_funccal(fc, copyID)
23008 funccall_T *fc;
23009 int copyID;
23010{
23011 return (fc->l_varlist.lv_copyID != copyID
23012 && fc->l_vars.dv_copyID != copyID
23013 && fc->l_avars.dv_copyID != copyID);
23014}
23015
23016/*
23017 * Free "fc" and what it contains.
23018 */
23019 static void
23020free_funccal(fc, free_val)
23021 funccall_T *fc;
23022 int free_val; /* a: vars were allocated */
23023{
23024 listitem_T *li;
23025
23026 /* The a: variables typevals may not have been allocated, only free the
23027 * allocated variables. */
23028 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
23029
23030 /* free all l: variables */
23031 vars_clear(&fc->l_vars.dv_hashtab);
23032
23033 /* Free the a:000 variables if they were allocated. */
23034 if (free_val)
23035 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23036 clear_tv(&li->li_tv);
23037
23038 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023039}
23040
23041/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023042 * Add a number variable "name" to dict "dp" with value "nr".
23043 */
23044 static void
23045add_nr_var(dp, v, name, nr)
23046 dict_T *dp;
23047 dictitem_T *v;
23048 char *name;
23049 varnumber_T nr;
23050{
23051 STRCPY(v->di_key, name);
23052 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23053 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23054 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023055 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023056 v->di_tv.vval.v_number = nr;
23057}
23058
23059/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023060 * ":return [expr]"
23061 */
23062 void
23063ex_return(eap)
23064 exarg_T *eap;
23065{
23066 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023067 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023068 int returning = FALSE;
23069
23070 if (current_funccal == NULL)
23071 {
23072 EMSG(_("E133: :return not inside a function"));
23073 return;
23074 }
23075
23076 if (eap->skip)
23077 ++emsg_skip;
23078
23079 eap->nextcmd = NULL;
23080 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023081 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 {
23083 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023084 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023086 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023087 }
23088 /* It's safer to return also on error. */
23089 else if (!eap->skip)
23090 {
23091 /*
23092 * Return unless the expression evaluation has been cancelled due to an
23093 * aborting error, an interrupt, or an exception.
23094 */
23095 if (!aborting())
23096 returning = do_return(eap, FALSE, TRUE, NULL);
23097 }
23098
23099 /* When skipping or the return gets pending, advance to the next command
23100 * in this line (!returning). Otherwise, ignore the rest of the line.
23101 * Following lines will be ignored by get_func_line(). */
23102 if (returning)
23103 eap->nextcmd = NULL;
23104 else if (eap->nextcmd == NULL) /* no argument */
23105 eap->nextcmd = check_nextcmd(arg);
23106
23107 if (eap->skip)
23108 --emsg_skip;
23109}
23110
23111/*
23112 * Return from a function. Possibly makes the return pending. Also called
23113 * for a pending return at the ":endtry" or after returning from an extra
23114 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023115 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023116 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117 * FALSE when the return gets pending.
23118 */
23119 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023120do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121 exarg_T *eap;
23122 int reanimate;
23123 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023124 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023125{
23126 int idx;
23127 struct condstack *cstack = eap->cstack;
23128
23129 if (reanimate)
23130 /* Undo the return. */
23131 current_funccal->returned = FALSE;
23132
23133 /*
23134 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23135 * not in its finally clause (which then is to be executed next) is found.
23136 * In this case, make the ":return" pending for execution at the ":endtry".
23137 * Otherwise, return normally.
23138 */
23139 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23140 if (idx >= 0)
23141 {
23142 cstack->cs_pending[idx] = CSTP_RETURN;
23143
23144 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023145 /* A pending return again gets pending. "rettv" points to an
23146 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023148 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 else
23150 {
23151 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023152 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023154 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023156 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023157 {
23158 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023159 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023160 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023161 else
23162 EMSG(_(e_outofmem));
23163 }
23164 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023165 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166
23167 if (reanimate)
23168 {
23169 /* The pending return value could be overwritten by a ":return"
23170 * without argument in a finally clause; reset the default
23171 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023172 current_funccal->rettv->v_type = VAR_NUMBER;
23173 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174 }
23175 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023176 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177 }
23178 else
23179 {
23180 current_funccal->returned = TRUE;
23181
23182 /* If the return is carried out now, store the return value. For
23183 * a return immediately after reanimation, the value is already
23184 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023185 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023187 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023188 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023190 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023191 }
23192 }
23193
23194 return idx < 0;
23195}
23196
23197/*
23198 * Free the variable with a pending return value.
23199 */
23200 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023201discard_pending_return(rettv)
23202 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023203{
Bram Moolenaar33570922005-01-25 22:26:29 +000023204 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205}
23206
23207/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023208 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023209 * is an allocated string. Used by report_pending() for verbose messages.
23210 */
23211 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023212get_return_cmd(rettv)
23213 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023215 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023216 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023217 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023218
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023219 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023220 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023221 if (s == NULL)
23222 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023223
23224 STRCPY(IObuff, ":return ");
23225 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23226 if (STRLEN(s) + 8 >= IOSIZE)
23227 STRCPY(IObuff + IOSIZE - 4, "...");
23228 vim_free(tofree);
23229 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230}
23231
23232/*
23233 * Get next function line.
23234 * Called by do_cmdline() to get the next line.
23235 * Returns allocated string, or NULL for end of function.
23236 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 char_u *
23238get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023239 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023240 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023241 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242{
Bram Moolenaar33570922005-01-25 22:26:29 +000023243 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023244 ufunc_T *fp = fcp->func;
23245 char_u *retval;
23246 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023247
23248 /* If breakpoints have been added/deleted need to check for it. */
23249 if (fcp->dbg_tick != debug_tick)
23250 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023251 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 sourcing_lnum);
23253 fcp->dbg_tick = debug_tick;
23254 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023255#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023256 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023257 func_line_end(cookie);
23258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023259
Bram Moolenaar05159a02005-02-26 23:04:13 +000023260 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023261 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23262 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023263 retval = NULL;
23264 else
23265 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023266 /* Skip NULL lines (continuation lines). */
23267 while (fcp->linenr < gap->ga_len
23268 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23269 ++fcp->linenr;
23270 if (fcp->linenr >= gap->ga_len)
23271 retval = NULL;
23272 else
23273 {
23274 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23275 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023276#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023277 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023278 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023279#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023280 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281 }
23282
23283 /* Did we encounter a breakpoint? */
23284 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23285 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023286 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023287 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023288 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023289 sourcing_lnum);
23290 fcp->dbg_tick = debug_tick;
23291 }
23292
23293 return retval;
23294}
23295
Bram Moolenaar05159a02005-02-26 23:04:13 +000023296#if defined(FEAT_PROFILE) || defined(PROTO)
23297/*
23298 * Called when starting to read a function line.
23299 * "sourcing_lnum" must be correct!
23300 * When skipping lines it may not actually be executed, but we won't find out
23301 * until later and we need to store the time now.
23302 */
23303 void
23304func_line_start(cookie)
23305 void *cookie;
23306{
23307 funccall_T *fcp = (funccall_T *)cookie;
23308 ufunc_T *fp = fcp->func;
23309
23310 if (fp->uf_profiling && sourcing_lnum >= 1
23311 && sourcing_lnum <= fp->uf_lines.ga_len)
23312 {
23313 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023314 /* Skip continuation lines. */
23315 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23316 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023317 fp->uf_tml_execed = FALSE;
23318 profile_start(&fp->uf_tml_start);
23319 profile_zero(&fp->uf_tml_children);
23320 profile_get_wait(&fp->uf_tml_wait);
23321 }
23322}
23323
23324/*
23325 * Called when actually executing a function line.
23326 */
23327 void
23328func_line_exec(cookie)
23329 void *cookie;
23330{
23331 funccall_T *fcp = (funccall_T *)cookie;
23332 ufunc_T *fp = fcp->func;
23333
23334 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23335 fp->uf_tml_execed = TRUE;
23336}
23337
23338/*
23339 * Called when done with a function line.
23340 */
23341 void
23342func_line_end(cookie)
23343 void *cookie;
23344{
23345 funccall_T *fcp = (funccall_T *)cookie;
23346 ufunc_T *fp = fcp->func;
23347
23348 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23349 {
23350 if (fp->uf_tml_execed)
23351 {
23352 ++fp->uf_tml_count[fp->uf_tml_idx];
23353 profile_end(&fp->uf_tml_start);
23354 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023355 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023356 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23357 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023358 }
23359 fp->uf_tml_idx = -1;
23360 }
23361}
23362#endif
23363
Bram Moolenaar071d4272004-06-13 20:20:40 +000023364/*
23365 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023366 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023367 */
23368 int
23369func_has_ended(cookie)
23370 void *cookie;
23371{
Bram Moolenaar33570922005-01-25 22:26:29 +000023372 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023373
23374 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23375 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023376 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023377 || fcp->returned);
23378}
23379
23380/*
23381 * return TRUE if cookie indicates a function which "abort"s on errors.
23382 */
23383 int
23384func_has_abort(cookie)
23385 void *cookie;
23386{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023387 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023388}
23389
23390#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23391typedef enum
23392{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023393 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23394 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23395 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396} var_flavour_T;
23397
23398static var_flavour_T var_flavour __ARGS((char_u *varname));
23399
23400 static var_flavour_T
23401var_flavour(varname)
23402 char_u *varname;
23403{
23404 char_u *p = varname;
23405
23406 if (ASCII_ISUPPER(*p))
23407 {
23408 while (*(++p))
23409 if (ASCII_ISLOWER(*p))
23410 return VAR_FLAVOUR_SESSION;
23411 return VAR_FLAVOUR_VIMINFO;
23412 }
23413 else
23414 return VAR_FLAVOUR_DEFAULT;
23415}
23416#endif
23417
23418#if defined(FEAT_VIMINFO) || defined(PROTO)
23419/*
23420 * Restore global vars that start with a capital from the viminfo file
23421 */
23422 int
23423read_viminfo_varlist(virp, writing)
23424 vir_T *virp;
23425 int writing;
23426{
23427 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023428 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023429 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430
23431 if (!writing && (find_viminfo_parameter('!') != NULL))
23432 {
23433 tab = vim_strchr(virp->vir_line + 1, '\t');
23434 if (tab != NULL)
23435 {
23436 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023437 switch (*tab)
23438 {
23439 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023440#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023441 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023442#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023443 case 'D': type = VAR_DICT; break;
23444 case 'L': type = VAR_LIST; break;
23445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446
23447 tab = vim_strchr(tab, '\t');
23448 if (tab != NULL)
23449 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023450 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023451 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023452 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023454#ifdef FEAT_FLOAT
23455 else if (type == VAR_FLOAT)
23456 (void)string2float(tab + 1, &tv.vval.v_float);
23457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023459 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023460 if (type == VAR_DICT || type == VAR_LIST)
23461 {
23462 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23463
23464 if (etv == NULL)
23465 /* Failed to parse back the dict or list, use it as a
23466 * string. */
23467 tv.v_type = VAR_STRING;
23468 else
23469 {
23470 vim_free(tv.vval.v_string);
23471 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023472 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023473 }
23474 }
23475
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023476 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023477
23478 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023479 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023480 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23481 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482 }
23483 }
23484 }
23485
23486 return viminfo_readline(virp);
23487}
23488
23489/*
23490 * Write global vars that start with a capital to the viminfo file
23491 */
23492 void
23493write_viminfo_varlist(fp)
23494 FILE *fp;
23495{
Bram Moolenaar33570922005-01-25 22:26:29 +000023496 hashitem_T *hi;
23497 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023498 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023499 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023500 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023501 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023502 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023503
23504 if (find_viminfo_parameter('!') == NULL)
23505 return;
23506
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023507 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023508
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023509 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023510 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023511 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023512 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023513 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023514 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023515 this_var = HI2DI(hi);
23516 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023517 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023518 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023519 {
23520 case VAR_STRING: s = "STR"; break;
23521 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023522#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023523 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023524#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023525 case VAR_DICT: s = "DIC"; break;
23526 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023527 default: continue;
23528 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023529 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023530 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023531 if (p != NULL)
23532 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023533 vim_free(tofree);
23534 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023535 }
23536 }
23537}
23538#endif
23539
23540#if defined(FEAT_SESSION) || defined(PROTO)
23541 int
23542store_session_globals(fd)
23543 FILE *fd;
23544{
Bram Moolenaar33570922005-01-25 22:26:29 +000023545 hashitem_T *hi;
23546 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023547 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023548 char_u *p, *t;
23549
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023550 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023551 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023553 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023554 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023555 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023556 this_var = HI2DI(hi);
23557 if ((this_var->di_tv.v_type == VAR_NUMBER
23558 || this_var->di_tv.v_type == VAR_STRING)
23559 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023560 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023561 /* Escape special characters with a backslash. Turn a LF and
23562 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023563 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023564 (char_u *)"\\\"\n\r");
23565 if (p == NULL) /* out of memory */
23566 break;
23567 for (t = p; *t != NUL; ++t)
23568 if (*t == '\n')
23569 *t = 'n';
23570 else if (*t == '\r')
23571 *t = 'r';
23572 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023573 this_var->di_key,
23574 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23575 : ' ',
23576 p,
23577 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23578 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023579 || put_eol(fd) == FAIL)
23580 {
23581 vim_free(p);
23582 return FAIL;
23583 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023584 vim_free(p);
23585 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023586#ifdef FEAT_FLOAT
23587 else if (this_var->di_tv.v_type == VAR_FLOAT
23588 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23589 {
23590 float_T f = this_var->di_tv.vval.v_float;
23591 int sign = ' ';
23592
23593 if (f < 0)
23594 {
23595 f = -f;
23596 sign = '-';
23597 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023598 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023599 this_var->di_key, sign, f) < 0)
23600 || put_eol(fd) == FAIL)
23601 return FAIL;
23602 }
23603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604 }
23605 }
23606 return OK;
23607}
23608#endif
23609
Bram Moolenaar661b1822005-07-28 22:36:45 +000023610/*
23611 * Display script name where an item was last set.
23612 * Should only be invoked when 'verbose' is non-zero.
23613 */
23614 void
23615last_set_msg(scriptID)
23616 scid_T scriptID;
23617{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023618 char_u *p;
23619
Bram Moolenaar661b1822005-07-28 22:36:45 +000023620 if (scriptID != 0)
23621 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023622 p = home_replace_save(NULL, get_scriptname(scriptID));
23623 if (p != NULL)
23624 {
23625 verbose_enter();
23626 MSG_PUTS(_("\n\tLast set from "));
23627 MSG_PUTS(p);
23628 vim_free(p);
23629 verbose_leave();
23630 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023631 }
23632}
23633
Bram Moolenaard812df62008-11-09 12:46:09 +000023634/*
23635 * List v:oldfiles in a nice way.
23636 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023637 void
23638ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023639 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023640{
23641 list_T *l = vimvars[VV_OLDFILES].vv_list;
23642 listitem_T *li;
23643 int nr = 0;
23644
23645 if (l == NULL)
23646 msg((char_u *)_("No old files"));
23647 else
23648 {
23649 msg_start();
23650 msg_scroll = TRUE;
23651 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23652 {
23653 msg_outnum((long)++nr);
23654 MSG_PUTS(": ");
23655 msg_outtrans(get_tv_string(&li->li_tv));
23656 msg_putchar('\n');
23657 out_flush(); /* output one line at a time */
23658 ui_breakcheck();
23659 }
23660 /* Assume "got_int" was set to truncate the listing. */
23661 got_int = FALSE;
23662
23663#ifdef FEAT_BROWSE_CMD
23664 if (cmdmod.browse)
23665 {
23666 quit_more = FALSE;
23667 nr = prompt_for_number(FALSE);
23668 msg_starthere();
23669 if (nr > 0)
23670 {
23671 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23672 (long)nr);
23673
23674 if (p != NULL)
23675 {
23676 p = expand_env_save(p);
23677 eap->arg = p;
23678 eap->cmdidx = CMD_edit;
23679 cmdmod.browse = FALSE;
23680 do_exedit(eap, NULL);
23681 vim_free(p);
23682 }
23683 }
23684 }
23685#endif
23686 }
23687}
23688
Bram Moolenaar071d4272004-06-13 20:20:40 +000023689#endif /* FEAT_EVAL */
23690
Bram Moolenaar071d4272004-06-13 20:20:40 +000023691
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023692#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693
23694#ifdef WIN3264
23695/*
23696 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23697 */
23698static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23699static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23700static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23701
23702/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023703 * Get the short path (8.3) for the filename in "fnamep".
23704 * Only works for a valid file name.
23705 * When the path gets longer "fnamep" is changed and the allocated buffer
23706 * is put in "bufp".
23707 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23708 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023709 */
23710 static int
23711get_short_pathname(fnamep, bufp, fnamelen)
23712 char_u **fnamep;
23713 char_u **bufp;
23714 int *fnamelen;
23715{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023716 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023717 char_u *newbuf;
23718
23719 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 l = GetShortPathName(*fnamep, *fnamep, len);
23721 if (l > len - 1)
23722 {
23723 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023724 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023725 newbuf = vim_strnsave(*fnamep, l);
23726 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023727 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728
23729 vim_free(*bufp);
23730 *fnamep = *bufp = newbuf;
23731
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023732 /* Really should always succeed, as the buffer is big enough. */
23733 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023734 }
23735
23736 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023737 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738}
23739
23740/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023741 * Get the short path (8.3) for the filename in "fname". The converted
23742 * path is returned in "bufp".
23743 *
23744 * Some of the directories specified in "fname" may not exist. This function
23745 * will shorten the existing directories at the beginning of the path and then
23746 * append the remaining non-existing path.
23747 *
23748 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023749 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023750 * bufp - Pointer to an allocated buffer for the filename.
23751 * fnamelen - Length of the filename pointed to by fname
23752 *
23753 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023754 */
23755 static int
23756shortpath_for_invalid_fname(fname, bufp, fnamelen)
23757 char_u **fname;
23758 char_u **bufp;
23759 int *fnamelen;
23760{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023761 char_u *short_fname, *save_fname, *pbuf_unused;
23762 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023763 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023764 int old_len, len;
23765 int new_len, sfx_len;
23766 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767
23768 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023769 old_len = *fnamelen;
23770 save_fname = vim_strnsave(*fname, old_len);
23771 pbuf_unused = NULL;
23772 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023774 endp = save_fname + old_len - 1; /* Find the end of the copy */
23775 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023776
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023777 /*
23778 * Try shortening the supplied path till it succeeds by removing one
23779 * directory at a time from the tail of the path.
23780 */
23781 len = 0;
23782 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023783 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023784 /* go back one path-separator */
23785 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23786 --endp;
23787 if (endp <= save_fname)
23788 break; /* processed the complete path */
23789
23790 /*
23791 * Replace the path separator with a NUL and try to shorten the
23792 * resulting path.
23793 */
23794 ch = *endp;
23795 *endp = 0;
23796 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023797 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023798 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23799 {
23800 retval = FAIL;
23801 goto theend;
23802 }
23803 *endp = ch; /* preserve the string */
23804
23805 if (len > 0)
23806 break; /* successfully shortened the path */
23807
23808 /* failed to shorten the path. Skip the path separator */
23809 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023810 }
23811
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023812 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023813 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023814 /*
23815 * Succeeded in shortening the path. Now concatenate the shortened
23816 * path with the remaining path at the tail.
23817 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023819 /* Compute the length of the new path. */
23820 sfx_len = (int)(save_endp - endp) + 1;
23821 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023823 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023825 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023826 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023827 /* There is not enough space in the currently allocated string,
23828 * copy it to a buffer big enough. */
23829 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023831 {
23832 retval = FAIL;
23833 goto theend;
23834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835 }
23836 else
23837 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023838 /* Transfer short_fname to the main buffer (it's big enough),
23839 * unless get_short_pathname() did its work in-place. */
23840 *fname = *bufp = save_fname;
23841 if (short_fname != save_fname)
23842 vim_strncpy(save_fname, short_fname, len);
23843 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023844 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023845
23846 /* concat the not-shortened part of the path */
23847 vim_strncpy(*fname + len, endp, sfx_len);
23848 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023849 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023850
23851theend:
23852 vim_free(pbuf_unused);
23853 vim_free(save_fname);
23854
23855 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023856}
23857
23858/*
23859 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023860 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023861 */
23862 static int
23863shortpath_for_partial(fnamep, bufp, fnamelen)
23864 char_u **fnamep;
23865 char_u **bufp;
23866 int *fnamelen;
23867{
23868 int sepcount, len, tflen;
23869 char_u *p;
23870 char_u *pbuf, *tfname;
23871 int hasTilde;
23872
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023873 /* Count up the path separators from the RHS.. so we know which part
23874 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023875 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023876 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023877 if (vim_ispathsep(*p))
23878 ++sepcount;
23879
23880 /* Need full path first (use expand_env() to remove a "~/") */
23881 hasTilde = (**fnamep == '~');
23882 if (hasTilde)
23883 pbuf = tfname = expand_env_save(*fnamep);
23884 else
23885 pbuf = tfname = FullName_save(*fnamep, FALSE);
23886
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023887 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023889 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23890 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891
23892 if (len == 0)
23893 {
23894 /* Don't have a valid filename, so shorten the rest of the
23895 * path if we can. This CAN give us invalid 8.3 filenames, but
23896 * there's not a lot of point in guessing what it might be.
23897 */
23898 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023899 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23900 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901 }
23902
23903 /* Count the paths backward to find the beginning of the desired string. */
23904 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023905 {
23906#ifdef FEAT_MBYTE
23907 if (has_mbyte)
23908 p -= mb_head_off(tfname, p);
23909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910 if (vim_ispathsep(*p))
23911 {
23912 if (sepcount == 0 || (hasTilde && sepcount == 1))
23913 break;
23914 else
23915 sepcount --;
23916 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023918 if (hasTilde)
23919 {
23920 --p;
23921 if (p >= tfname)
23922 *p = '~';
23923 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023924 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023925 }
23926 else
23927 ++p;
23928
23929 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23930 vim_free(*bufp);
23931 *fnamelen = (int)STRLEN(p);
23932 *bufp = pbuf;
23933 *fnamep = p;
23934
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023935 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023936}
23937#endif /* WIN3264 */
23938
23939/*
23940 * Adjust a filename, according to a string of modifiers.
23941 * *fnamep must be NUL terminated when called. When returning, the length is
23942 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023943 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 * When there is an error, *fnamep is set to NULL.
23945 */
23946 int
23947modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23948 char_u *src; /* string with modifiers */
23949 int *usedlen; /* characters after src that are used */
23950 char_u **fnamep; /* file name so far */
23951 char_u **bufp; /* buffer for allocated file name or NULL */
23952 int *fnamelen; /* length of fnamep */
23953{
23954 int valid = 0;
23955 char_u *tail;
23956 char_u *s, *p, *pbuf;
23957 char_u dirname[MAXPATHL];
23958 int c;
23959 int has_fullname = 0;
23960#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023961 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 int has_shortname = 0;
23963#endif
23964
23965repeat:
23966 /* ":p" - full path/file_name */
23967 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23968 {
23969 has_fullname = 1;
23970
23971 valid |= VALID_PATH;
23972 *usedlen += 2;
23973
23974 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23975 if ((*fnamep)[0] == '~'
23976#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23977 && ((*fnamep)[1] == '/'
23978# ifdef BACKSLASH_IN_FILENAME
23979 || (*fnamep)[1] == '\\'
23980# endif
23981 || (*fnamep)[1] == NUL)
23982
23983#endif
23984 )
23985 {
23986 *fnamep = expand_env_save(*fnamep);
23987 vim_free(*bufp); /* free any allocated file name */
23988 *bufp = *fnamep;
23989 if (*fnamep == NULL)
23990 return -1;
23991 }
23992
23993 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023994 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995 {
23996 if (vim_ispathsep(*p)
23997 && p[1] == '.'
23998 && (p[2] == NUL
23999 || vim_ispathsep(p[2])
24000 || (p[2] == '.'
24001 && (p[3] == NUL || vim_ispathsep(p[3])))))
24002 break;
24003 }
24004
24005 /* FullName_save() is slow, don't use it when not needed. */
24006 if (*p != NUL || !vim_isAbsName(*fnamep))
24007 {
24008 *fnamep = FullName_save(*fnamep, *p != NUL);
24009 vim_free(*bufp); /* free any allocated file name */
24010 *bufp = *fnamep;
24011 if (*fnamep == NULL)
24012 return -1;
24013 }
24014
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020024015#ifdef WIN3264
24016# if _WIN32_WINNT >= 0x0500
24017 if (vim_strchr(*fnamep, '~') != NULL)
24018 {
24019 /* Expand 8.3 filename to full path. Needed to make sure the same
24020 * file does not have two different names.
24021 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
24022 p = alloc(_MAX_PATH + 1);
24023 if (p != NULL)
24024 {
24025 if (GetLongPathName(*fnamep, p, MAXPATHL))
24026 {
24027 vim_free(*bufp);
24028 *bufp = *fnamep = p;
24029 }
24030 else
24031 vim_free(p);
24032 }
24033 }
24034# endif
24035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024036 /* Append a path separator to a directory. */
24037 if (mch_isdir(*fnamep))
24038 {
24039 /* Make room for one or two extra characters. */
24040 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24041 vim_free(*bufp); /* free any allocated file name */
24042 *bufp = *fnamep;
24043 if (*fnamep == NULL)
24044 return -1;
24045 add_pathsep(*fnamep);
24046 }
24047 }
24048
24049 /* ":." - path relative to the current directory */
24050 /* ":~" - path relative to the home directory */
24051 /* ":8" - shortname path - postponed till after */
24052 while (src[*usedlen] == ':'
24053 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24054 {
24055 *usedlen += 2;
24056 if (c == '8')
24057 {
24058#ifdef WIN3264
24059 has_shortname = 1; /* Postpone this. */
24060#endif
24061 continue;
24062 }
24063 pbuf = NULL;
24064 /* Need full path first (use expand_env() to remove a "~/") */
24065 if (!has_fullname)
24066 {
24067 if (c == '.' && **fnamep == '~')
24068 p = pbuf = expand_env_save(*fnamep);
24069 else
24070 p = pbuf = FullName_save(*fnamep, FALSE);
24071 }
24072 else
24073 p = *fnamep;
24074
24075 has_fullname = 0;
24076
24077 if (p != NULL)
24078 {
24079 if (c == '.')
24080 {
24081 mch_dirname(dirname, MAXPATHL);
24082 s = shorten_fname(p, dirname);
24083 if (s != NULL)
24084 {
24085 *fnamep = s;
24086 if (pbuf != NULL)
24087 {
24088 vim_free(*bufp); /* free any allocated file name */
24089 *bufp = pbuf;
24090 pbuf = NULL;
24091 }
24092 }
24093 }
24094 else
24095 {
24096 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24097 /* Only replace it when it starts with '~' */
24098 if (*dirname == '~')
24099 {
24100 s = vim_strsave(dirname);
24101 if (s != NULL)
24102 {
24103 *fnamep = s;
24104 vim_free(*bufp);
24105 *bufp = s;
24106 }
24107 }
24108 }
24109 vim_free(pbuf);
24110 }
24111 }
24112
24113 tail = gettail(*fnamep);
24114 *fnamelen = (int)STRLEN(*fnamep);
24115
24116 /* ":h" - head, remove "/file_name", can be repeated */
24117 /* Don't remove the first "/" or "c:\" */
24118 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24119 {
24120 valid |= VALID_HEAD;
24121 *usedlen += 2;
24122 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024123 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024124 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024125 *fnamelen = (int)(tail - *fnamep);
24126#ifdef VMS
24127 if (*fnamelen > 0)
24128 *fnamelen += 1; /* the path separator is part of the path */
24129#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024130 if (*fnamelen == 0)
24131 {
24132 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24133 p = vim_strsave((char_u *)".");
24134 if (p == NULL)
24135 return -1;
24136 vim_free(*bufp);
24137 *bufp = *fnamep = tail = p;
24138 *fnamelen = 1;
24139 }
24140 else
24141 {
24142 while (tail > s && !after_pathsep(s, tail))
24143 mb_ptr_back(*fnamep, tail);
24144 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024145 }
24146
24147 /* ":8" - shortname */
24148 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24149 {
24150 *usedlen += 2;
24151#ifdef WIN3264
24152 has_shortname = 1;
24153#endif
24154 }
24155
24156#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024157 /*
24158 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024159 */
24160 if (has_shortname)
24161 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024162 /* Copy the string if it is shortened by :h and when it wasn't copied
24163 * yet, because we are going to change it in place. Avoids changing
24164 * the buffer name for "%:8". */
24165 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024166 {
24167 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024168 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024169 return -1;
24170 vim_free(*bufp);
24171 *bufp = *fnamep = p;
24172 }
24173
24174 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024175 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176 if (!has_fullname && !vim_isAbsName(*fnamep))
24177 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024178 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024179 return -1;
24180 }
24181 else
24182 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024183 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184
Bram Moolenaardc935552011-08-17 15:23:23 +020024185 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024187 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188 return -1;
24189
24190 if (l == 0)
24191 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024192 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024193 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024194 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 return -1;
24196 }
24197 *fnamelen = l;
24198 }
24199 }
24200#endif /* WIN3264 */
24201
24202 /* ":t" - tail, just the basename */
24203 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24204 {
24205 *usedlen += 2;
24206 *fnamelen -= (int)(tail - *fnamep);
24207 *fnamep = tail;
24208 }
24209
24210 /* ":e" - extension, can be repeated */
24211 /* ":r" - root, without extension, can be repeated */
24212 while (src[*usedlen] == ':'
24213 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24214 {
24215 /* find a '.' in the tail:
24216 * - for second :e: before the current fname
24217 * - otherwise: The last '.'
24218 */
24219 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24220 s = *fnamep - 2;
24221 else
24222 s = *fnamep + *fnamelen - 1;
24223 for ( ; s > tail; --s)
24224 if (s[0] == '.')
24225 break;
24226 if (src[*usedlen + 1] == 'e') /* :e */
24227 {
24228 if (s > tail)
24229 {
24230 *fnamelen += (int)(*fnamep - (s + 1));
24231 *fnamep = s + 1;
24232#ifdef VMS
24233 /* cut version from the extension */
24234 s = *fnamep + *fnamelen - 1;
24235 for ( ; s > *fnamep; --s)
24236 if (s[0] == ';')
24237 break;
24238 if (s > *fnamep)
24239 *fnamelen = s - *fnamep;
24240#endif
24241 }
24242 else if (*fnamep <= tail)
24243 *fnamelen = 0;
24244 }
24245 else /* :r */
24246 {
24247 if (s > tail) /* remove one extension */
24248 *fnamelen = (int)(s - *fnamep);
24249 }
24250 *usedlen += 2;
24251 }
24252
24253 /* ":s?pat?foo?" - substitute */
24254 /* ":gs?pat?foo?" - global substitute */
24255 if (src[*usedlen] == ':'
24256 && (src[*usedlen + 1] == 's'
24257 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24258 {
24259 char_u *str;
24260 char_u *pat;
24261 char_u *sub;
24262 int sep;
24263 char_u *flags;
24264 int didit = FALSE;
24265
24266 flags = (char_u *)"";
24267 s = src + *usedlen + 2;
24268 if (src[*usedlen + 1] == 'g')
24269 {
24270 flags = (char_u *)"g";
24271 ++s;
24272 }
24273
24274 sep = *s++;
24275 if (sep)
24276 {
24277 /* find end of pattern */
24278 p = vim_strchr(s, sep);
24279 if (p != NULL)
24280 {
24281 pat = vim_strnsave(s, (int)(p - s));
24282 if (pat != NULL)
24283 {
24284 s = p + 1;
24285 /* find end of substitution */
24286 p = vim_strchr(s, sep);
24287 if (p != NULL)
24288 {
24289 sub = vim_strnsave(s, (int)(p - s));
24290 str = vim_strnsave(*fnamep, *fnamelen);
24291 if (sub != NULL && str != NULL)
24292 {
24293 *usedlen = (int)(p + 1 - src);
24294 s = do_string_sub(str, pat, sub, flags);
24295 if (s != NULL)
24296 {
24297 *fnamep = s;
24298 *fnamelen = (int)STRLEN(s);
24299 vim_free(*bufp);
24300 *bufp = s;
24301 didit = TRUE;
24302 }
24303 }
24304 vim_free(sub);
24305 vim_free(str);
24306 }
24307 vim_free(pat);
24308 }
24309 }
24310 /* after using ":s", repeat all the modifiers */
24311 if (didit)
24312 goto repeat;
24313 }
24314 }
24315
24316 return valid;
24317}
24318
24319/*
24320 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24321 * "flags" can be "g" to do a global substitute.
24322 * Returns an allocated string, NULL for error.
24323 */
24324 char_u *
24325do_string_sub(str, pat, sub, flags)
24326 char_u *str;
24327 char_u *pat;
24328 char_u *sub;
24329 char_u *flags;
24330{
24331 int sublen;
24332 regmatch_T regmatch;
24333 int i;
24334 int do_all;
24335 char_u *tail;
24336 garray_T ga;
24337 char_u *ret;
24338 char_u *save_cpo;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024339 int zero_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024340
24341 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24342 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024343 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024344
24345 ga_init2(&ga, 1, 200);
24346
24347 do_all = (flags[0] == 'g');
24348
24349 regmatch.rm_ic = p_ic;
24350 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24351 if (regmatch.regprog != NULL)
24352 {
24353 tail = str;
24354 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24355 {
24356 /*
24357 * Get some space for a temporary buffer to do the substitution
24358 * into. It will contain:
24359 * - The text up to where the match is.
24360 * - The substituted text.
24361 * - The text after the match.
24362 */
24363 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24364 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24365 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24366 {
24367 ga_clear(&ga);
24368 break;
24369 }
24370
24371 /* copy the text up to where the match is */
24372 i = (int)(regmatch.startp[0] - tail);
24373 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24374 /* add the substituted text */
24375 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24376 + ga.ga_len + i, TRUE, TRUE, FALSE);
24377 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024378 zero_width = (tail == regmatch.endp[0]
24379 || regmatch.startp[0] == regmatch.endp[0]);
24380 tail = regmatch.endp[0];
24381 if (*tail == NUL)
24382 break;
24383 if (zero_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024384 {
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024385 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024386 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24387 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024389 if (!do_all)
24390 break;
24391 }
24392
24393 if (ga.ga_data != NULL)
24394 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24395
Bram Moolenaar473de612013-06-08 18:19:48 +020024396 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024397 }
24398
24399 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24400 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024401 if (p_cpo == empty_option)
24402 p_cpo = save_cpo;
24403 else
24404 /* Darn, evaluating {sub} expression changed the value. */
24405 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406
24407 return ret;
24408}
24409
24410#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */