blob: 2a2817e6d23cd15751b88e46e9c5c1f736e5b048 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200128/* When using exists() don't auto-load a script. */
129static int no_autoload = FALSE;
130
Bram Moolenaar532c7802005-01-27 14:44:31 +0000131/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 * When recursively copying lists and dicts we need to remember which ones we
133 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135 */
136static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000137#define COPYID_INC 2
138#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139
140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
161 * Structure to hold info for a user function.
162 */
163typedef struct ufunc ufunc_T;
164
165struct ufunc
166{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000167 int uf_varargs; /* variable nr of arguments */
168 int uf_flags;
169 int uf_calls; /* nr of active calls */
170 garray_T uf_args; /* arguments */
171 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000172#ifdef FEAT_PROFILE
173 int uf_profiling; /* TRUE when func is being profiled */
174 /* profiling the function as a whole */
175 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000176 proftime_T uf_tm_total; /* time spent in function + children */
177 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000178 proftime_T uf_tm_children; /* time spent in children this call */
179 /* profiling the function per line */
180 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T *uf_tml_total; /* time spent in a line + children */
182 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tml_start; /* start time for current line */
184 proftime_T uf_tml_children; /* time spent in children for this line */
185 proftime_T uf_tml_wait; /* start wait time for current line */
186 int uf_tml_idx; /* index of line being timed; -1 if none */
187 int uf_tml_execed; /* line being timed was executed */
188#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 int uf_refcount; /* for numbered function: reference count */
192 char_u uf_name[1]; /* name of function (actually longer); can
193 start with <SNR>123_ (<SNR> is K_SPECIAL
194 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195};
196
197/* function flags */
198#define FC_ABORT 1 /* abort function on error */
199#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000200#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
202/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000203 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000205static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000207/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000208static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
209
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000210/* list heads for garbage collection */
211static dict_T *first_dict = NULL; /* list of all dicts */
212static list_T *first_list = NULL; /* list of all lists */
213
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000214/* From user function to hashitem and back. */
215static ufunc_T dumuf;
216#define UF2HIKEY(fp) ((fp)->uf_name)
217#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
218#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
219
220#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
221#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
Bram Moolenaar33570922005-01-25 22:26:29 +0000223#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
224#define VAR_SHORT_LEN 20 /* short variable name length */
225#define FIXVAR_CNT 12 /* number of fixed variables */
226
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000228typedef struct funccall_S funccall_T;
229
230struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231{
232 ufunc_T *func; /* function being called */
233 int linenr; /* next line to be executed */
234 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000235 struct /* fixed variables for arguments */
236 {
237 dictitem_T var; /* variable (without room for name) */
238 char_u room[VAR_SHORT_LEN]; /* room for the name */
239 } fixvar[FIXVAR_CNT];
240 dict_T l_vars; /* l: local function variables */
241 dictitem_T l_vars_var; /* variable for l: scope */
242 dict_T l_avars; /* a: argument variables */
243 dictitem_T l_avars_var; /* variable for a: scope */
244 list_T l_varlist; /* list for a:000 */
245 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
246 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 linenr_T breakpoint; /* next line with breakpoint or zero */
248 int dbg_tick; /* debug_tick when breakpoint was set */
249 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000250#ifdef FEAT_PROFILE
251 proftime_T prof_child; /* time spent in a child */
252#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000253 funccall_T *caller; /* calling function or NULL */
254};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257 * Info used by a ":for" loop.
258 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000259typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000260{
261 int fi_semicolon; /* TRUE if ending in '; var]' */
262 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 listwatch_T fi_lw; /* keep an eye on the item used. */
264 list_T *fi_list; /* list being used */
265} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000266
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000268 * Struct used by trans_function_name()
269 */
270typedef struct
271{
Bram Moolenaar33570922005-01-25 22:26:29 +0000272 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000273 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dictitem_T *fd_di; /* Dictionary item used */
275} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000276
Bram Moolenaara7043832005-01-21 11:56:39 +0000277
278/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 * Array to hold the value of v: variables.
280 * The value is in a dictitem, so that it can also be used in the v: scope.
281 * The reason to use this table anyway is for very quick access to the
282 * variables with the VV_ defines.
283 */
284#include "version.h"
285
286/* values for vv_flags: */
287#define VV_COMPAT 1 /* compatible, also used without "v:" */
288#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000289#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000290
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000291#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
293static struct vimvar
294{
295 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000296 dictitem_T vv_di; /* value and name for key */
297 char vv_filler[16]; /* space for LONGEST name below!!! */
298 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
299} vimvars[VV_LEN] =
300{
301 /*
302 * The order here must match the VV_ defines in vim.h!
303 * Initializing a union does not work, leave tv.vval empty to get zero's.
304 */
305 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
306 {VV_NAME("count1", VAR_NUMBER), VV_RO},
307 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
308 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
309 {VV_NAME("warningmsg", VAR_STRING), 0},
310 {VV_NAME("statusmsg", VAR_STRING), 0},
311 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
313 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
315 {VV_NAME("termresponse", VAR_STRING), VV_RO},
316 {VV_NAME("fname", VAR_STRING), VV_RO},
317 {VV_NAME("lang", VAR_STRING), VV_RO},
318 {VV_NAME("lc_time", VAR_STRING), VV_RO},
319 {VV_NAME("ctype", VAR_STRING), VV_RO},
320 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
321 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
322 {VV_NAME("fname_in", VAR_STRING), VV_RO},
323 {VV_NAME("fname_out", VAR_STRING), VV_RO},
324 {VV_NAME("fname_new", VAR_STRING), VV_RO},
325 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
326 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
327 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
328 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
330 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("progname", VAR_STRING), VV_RO},
332 {VV_NAME("servername", VAR_STRING), VV_RO},
333 {VV_NAME("dying", VAR_NUMBER), VV_RO},
334 {VV_NAME("exception", VAR_STRING), VV_RO},
335 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
336 {VV_NAME("register", VAR_STRING), VV_RO},
337 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
338 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000339 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
340 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000341 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000342 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
343 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000344 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
345 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000349 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000350 {VV_NAME("swapname", VAR_STRING), VV_RO},
351 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000352 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200353 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000354 {VV_NAME("mouse_win", VAR_NUMBER), 0},
355 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
356 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000357 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000358 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000359 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200360 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000361};
362
363/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364#define vv_type vv_di.di_tv.v_type
365#define vv_nr vv_di.di_tv.vval.v_number
366#define vv_float vv_di.di_tv.vval.v_float
367#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000368#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000369#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000370
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200371static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000372#define vimvarht vimvardict.dv_hashtab
373
Bram Moolenaara40058a2005-07-11 22:42:07 +0000374static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
375static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000376static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
377static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
378static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000379static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
380static void list_glob_vars __ARGS((int *first));
381static void list_buf_vars __ARGS((int *first));
382static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000383#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000384static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000385#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_vim_vars __ARGS((int *first));
387static void list_script_vars __ARGS((int *first));
388static void list_func_vars __ARGS((int *first));
389static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000390static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
391static int check_changedtick __ARGS((char_u *arg));
392static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
393static void clear_lval __ARGS((lval_T *lp));
394static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
395static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000396static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
397static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
398static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
399static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
400static void item_lock __ARGS((typval_T *tv, int deep, int lock));
401static int tv_islocked __ARGS((typval_T *tv));
402
Bram Moolenaar33570922005-01-25 22:26:29 +0000403static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
404static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000409static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
410static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000411
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000412static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000417static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100419static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
420static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
421static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000422static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000424static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
426static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000427static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000428static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100429static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000430static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000431static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200432static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
434static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000435static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000437static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000439static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
440static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000442#ifdef FEAT_FLOAT
443static int string2float __ARGS((char_u *text, float_T *value));
444#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
446static int find_internal_func __ARGS((char_u *name));
447static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
448static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200449static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000450static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000451static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200455static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100458static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200464static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
480static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
481#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000482static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000485static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000487#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000488static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000489static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
491#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#ifdef FEAT_FLOAT
495static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200496static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
501static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200512#ifdef FEAT_FLOAT
513static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000517static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523#ifdef FEAT_FLOAT
524static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200526static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000527#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000528static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000537static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000539static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000545static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000553static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000554static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000555static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000556static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200559static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000560static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000568static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000582static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100587static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000589static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000601#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200602static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000603static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
604#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200605#ifdef FEAT_LUA
606static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
607#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000612static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000613static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000614static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000616static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000617static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000620#ifdef vim_mkdir
621static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
622#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100624#ifdef FEAT_MZSCHEME
625static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
626#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100629static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000630static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000631#ifdef FEAT_FLOAT
632static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000635static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000636static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200637#ifdef FEAT_PYTHON3
638static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
640#ifdef FEAT_PYTHON
641static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000644static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000645static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#ifdef FEAT_FLOAT
658static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
659#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200660static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100662static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000665static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000667static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000674static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000675static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000676static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000677static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200679static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000680static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100682#ifdef FEAT_CRYPT
683static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
684#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000685static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200686static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000688#ifdef FEAT_FLOAT
689static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200690static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000693static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000694static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#ifdef FEAT_FLOAT
698static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
699static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
700#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000701static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200702static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703#ifdef HAVE_STRFTIME
704static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
705#endif
706static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200712static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200713static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000719static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200720static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000722static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000723static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000724static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000725static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000726static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000728static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200729#ifdef FEAT_FLOAT
730static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
732#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000736#ifdef FEAT_FLOAT
737static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200740static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200741static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100745static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000752static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000755static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100756static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000757
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000758static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000759static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760static int get_env_len __ARGS((char_u **arg));
761static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000762static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000763static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
764#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
765#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
766 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000767static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000768static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000769static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000770static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
771static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772static typval_T *alloc_tv __ARGS((void));
773static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static void init_tv __ARGS((typval_T *varp));
775static long get_tv_number __ARGS((typval_T *varp));
776static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000777static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static char_u *get_tv_string __ARGS((typval_T *varp));
779static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000780static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200782static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
784static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
785static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000786static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
787static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
789static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000790static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200791static int var_check_func_name __ARGS((char_u *name, int new_var));
792static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000793static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000794static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
796static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
797static int eval_fname_script __ARGS((char_u *p));
798static int eval_fname_sid __ARGS((char_u *p));
799static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static ufunc_T *find_func __ARGS((char_u *name));
801static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000802static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803#ifdef FEAT_PROFILE
804static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000805static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
806static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
807static int
808# ifdef __BORLANDC__
809 _RTLENTRYF
810# endif
811 prof_total_cmp __ARGS((const void *s1, const void *s2));
812static int
813# ifdef __BORLANDC__
814 _RTLENTRYF
815# endif
816 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200818static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000819static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000820static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000821static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000823static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
824static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000825static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000826static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
827static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000828static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000829static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000830static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200832
833#ifdef EBCDIC
834static int compare_func_name __ARGS((const void *s1, const void *s2));
835static void sortFunctions __ARGS(());
836#endif
837
Bram Moolenaar33570922005-01-25 22:26:29 +0000838/*
839 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000840 */
841 void
842eval_init()
843{
Bram Moolenaar33570922005-01-25 22:26:29 +0000844 int i;
845 struct vimvar *p;
846
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200847 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
848 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200849 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000850 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000851 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000852
853 for (i = 0; i < VV_LEN; ++i)
854 {
855 p = &vimvars[i];
856 STRCPY(p->vv_di.di_key, p->vv_name);
857 if (p->vv_flags & VV_RO)
858 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
859 else if (p->vv_flags & VV_RO_SBX)
860 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
861 else
862 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000863
864 /* add to v: scope dict, unless the value is not always available */
865 if (p->vv_type != VAR_UNKNOWN)
866 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000868 /* add to compat scope dict */
869 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000871 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200872 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200873
874#ifdef EBCDIC
875 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100876 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200877 */
878 sortFunctions();
879#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000880}
881
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000882#if defined(EXITFREE) || defined(PROTO)
883 void
884eval_clear()
885{
886 int i;
887 struct vimvar *p;
888
889 for (i = 0; i < VV_LEN; ++i)
890 {
891 p = &vimvars[i];
892 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000893 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000894 vim_free(p->vv_str);
895 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000896 }
897 else if (p->vv_di.di_tv.v_type == VAR_LIST)
898 {
899 list_unref(p->vv_list);
900 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000901 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000902 }
903 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000904 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000905 hash_clear(&compat_hashtab);
906
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100908# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200909 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100910# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911
912 /* global variables */
913 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000915 /* autoloaded script names */
916 ga_clear_strings(&ga_loaded);
917
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200918 /* script-local variables */
919 for (i = 1; i <= ga_scripts.ga_len; ++i)
920 {
921 vars_clear(&SCRIPT_VARS(i));
922 vim_free(SCRIPT_SV(i));
923 }
924 ga_clear(&ga_scripts);
925
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000926 /* unreferenced lists and dicts */
927 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000928
929 /* functions */
930 free_all_functions();
931 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000932}
933#endif
934
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 * Return the name of the executed function.
937 */
938 char_u *
939func_name(cookie)
940 void *cookie;
941{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000942 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943}
944
945/*
946 * Return the address holding the next breakpoint line for a funccall cookie.
947 */
948 linenr_T *
949func_breakpoint(cookie)
950 void *cookie;
951{
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the address holding the debug tick for a funccall cookie.
957 */
958 int *
959func_dbg_tick(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the nesting level for a funccall cookie.
967 */
968 int
969func_level(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000976funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000978/* pointer to list of previously used funccal, still around because some
979 * item in it is still being used. */
980funccall_T *previous_funccal = NULL;
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Return TRUE when a function was ended by a ":return" command.
984 */
985 int
986current_func_returned()
987{
988 return current_funccal->returned;
989}
990
991
992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 * Set an internal variable to a string value. Creates the variable if it does
994 * not already exist.
995 */
996 void
997set_internal_string_var(name, value)
998 char_u *name;
999 char_u *value;
1000{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001001 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001002 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004 val = vim_strsave(value);
1005 if (val != NULL)
1006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001007 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001010 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 }
1014}
1015
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001017static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018static char_u *redir_endp = NULL;
1019static char_u *redir_varname = NULL;
1020
1021/*
1022 * Start recording command output to a variable
1023 * Returns OK if successfully completed the setup. FAIL otherwise.
1024 */
1025 int
1026var_redir_start(name, append)
1027 char_u *name;
1028 int append; /* append to an existing variable */
1029{
1030 int save_emsg;
1031 int err;
1032 typval_T tv;
1033
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001034 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001035 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 {
1037 EMSG(_(e_invarg));
1038 return FAIL;
1039 }
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042 redir_varname = vim_strsave(name);
1043 if (redir_varname == NULL)
1044 return FAIL;
1045
1046 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1047 if (redir_lval == NULL)
1048 {
1049 var_redir_stop();
1050 return FAIL;
1051 }
1052
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001053 /* The output is stored in growarray "redir_ga" until redirection ends. */
1054 ga_init2(&redir_ga, (int)sizeof(char), 500);
1055
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001057 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1058 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1060 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001061 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp != NULL && *redir_endp != NUL)
1063 /* Trailing characters are present after the variable name */
1064 EMSG(_(e_trailing));
1065 else
1066 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001067 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068 var_redir_stop();
1069 return FAIL;
1070 }
1071
1072 /* check if we can write to the variable: set it to or append an empty
1073 * string */
1074 save_emsg = did_emsg;
1075 did_emsg = FALSE;
1076 tv.v_type = VAR_STRING;
1077 tv.vval.v_string = (char_u *)"";
1078 if (append)
1079 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1080 else
1081 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001082 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001084 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 if (err)
1086 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096 * Append "value[value_len]" to the variable set by var_redir_start().
1097 * The actual appending is postponed until redirection ends, because the value
1098 * appended may in fact be the string we write to, changing it may cause freed
1099 * memory to be used:
1100 * :redir => foo
1101 * :let foo
1102 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 */
1104 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001109 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110
1111 if (redir_lval == NULL)
1112 return;
1113
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 {
1121 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 }
1124 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126}
1127
1128/*
1129 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001130 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 */
1132 void
1133var_redir_stop()
1134{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 typval_T tv;
1136
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_lval != NULL)
1138 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001139 /* If there was no error: assign the text to the variable. */
1140 if (redir_endp != NULL)
1141 {
1142 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1143 tv.v_type = VAR_STRING;
1144 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001145 /* Call get_lval() again, if it's inside a Dict or List it may
1146 * have changed. */
1147 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1148 FALSE, FALSE, FALSE, FNE_CHECK_START);
1149 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1150 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1151 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* free the collected output */
1155 vim_free(redir_ga.ga_data);
1156 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 vim_free(redir_lval);
1159 redir_lval = NULL;
1160 }
1161 vim_free(redir_varname);
1162 redir_varname = NULL;
1163}
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165# if defined(FEAT_MBYTE) || defined(PROTO)
1166 int
1167eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1168 char_u *enc_from;
1169 char_u *enc_to;
1170 char_u *fname_from;
1171 char_u *fname_to;
1172{
1173 int err = FALSE;
1174
1175 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1176 set_vim_var_string(VV_CC_TO, enc_to, -1);
1177 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1178 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1179 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1180 err = TRUE;
1181 set_vim_var_string(VV_CC_FROM, NULL, -1);
1182 set_vim_var_string(VV_CC_TO, NULL, -1);
1183 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1184 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1185
1186 if (err)
1187 return FAIL;
1188 return OK;
1189}
1190# endif
1191
1192# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1193 int
1194eval_printexpr(fname, args)
1195 char_u *fname;
1196 char_u *args;
1197{
1198 int err = FALSE;
1199
1200 set_vim_var_string(VV_FNAME_IN, fname, -1);
1201 set_vim_var_string(VV_CMDARG, args, -1);
1202 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1203 err = TRUE;
1204 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1205 set_vim_var_string(VV_CMDARG, NULL, -1);
1206
1207 if (err)
1208 {
1209 mch_remove(fname);
1210 return FAIL;
1211 }
1212 return OK;
1213}
1214# endif
1215
1216# if defined(FEAT_DIFF) || defined(PROTO)
1217 void
1218eval_diff(origfile, newfile, outfile)
1219 char_u *origfile;
1220 char_u *newfile;
1221 char_u *outfile;
1222{
1223 int err = FALSE;
1224
1225 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1226 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1227 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1228 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1229 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1230 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1231 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1232}
1233
1234 void
1235eval_patch(origfile, difffile, outfile)
1236 char_u *origfile;
1237 char_u *difffile;
1238 char_u *outfile;
1239{
1240 int err;
1241
1242 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1243 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1244 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1245 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1246 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1248 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1249}
1250# endif
1251
1252/*
1253 * Top level evaluation function, returning a boolean.
1254 * Sets "error" to TRUE if there was an error.
1255 * Return TRUE or FALSE.
1256 */
1257 int
1258eval_to_bool(arg, error, nextcmd, skip)
1259 char_u *arg;
1260 int *error;
1261 char_u **nextcmd;
1262 int skip; /* only parse, don't execute */
1263{
Bram Moolenaar33570922005-01-25 22:26:29 +00001264 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 int retval = FALSE;
1266
1267 if (skip)
1268 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001269 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else
1272 {
1273 *error = FALSE;
1274 if (!skip)
1275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001276 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 }
1280 if (skip)
1281 --emsg_skip;
1282
1283 return retval;
1284}
1285
1286/*
1287 * Top level evaluation function, returning a string. If "skip" is TRUE,
1288 * only parsing to "nextcmd" is done, without reporting errors. Return
1289 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1290 */
1291 char_u *
1292eval_to_string_skip(arg, nextcmd, skip)
1293 char_u *arg;
1294 char_u **nextcmd;
1295 int skip; /* only parse, don't execute */
1296{
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 char_u *retval;
1299
1300 if (skip)
1301 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 retval = NULL;
1304 else
1305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 retval = vim_strsave(get_tv_string(&tv));
1307 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309 if (skip)
1310 --emsg_skip;
1311
1312 return retval;
1313}
1314
1315/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001316 * Skip over an expression at "*pp".
1317 * Return FAIL for an error, OK otherwise.
1318 */
1319 int
1320skip_expr(pp)
1321 char_u **pp;
1322{
Bram Moolenaar33570922005-01-25 22:26:29 +00001323 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001324
1325 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327}
1328
1329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001331 * When "convert" is TRUE convert a List into a sequence of lines and convert
1332 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Return pointer to allocated memory, or NULL for failure.
1334 */
1335 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 char_u *arg;
1338 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001343 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001344#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001345 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 retval = NULL;
1350 else
1351 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001353 {
1354 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001355 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001356 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001357 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001358 if (tv.vval.v_list->lv_len > 0)
1359 ga_append(&ga, NL);
1360 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001361 ga_append(&ga, NUL);
1362 retval = (char_u *)ga.ga_data;
1363 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364#ifdef FEAT_FLOAT
1365 else if (convert && tv.v_type == VAR_FLOAT)
1366 {
1367 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1368 retval = vim_strsave(numbuf);
1369 }
1370#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 else
1372 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375
1376 return retval;
1377}
1378
1379/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001380 * Call eval_to_string() without using current local variables and using
1381 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 */
1383 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001384eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 char_u *arg;
1386 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 char_u *retval;
1390 void *save_funccalp;
1391
1392 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 if (use_sandbox)
1394 ++sandbox;
1395 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001396 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 if (use_sandbox)
1398 --sandbox;
1399 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 restore_funccal(save_funccalp);
1401 return retval;
1402}
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404/*
1405 * Top level evaluation function, returning a number.
1406 * Evaluates "expr" silently.
1407 * Returns -1 for an error.
1408 */
1409 int
1410eval_to_number(expr)
1411 char_u *expr;
1412{
Bram Moolenaar33570922005-01-25 22:26:29 +00001413 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001415 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
1417 ++emsg_off;
1418
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 retval = -1;
1421 else
1422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001423 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426 --emsg_off;
1427
1428 return retval;
1429}
1430
Bram Moolenaara40058a2005-07-11 22:42:07 +00001431/*
1432 * Prepare v: variable "idx" to be used.
1433 * Save the current typeval in "save_tv".
1434 * When not used yet add the variable to the v: hashtable.
1435 */
1436 static void
1437prepare_vimvar(idx, save_tv)
1438 int idx;
1439 typval_T *save_tv;
1440{
1441 *save_tv = vimvars[idx].vv_tv;
1442 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1443 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1444}
1445
1446/*
1447 * Restore v: variable "idx" to typeval "save_tv".
1448 * When no longer defined, remove the variable from the v: hashtable.
1449 */
1450 static void
1451restore_vimvar(idx, save_tv)
1452 int idx;
1453 typval_T *save_tv;
1454{
1455 hashitem_T *hi;
1456
Bram Moolenaara40058a2005-07-11 22:42:07 +00001457 vimvars[idx].vv_tv = *save_tv;
1458 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1459 {
1460 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1461 if (HASHITEM_EMPTY(hi))
1462 EMSG2(_(e_intern2), "restore_vimvar()");
1463 else
1464 hash_remove(&vimvarht, hi);
1465 }
1466}
1467
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001468#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001469/*
1470 * Evaluate an expression to a list with suggestions.
1471 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001472 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001473 */
1474 list_T *
1475eval_spell_expr(badword, expr)
1476 char_u *badword;
1477 char_u *expr;
1478{
1479 typval_T save_val;
1480 typval_T rettv;
1481 list_T *list = NULL;
1482 char_u *p = skipwhite(expr);
1483
1484 /* Set "v:val" to the bad word. */
1485 prepare_vimvar(VV_VAL, &save_val);
1486 vimvars[VV_VAL].vv_type = VAR_STRING;
1487 vimvars[VV_VAL].vv_str = badword;
1488 if (p_verbose == 0)
1489 ++emsg_off;
1490
1491 if (eval1(&p, &rettv, TRUE) == OK)
1492 {
1493 if (rettv.v_type != VAR_LIST)
1494 clear_tv(&rettv);
1495 else
1496 list = rettv.vval.v_list;
1497 }
1498
1499 if (p_verbose == 0)
1500 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001501 restore_vimvar(VV_VAL, &save_val);
1502
1503 return list;
1504}
1505
1506/*
1507 * "list" is supposed to contain two items: a word and a number. Return the
1508 * word in "pp" and the number as the return value.
1509 * Return -1 if anything isn't right.
1510 * Used to get the good word and score from the eval_spell_expr() result.
1511 */
1512 int
1513get_spellword(list, pp)
1514 list_T *list;
1515 char_u **pp;
1516{
1517 listitem_T *li;
1518
1519 li = list->lv_first;
1520 if (li == NULL)
1521 return -1;
1522 *pp = get_tv_string(&li->li_tv);
1523
1524 li = li->li_next;
1525 if (li == NULL)
1526 return -1;
1527 return get_tv_number(&li->li_tv);
1528}
1529#endif
1530
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001531/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001532 * Top level evaluation function.
1533 * Returns an allocated typval_T with the result.
1534 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001535 */
1536 typval_T *
1537eval_expr(arg, nextcmd)
1538 char_u *arg;
1539 char_u **nextcmd;
1540{
1541 typval_T *tv;
1542
1543 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001544 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001545 {
1546 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001547 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 }
1549
1550 return tv;
1551}
1552
1553
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001555 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001556 * Uses argv[argc] for the function arguments. Only Number and String
1557 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001560 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001561call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 char_u *func;
1563 int argc;
1564 char_u **argv;
1565 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001566 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Bram Moolenaar33570922005-01-25 22:26:29 +00001569 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 long n;
1571 int len;
1572 int i;
1573 int doesrange;
1574 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001577 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001579 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581 for (i = 0; i < argc; i++)
1582 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001583 /* Pass a NULL or empty argument as an empty string */
1584 if (argv[i] == NULL || *argv[i] == NUL)
1585 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001586 argvars[i].v_type = VAR_STRING;
1587 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001588 continue;
1589 }
1590
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001591 if (str_arg_only)
1592 len = 0;
1593 else
1594 /* Recognize a number argument, the others must be strings. */
1595 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (len != 0 && len == (int)STRLEN(argv[i]))
1597 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001598 argvars[i].v_type = VAR_NUMBER;
1599 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 else
1602 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001603 argvars[i].v_type = VAR_STRING;
1604 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606 }
1607
1608 if (safe)
1609 {
1610 save_funccalp = save_funccal();
1611 ++sandbox;
1612 }
1613
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1615 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 if (safe)
1619 {
1620 --sandbox;
1621 restore_funccal(save_funccalp);
1622 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 vim_free(argvars);
1624
1625 if (ret == FAIL)
1626 clear_tv(rettv);
1627
1628 return ret;
1629}
1630
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001631/*
1632 * Call vimL function "func" and return the result as a number.
1633 * Returns -1 when calling the function fails.
1634 * Uses argv[argc] for the function arguments.
1635 */
1636 long
1637call_func_retnr(func, argc, argv, safe)
1638 char_u *func;
1639 int argc;
1640 char_u **argv;
1641 int safe; /* use the sandbox */
1642{
1643 typval_T rettv;
1644 long retval;
1645
1646 /* All arguments are passed as strings, no conversion to number. */
1647 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1648 return -1;
1649
1650 retval = get_tv_number_chk(&rettv, NULL);
1651 clear_tv(&rettv);
1652 return retval;
1653}
1654
1655#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1656 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1657
Bram Moolenaar4f688582007-07-24 12:34:30 +00001658# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001660 * Call vimL function "func" and return the result as a string.
1661 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001662 * Uses argv[argc] for the function arguments.
1663 */
1664 void *
1665call_func_retstr(func, argc, argv, safe)
1666 char_u *func;
1667 int argc;
1668 char_u **argv;
1669 int safe; /* use the sandbox */
1670{
1671 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001672 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001674 /* All arguments are passed as strings, no conversion to number. */
1675 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 return NULL;
1677
1678 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 return retval;
1681}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001682# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001684/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001685 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001687 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688 */
1689 void *
1690call_func_retlist(func, argc, argv, safe)
1691 char_u *func;
1692 int argc;
1693 char_u **argv;
1694 int safe; /* use the sandbox */
1695{
1696 typval_T rettv;
1697
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001698 /* All arguments are passed as strings, no conversion to number. */
1699 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001700 return NULL;
1701
1702 if (rettv.v_type != VAR_LIST)
1703 {
1704 clear_tv(&rettv);
1705 return NULL;
1706 }
1707
1708 return rettv.vval.v_list;
1709}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710#endif
1711
1712/*
1713 * Save the current function call pointer, and set it to NULL.
1714 * Used when executing autocommands and for ":source".
1715 */
1716 void *
1717save_funccal()
1718{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001719 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 current_funccal = NULL;
1722 return (void *)fc;
1723}
1724
1725 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001726restore_funccal(vfc)
1727 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729 funccall_T *fc = (funccall_T *)vfc;
1730
1731 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732}
1733
Bram Moolenaar05159a02005-02-26 23:04:13 +00001734#if defined(FEAT_PROFILE) || defined(PROTO)
1735/*
1736 * Prepare profiling for entering a child or something else that is not
1737 * counted for the script/function itself.
1738 * Should always be called in pair with prof_child_exit().
1739 */
1740 void
1741prof_child_enter(tm)
1742 proftime_T *tm; /* place to store waittime */
1743{
1744 funccall_T *fc = current_funccal;
1745
1746 if (fc != NULL && fc->func->uf_profiling)
1747 profile_start(&fc->prof_child);
1748 script_prof_save(tm);
1749}
1750
1751/*
1752 * Take care of time spent in a child.
1753 * Should always be called after prof_child_enter().
1754 */
1755 void
1756prof_child_exit(tm)
1757 proftime_T *tm; /* where waittime was stored */
1758{
1759 funccall_T *fc = current_funccal;
1760
1761 if (fc != NULL && fc->func->uf_profiling)
1762 {
1763 profile_end(&fc->prof_child);
1764 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1765 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1766 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1767 }
1768 script_prof_restore(tm);
1769}
1770#endif
1771
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#ifdef FEAT_FOLDING
1774/*
1775 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1776 * it in "*cp". Doesn't give error messages.
1777 */
1778 int
1779eval_foldexpr(arg, cp)
1780 char_u *arg;
1781 int *cp;
1782{
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 int retval;
1785 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001786 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1787 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
1789 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001790 if (use_sandbox)
1791 ++sandbox;
1792 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 retval = 0;
1796 else
1797 {
1798 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799 if (tv.v_type == VAR_NUMBER)
1800 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001801 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 retval = 0;
1803 else
1804 {
1805 /* If the result is a string, check if there is a non-digit before
1806 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (!VIM_ISDIGIT(*s) && *s != '-')
1809 *cp = *s++;
1810 retval = atol((char *)s);
1811 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
1814 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001815 if (use_sandbox)
1816 --sandbox;
1817 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
1819 return retval;
1820}
1821#endif
1822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 * ":let" list all variable values
1825 * ":let var1 var2" list variable values
1826 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001827 * ":let var += expr" assignment command.
1828 * ":let var -= expr" assignment command.
1829 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 */
1832 void
1833ex_let(eap)
1834 exarg_T *eap;
1835{
1836 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 int var_count = 0;
1841 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001842 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001843 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001844 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 argend = skip_var_list(arg, &var_count, &semicolon);
1847 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001849 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1850 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001851 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001854 /*
1855 * ":let" without "=": list variables
1856 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 if (*arg == '[')
1858 EMSG(_(e_invarg));
1859 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001861 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001865 list_glob_vars(&first);
1866 list_buf_vars(&first);
1867 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001868#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001869 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001870#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001871 list_script_vars(&first);
1872 list_func_vars(&first);
1873 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 eap->nextcmd = check_nextcmd(arg);
1876 }
1877 else
1878 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 op[0] = '=';
1880 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001881 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882 {
1883 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1884 op[0] = expr[-1]; /* +=, -= or .= */
1885 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (eap->skip)
1889 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (eap->skip)
1892 {
1893 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 --emsg_skip;
1896 }
1897 else if (i != FAIL)
1898 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
1903 }
1904}
1905
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906/*
1907 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1908 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 * When "nextchars" is not NULL it points to a string with characters that
1910 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1911 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 * Returns OK or FAIL;
1913 */
1914 static int
1915ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1916 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001917 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 int copy; /* copy values from "tv", don't move */
1919 int semicolon; /* from skip_var_list() */
1920 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922{
1923 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001926 listitem_T *item;
1927 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928
1929 if (*arg != '[')
1930 {
1931 /*
1932 * ":let var = expr" or ":for var in list"
1933 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 return FAIL;
1936 return OK;
1937 }
1938
1939 /*
1940 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1941 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001942 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 {
1944 EMSG(_(e_listreq));
1945 return FAIL;
1946 }
1947
1948 i = list_len(l);
1949 if (semicolon == 0 && var_count < i)
1950 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001951 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 return FAIL;
1953 }
1954 if (var_count - semicolon > i)
1955 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001956 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 return FAIL;
1958 }
1959
1960 item = l->lv_first;
1961 while (*arg != ']')
1962 {
1963 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 item = item->li_next;
1966 if (arg == NULL)
1967 return FAIL;
1968
1969 arg = skipwhite(arg);
1970 if (*arg == ';')
1971 {
1972 /* Put the rest of the list (may be empty) in the var after ';'.
1973 * Create a new list for this. */
1974 l = list_alloc();
1975 if (l == NULL)
1976 return FAIL;
1977 while (item != NULL)
1978 {
1979 list_append_tv(l, &item->li_tv);
1980 item = item->li_next;
1981 }
1982
1983 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001984 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 ltv.vval.v_list = l;
1986 l->lv_refcount = 1;
1987
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001988 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1989 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 clear_tv(&ltv);
1991 if (arg == NULL)
1992 return FAIL;
1993 break;
1994 }
1995 else if (*arg != ',' && *arg != ']')
1996 {
1997 EMSG2(_(e_intern2), "ex_let_vars()");
1998 return FAIL;
1999 }
2000 }
2001
2002 return OK;
2003}
2004
2005/*
2006 * Skip over assignable variable "var" or list of variables "[var, var]".
2007 * Used for ":let varvar = expr" and ":for varvar in expr".
2008 * For "[var, var]" increment "*var_count" for each variable.
2009 * for "[var, var; var]" set "semicolon".
2010 * Return NULL for an error.
2011 */
2012 static char_u *
2013skip_var_list(arg, var_count, semicolon)
2014 char_u *arg;
2015 int *var_count;
2016 int *semicolon;
2017{
2018 char_u *p, *s;
2019
2020 if (*arg == '[')
2021 {
2022 /* "[var, var]": find the matching ']'. */
2023 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002024 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 {
2026 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2027 s = skip_var_one(p);
2028 if (s == p)
2029 {
2030 EMSG2(_(e_invarg2), p);
2031 return NULL;
2032 }
2033 ++*var_count;
2034
2035 p = skipwhite(s);
2036 if (*p == ']')
2037 break;
2038 else if (*p == ';')
2039 {
2040 if (*semicolon == 1)
2041 {
2042 EMSG(_("Double ; in list of variables"));
2043 return NULL;
2044 }
2045 *semicolon = 1;
2046 }
2047 else if (*p != ',')
2048 {
2049 EMSG2(_(e_invarg2), p);
2050 return NULL;
2051 }
2052 }
2053 return p + 1;
2054 }
2055 else
2056 return skip_var_one(arg);
2057}
2058
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002059/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002060 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002061 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002063 static char_u *
2064skip_var_one(arg)
2065 char_u *arg;
2066{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002067 if (*arg == '@' && arg[1] != NUL)
2068 return arg + 2;
2069 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2070 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002071}
2072
Bram Moolenaara7043832005-01-21 11:56:39 +00002073/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 * List variables for hashtab "ht" with prefix "prefix".
2075 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002077 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002078list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002082 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083{
Bram Moolenaar33570922005-01-25 22:26:29 +00002084 hashitem_T *hi;
2085 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 int todo;
2087
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002088 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2090 {
2091 if (!HASHITEM_EMPTY(hi))
2092 {
2093 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002094 di = HI2DI(hi);
2095 if (empty || di->di_tv.v_type != VAR_STRING
2096 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002097 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 }
2099 }
2100}
2101
2102/*
2103 * List global variables.
2104 */
2105 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002106list_glob_vars(first)
2107 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002108{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002110}
2111
2112/*
2113 * List buffer variables.
2114 */
2115 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116list_buf_vars(first)
2117 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002118{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119 char_u numbuf[NUMBUFLEN];
2120
Bram Moolenaar429fa852013-04-15 12:27:36 +02002121 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123
2124 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2126 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002127}
2128
2129/*
2130 * List window variables.
2131 */
2132 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133list_win_vars(first)
2134 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002135{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002136 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002138}
2139
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002140#ifdef FEAT_WINDOWS
2141/*
2142 * List tab page variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_tab_vars(first)
2146 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002148 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002150}
2151#endif
2152
Bram Moolenaara7043832005-01-21 11:56:39 +00002153/*
2154 * List Vim variables.
2155 */
2156 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157list_vim_vars(first)
2158 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161}
2162
2163/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164 * List script-local variables, if there is a script.
2165 */
2166 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167list_script_vars(first)
2168 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002169{
2170 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2172 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002173}
2174
2175/*
2176 * List function variables, if there is a function.
2177 */
2178 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179list_func_vars(first)
2180 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002181{
2182 if (current_funccal != NULL)
2183 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185}
2186
2187/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 * List variables in "arg".
2189 */
2190 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 exarg_T *eap;
2193 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195{
2196 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002199 char_u *name_start;
2200 char_u *arg_subsc;
2201 char_u *tofree;
2202 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203
2204 while (!ends_excmd(*arg) && !got_int)
2205 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002208 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2210 {
2211 emsg_severe = TRUE;
2212 EMSG(_(e_trailing));
2213 break;
2214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 /* get_name_len() takes care of expanding curly braces */
2219 name_start = name = arg;
2220 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2221 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 /* This is mainly to keep test 49 working: when expanding
2224 * curly braces fails overrule the exception error message. */
2225 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002227 emsg_severe = TRUE;
2228 EMSG2(_(e_invarg2), arg);
2229 break;
2230 }
2231 error = TRUE;
2232 }
2233 else
2234 {
2235 if (tofree != NULL)
2236 name = tofree;
2237 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 else
2240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* handle d.key, l[idx], f(expr) */
2242 arg_subsc = arg;
2243 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002250 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 case 'g': list_glob_vars(first); break;
2252 case 'b': list_buf_vars(first); break;
2253 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002254#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002255 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002256#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002257 case 'v': list_vim_vars(first); break;
2258 case 's': list_script_vars(first); break;
2259 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 default:
2261 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 }
2264 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 {
2266 char_u numbuf[NUMBUFLEN];
2267 char_u *tf;
2268 int c;
2269 char_u *s;
2270
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002271 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 c = *arg;
2273 *arg = NUL;
2274 list_one_var_a((char_u *)"",
2275 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002276 tv.v_type,
2277 s == NULL ? (char_u *)"" : s,
2278 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 *arg = c;
2280 vim_free(tf);
2281 }
2282 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286
2287 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289
2290 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
2292
2293 return arg;
2294}
2295
2296/*
2297 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2298 * Returns a pointer to the char just after the var name.
2299 * Returns NULL if there is an error.
2300 */
2301 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002304 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002306 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308{
2309 int c1;
2310 char_u *name;
2311 char_u *p;
2312 char_u *arg_end = NULL;
2313 int len;
2314 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316
2317 /*
2318 * ":let $VAR = expr": Set environment variable.
2319 */
2320 if (*arg == '$')
2321 {
2322 /* Find the end of the name. */
2323 ++arg;
2324 name = arg;
2325 len = get_env_len(&arg);
2326 if (len == 0)
2327 EMSG2(_(e_invarg2), name - 1);
2328 else
2329 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 if (op != NULL && (*op == '+' || *op == '-'))
2331 EMSG2(_(e_letwrong), op);
2332 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002333 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002335 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 {
2337 c1 = name[len];
2338 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002339 p = get_tv_string_chk(tv);
2340 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341 {
2342 int mustfree = FALSE;
2343 char_u *s = vim_getenv(name, &mustfree);
2344
2345 if (s != NULL)
2346 {
2347 p = tofree = concat_str(s, p);
2348 if (mustfree)
2349 vim_free(s);
2350 }
2351 }
2352 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002353 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 if (STRICMP(name, "HOME") == 0)
2356 init_homedir();
2357 else if (didset_vim && STRICMP(name, "VIM") == 0)
2358 didset_vim = FALSE;
2359 else if (didset_vimruntime
2360 && STRICMP(name, "VIMRUNTIME") == 0)
2361 didset_vimruntime = FALSE;
2362 arg_end = arg;
2363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366 }
2367 }
2368 }
2369
2370 /*
2371 * ":let &option = expr": Set option value.
2372 * ":let &l:option = expr": Set local option value.
2373 * ":let &g:option = expr": Set global option value.
2374 */
2375 else if (*arg == '&')
2376 {
2377 /* Find the end of the name. */
2378 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002379 if (p == NULL || (endchars != NULL
2380 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 EMSG(_(e_letunexp));
2382 else
2383 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 long n;
2385 int opt_type;
2386 long numval;
2387 char_u *stringval = NULL;
2388 char_u *s;
2389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 c1 = *p;
2391 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392
2393 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 s = get_tv_string_chk(tv); /* != NULL if number or string */
2395 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 {
2397 opt_type = get_option_value(arg, &numval,
2398 &stringval, opt_flags);
2399 if ((opt_type == 1 && *op == '.')
2400 || (opt_type == 0 && *op != '.'))
2401 EMSG2(_(e_letwrong), op);
2402 else
2403 {
2404 if (opt_type == 1) /* number */
2405 {
2406 if (*op == '+')
2407 n = numval + n;
2408 else
2409 n = numval - n;
2410 }
2411 else if (opt_type == 0 && stringval != NULL) /* string */
2412 {
2413 s = concat_str(stringval, s);
2414 vim_free(stringval);
2415 stringval = s;
2416 }
2417 }
2418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002419 if (s != NULL)
2420 {
2421 set_option_value(arg, n, s, opt_flags);
2422 arg_end = p;
2423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002425 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 }
2427 }
2428
2429 /*
2430 * ":let @r = expr": Set register contents.
2431 */
2432 else if (*arg == '@')
2433 {
2434 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 if (op != NULL && (*op == '+' || *op == '-'))
2436 EMSG2(_(e_letwrong), op);
2437 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002438 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 EMSG(_(e_letunexp));
2440 else
2441 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002442 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 char_u *s;
2444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 p = get_tv_string_chk(tv);
2446 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002448 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 if (s != NULL)
2450 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002451 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 vim_free(s);
2453 }
2454 }
2455 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 arg_end = arg + 1;
2459 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002460 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 }
2462 }
2463
2464 /*
2465 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002468 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002470 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002472 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2476 EMSG(_(e_letunexp));
2477 else
2478 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 arg_end = p;
2481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485
2486 else
2487 EMSG2(_(e_invarg2), arg);
2488
2489 return arg_end;
2490}
2491
2492/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2494 */
2495 static int
2496check_changedtick(arg)
2497 char_u *arg;
2498{
2499 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2500 {
2501 EMSG2(_(e_readonlyvar), arg);
2502 return TRUE;
2503 }
2504 return FALSE;
2505}
2506
2507/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 * Get an lval: variable, Dict item or List item that can be assigned a value
2509 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2510 * "name.key", "name.key[expr]" etc.
2511 * Indexing only works if "name" is an existing List or Dictionary.
2512 * "name" points to the start of the name.
2513 * If "rettv" is not NULL it points to the value to be assigned.
2514 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2515 * wrong; must end in space or cmd separator.
2516 *
2517 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002518 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 */
2521 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002522get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002524 typval_T *rettv;
2525 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 int unlet;
2527 int skip;
2528 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002529 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 char_u *expr_start, *expr_end;
2533 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002534 dictitem_T *v;
2535 typval_T var1;
2536 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545
2546 if (skip)
2547 {
2548 /* When skipping just find the end of the name. */
2549 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002550 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 }
2552
2553 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002554 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (expr_start != NULL)
2556 {
2557 /* Don't expand the name when we already know there is an error. */
2558 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2559 && *p != '[' && *p != '.')
2560 {
2561 EMSG(_(e_trailing));
2562 return NULL;
2563 }
2564
2565 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2566 if (lp->ll_exp_name == NULL)
2567 {
2568 /* Report an invalid expression in braces, unless the
2569 * expression evaluation has been cancelled due to an
2570 * aborting error, an interrupt, or an exception. */
2571 if (!aborting() && !quiet)
2572 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002573 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 EMSG2(_(e_invarg2), name);
2575 return NULL;
2576 }
2577 }
2578 lp->ll_name = lp->ll_exp_name;
2579 }
2580 else
2581 lp->ll_name = name;
2582
2583 /* Without [idx] or .key we are done. */
2584 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2585 return p;
2586
2587 cc = *p;
2588 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002589 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (v == NULL && !quiet)
2591 EMSG2(_(e_undefvar), lp->ll_name);
2592 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 if (v == NULL)
2594 return NULL;
2595
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 /*
2597 * Loop until no more [idx] or .key is following.
2598 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002599 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002601 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2603 && !(lp->ll_tv->v_type == VAR_DICT
2604 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (!quiet)
2607 EMSG(_("E689: Can only index a List or Dictionary"));
2608 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (!quiet)
2613 EMSG(_("E708: [:] must come last"));
2614 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002615 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002616
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 len = -1;
2618 if (*p == '.')
2619 {
2620 key = p + 1;
2621 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2622 ;
2623 if (len == 0)
2624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (!quiet)
2626 EMSG(_(e_emptykey));
2627 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 }
2629 p = key + len;
2630 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 else
2632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 if (*p == ':')
2636 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 else
2638 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 empty1 = FALSE;
2640 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002642 if (get_tv_string_chk(&var1) == NULL)
2643 {
2644 /* not a number or string */
2645 clear_tv(&var1);
2646 return NULL;
2647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 }
2649
2650 /* Optionally get the second index [ :expr]. */
2651 if (*p == ':')
2652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002656 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002657 if (!empty1)
2658 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (rettv != NULL && (rettv->v_type != VAR_LIST
2662 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
2665 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 if (!empty1)
2667 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
2670 p = skipwhite(p + 1);
2671 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 else
2674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2677 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002682 if (get_tv_string_chk(&var2) == NULL)
2683 {
2684 /* not a number or string */
2685 if (!empty1)
2686 clear_tv(&var1);
2687 clear_tv(&var2);
2688 return NULL;
2689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (!quiet)
2699 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 if (!empty1)
2701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
2706
2707 /* Skip to past ']'. */
2708 ++p;
2709 }
2710
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 {
2713 if (len == -1)
2714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002716 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (*key == NUL)
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
2724 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 lp->ll_list = NULL;
2726 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002727 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002728
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002729 /* When assigning to a scope dictionary check that a function and
2730 * variable name is valid (only variable name unless it is l: or
2731 * g: dictionary). Disallow overwriting a builtin function. */
2732 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002733 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002734 int prevval;
2735 int wrong;
2736
2737 if (len != -1)
2738 {
2739 prevval = key[len];
2740 key[len] = NUL;
2741 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002742 else
2743 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002744 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2745 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002746 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002747 || !valid_varname(key);
2748 if (len != -1)
2749 key[len] = prevval;
2750 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002751 return NULL;
2752 }
2753
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756 /* Can't add "v:" variable. */
2757 if (lp->ll_dict == &vimvardict)
2758 {
2759 EMSG2(_(e_illvar), name);
2760 return NULL;
2761 }
2762
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002763 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002767 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 if (len == -1)
2769 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 }
2772 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 if (len == -1)
2777 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 p = NULL;
2780 break;
2781 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782 /* existing variable, need to check if it can be changed */
2783 else if (var_check_ro(lp->ll_di->di_flags, name))
2784 return NULL;
2785
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 if (len == -1)
2787 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 }
2790 else
2791 {
2792 /*
2793 * Get the number and item for the only or first index of the List.
2794 */
2795 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 else
2798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002799 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 clear_tv(&var1);
2801 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002802 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_list = lp->ll_tv->vval.v_list;
2804 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2805 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002807 if (lp->ll_n1 < 0)
2808 {
2809 lp->ll_n1 = 0;
2810 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2811 }
2812 }
2813 if (lp->ll_li == NULL)
2814 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002817 if (!quiet)
2818 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 }
2821
2822 /*
2823 * May need to find the item or absolute index for the second
2824 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 * When no index given: "lp->ll_empty2" is TRUE.
2826 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002830 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002836 {
2837 if (!quiet)
2838 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002840 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 }
2843
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2845 if (lp->ll_n1 < 0)
2846 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2847 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002848 {
2849 if (!quiet)
2850 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002852 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002853 }
2854
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002856 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002857 }
2858
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 return p;
2860}
2861
2862/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002863 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 */
2865 static void
2866clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868{
2869 vim_free(lp->ll_exp_name);
2870 vim_free(lp->ll_newkey);
2871}
2872
2873/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002874 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002876 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 */
2878 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002879set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002880 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002884 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885{
2886 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002887 listitem_T *ri;
2888 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889
2890 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002891 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 cc = *endp;
2895 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 if (op != NULL && *op != '=')
2897 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002899
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002900 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002901 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002902 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903 {
2904 if (tv_op(&tv, rettv, op) == OK)
2905 set_var(lp->ll_name, &tv, FALSE);
2906 clear_tv(&tv);
2907 }
2908 }
2909 else
2910 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002914 else if (tv_check_lock(lp->ll_newkey == NULL
2915 ? lp->ll_tv->v_lock
2916 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2917 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 else if (lp->ll_range)
2919 {
2920 /*
2921 * Assign the List values to the list items.
2922 */
2923 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002924 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 if (op != NULL && *op != '=')
2926 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2927 else
2928 {
2929 clear_tv(&lp->ll_li->li_tv);
2930 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2931 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 ri = ri->li_next;
2933 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2934 break;
2935 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002936 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002938 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 ri = NULL;
2941 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002942 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 lp->ll_li = lp->ll_li->li_next;
2945 ++lp->ll_n1;
2946 }
2947 if (ri != NULL)
2948 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 else if (lp->ll_empty2
2950 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 : lp->ll_n1 != lp->ll_n2)
2952 EMSG(_("E711: List value has not enough items"));
2953 }
2954 else
2955 {
2956 /*
2957 * Assign to a List or Dictionary item.
2958 */
2959 if (lp->ll_newkey != NULL)
2960 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 if (op != NULL && *op != '=')
2962 {
2963 EMSG2(_(e_letwrong), op);
2964 return;
2965 }
2966
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002968 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 if (di == NULL)
2970 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002971 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2972 {
2973 vim_free(di);
2974 return;
2975 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002977 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002978 else if (op != NULL && *op != '=')
2979 {
2980 tv_op(lp->ll_tv, rettv, op);
2981 return;
2982 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002985
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986 /*
2987 * Assign the value to the variable or list item.
2988 */
2989 if (copy)
2990 copy_tv(rettv, lp->ll_tv);
2991 else
2992 {
2993 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002994 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002996 }
2997 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002998}
2999
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003000/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3002 * Returns OK or FAIL.
3003 */
3004 static int
3005tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003006 typval_T *tv1;
3007 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003008 char_u *op;
3009{
3010 long n;
3011 char_u numbuf[NUMBUFLEN];
3012 char_u *s;
3013
3014 /* Can't do anything with a Funcref or a Dict on the right. */
3015 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3016 {
3017 switch (tv1->v_type)
3018 {
3019 case VAR_DICT:
3020 case VAR_FUNC:
3021 break;
3022
3023 case VAR_LIST:
3024 if (*op != '+' || tv2->v_type != VAR_LIST)
3025 break;
3026 /* List += List */
3027 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3028 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3029 return OK;
3030
3031 case VAR_NUMBER:
3032 case VAR_STRING:
3033 if (tv2->v_type == VAR_LIST)
3034 break;
3035 if (*op == '+' || *op == '-')
3036 {
3037 /* nr += nr or nr -= nr*/
3038 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039#ifdef FEAT_FLOAT
3040 if (tv2->v_type == VAR_FLOAT)
3041 {
3042 float_T f = n;
3043
3044 if (*op == '+')
3045 f += tv2->vval.v_float;
3046 else
3047 f -= tv2->vval.v_float;
3048 clear_tv(tv1);
3049 tv1->v_type = VAR_FLOAT;
3050 tv1->vval.v_float = f;
3051 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003053#endif
3054 {
3055 if (*op == '+')
3056 n += get_tv_number(tv2);
3057 else
3058 n -= get_tv_number(tv2);
3059 clear_tv(tv1);
3060 tv1->v_type = VAR_NUMBER;
3061 tv1->vval.v_number = n;
3062 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003063 }
3064 else
3065 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003066 if (tv2->v_type == VAR_FLOAT)
3067 break;
3068
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 /* str .= str */
3070 s = get_tv_string(tv1);
3071 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3072 clear_tv(tv1);
3073 tv1->v_type = VAR_STRING;
3074 tv1->vval.v_string = s;
3075 }
3076 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003077
3078#ifdef FEAT_FLOAT
3079 case VAR_FLOAT:
3080 {
3081 float_T f;
3082
3083 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3084 && tv2->v_type != VAR_NUMBER
3085 && tv2->v_type != VAR_STRING))
3086 break;
3087 if (tv2->v_type == VAR_FLOAT)
3088 f = tv2->vval.v_float;
3089 else
3090 f = get_tv_number(tv2);
3091 if (*op == '+')
3092 tv1->vval.v_float += f;
3093 else
3094 tv1->vval.v_float -= f;
3095 }
3096 return OK;
3097#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003098 }
3099 }
3100
3101 EMSG2(_(e_letwrong), op);
3102 return FAIL;
3103}
3104
3105/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003106 * Add a watcher to a list.
3107 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003108 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003109list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003110 list_T *l;
3111 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003112{
3113 lw->lw_next = l->lv_watch;
3114 l->lv_watch = lw;
3115}
3116
3117/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003118 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119 * No warning when it isn't found...
3120 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003121 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003123 list_T *l;
3124 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003125{
Bram Moolenaar33570922005-01-25 22:26:29 +00003126 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127
3128 lwp = &l->lv_watch;
3129 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3130 {
3131 if (lw == lwrem)
3132 {
3133 *lwp = lw->lw_next;
3134 break;
3135 }
3136 lwp = &lw->lw_next;
3137 }
3138}
3139
3140/*
3141 * Just before removing an item from a list: advance watchers to the next
3142 * item.
3143 */
3144 static void
3145list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003146 list_T *l;
3147 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148{
Bram Moolenaar33570922005-01-25 22:26:29 +00003149 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150
3151 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3152 if (lw->lw_item == item)
3153 lw->lw_item = item->li_next;
3154}
3155
3156/*
3157 * Evaluate the expression used in a ":for var in expr" command.
3158 * "arg" points to "var".
3159 * Set "*errp" to TRUE for an error, FALSE otherwise;
3160 * Return a pointer that holds the info. Null when there is an error.
3161 */
3162 void *
3163eval_for_line(arg, errp, nextcmdp, skip)
3164 char_u *arg;
3165 int *errp;
3166 char_u **nextcmdp;
3167 int skip;
3168{
Bram Moolenaar33570922005-01-25 22:26:29 +00003169 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003171 typval_T tv;
3172 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173
3174 *errp = TRUE; /* default: there is an error */
3175
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 if (fi == NULL)
3178 return NULL;
3179
3180 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3181 if (expr == NULL)
3182 return fi;
3183
3184 expr = skipwhite(expr);
3185 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3186 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003187 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 return fi;
3189 }
3190
3191 if (skip)
3192 ++emsg_skip;
3193 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3194 {
3195 *errp = FALSE;
3196 if (!skip)
3197 {
3198 l = tv.vval.v_list;
3199 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003200 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003202 clear_tv(&tv);
3203 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204 else
3205 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003206 /* No need to increment the refcount, it's already set for the
3207 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 fi->fi_list = l;
3209 list_add_watch(l, &fi->fi_lw);
3210 fi->fi_lw.lw_item = l->lv_first;
3211 }
3212 }
3213 }
3214 if (skip)
3215 --emsg_skip;
3216
3217 return fi;
3218}
3219
3220/*
3221 * Use the first item in a ":for" list. Advance to the next.
3222 * Assign the values to the variable (list). "arg" points to the first one.
3223 * Return TRUE when a valid item was found, FALSE when at end of list or
3224 * something wrong.
3225 */
3226 int
3227next_for_item(fi_void, arg)
3228 void *fi_void;
3229 char_u *arg;
3230{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003231 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234
3235 item = fi->fi_lw.lw_item;
3236 if (item == NULL)
3237 result = FALSE;
3238 else
3239 {
3240 fi->fi_lw.lw_item = item->li_next;
3241 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3242 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3243 }
3244 return result;
3245}
3246
3247/*
3248 * Free the structure used to store info used by ":for".
3249 */
3250 void
3251free_for_info(fi_void)
3252 void *fi_void;
3253{
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003256 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003257 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003259 list_unref(fi->fi_list);
3260 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 vim_free(fi);
3262}
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3265
3266 void
3267set_context_for_expression(xp, arg, cmdidx)
3268 expand_T *xp;
3269 char_u *arg;
3270 cmdidx_T cmdidx;
3271{
3272 int got_eq = FALSE;
3273 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 if (cmdidx == CMD_let)
3277 {
3278 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003279 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 {
3281 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003282 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 {
3284 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003285 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 if (vim_iswhite(*p))
3287 break;
3288 }
3289 return;
3290 }
3291 }
3292 else
3293 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3294 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 while ((xp->xp_pattern = vim_strpbrk(arg,
3296 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3297 {
3298 c = *xp->xp_pattern;
3299 if (c == '&')
3300 {
3301 c = xp->xp_pattern[1];
3302 if (c == '&')
3303 {
3304 ++xp->xp_pattern;
3305 xp->xp_context = cmdidx != CMD_let || got_eq
3306 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3307 }
3308 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003311 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3312 xp->xp_pattern += 2;
3313
3314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 }
3316 else if (c == '$')
3317 {
3318 /* environment variable */
3319 xp->xp_context = EXPAND_ENV_VARS;
3320 }
3321 else if (c == '=')
3322 {
3323 got_eq = TRUE;
3324 xp->xp_context = EXPAND_EXPRESSION;
3325 }
3326 else if (c == '<'
3327 && xp->xp_context == EXPAND_FUNCTIONS
3328 && vim_strchr(xp->xp_pattern, '(') == NULL)
3329 {
3330 /* Function name can start with "<SNR>" */
3331 break;
3332 }
3333 else if (cmdidx != CMD_let || got_eq)
3334 {
3335 if (c == '"') /* string */
3336 {
3337 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3338 if (c == '\\' && xp->xp_pattern[1] != NUL)
3339 ++xp->xp_pattern;
3340 xp->xp_context = EXPAND_NOTHING;
3341 }
3342 else if (c == '\'') /* literal string */
3343 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003344 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3346 /* skip */ ;
3347 xp->xp_context = EXPAND_NOTHING;
3348 }
3349 else if (c == '|')
3350 {
3351 if (xp->xp_pattern[1] == '|')
3352 {
3353 ++xp->xp_pattern;
3354 xp->xp_context = EXPAND_EXPRESSION;
3355 }
3356 else
3357 xp->xp_context = EXPAND_COMMANDS;
3358 }
3359 else
3360 xp->xp_context = EXPAND_EXPRESSION;
3361 }
3362 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003363 /* Doesn't look like something valid, expand as an expression
3364 * anyway. */
3365 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 arg = xp->xp_pattern;
3367 if (*arg != NUL)
3368 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3369 /* skip */ ;
3370 }
3371 xp->xp_pattern = arg;
3372}
3373
3374#endif /* FEAT_CMDL_COMPL */
3375
3376/*
3377 * ":1,25call func(arg1, arg2)" function call.
3378 */
3379 void
3380ex_call(eap)
3381 exarg_T *eap;
3382{
3383 char_u *arg = eap->arg;
3384 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003386 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003388 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 linenr_T lnum;
3390 int doesrange;
3391 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003392 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003394 if (eap->skip)
3395 {
3396 /* trans_function_name() doesn't work well when skipping, use eval0()
3397 * instead to skip to any following command, e.g. for:
3398 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003399 ++emsg_skip;
3400 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3401 clear_tv(&rettv);
3402 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003403 return;
3404 }
3405
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003406 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003407 if (fudi.fd_newkey != NULL)
3408 {
3409 /* Still need to give an error message for missing key. */
3410 EMSG2(_(e_dictkey), fudi.fd_newkey);
3411 vim_free(fudi.fd_newkey);
3412 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003413 if (tofree == NULL)
3414 return;
3415
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003416 /* Increase refcount on dictionary, it could get deleted when evaluating
3417 * the arguments. */
3418 if (fudi.fd_dict != NULL)
3419 ++fudi.fd_dict->dv_refcount;
3420
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003421 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003422 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003423 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
Bram Moolenaar532c7802005-01-27 14:44:31 +00003425 /* Skip white space to allow ":call func ()". Not good, but required for
3426 * backward compatibility. */
3427 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003428 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430 if (*startarg != '(')
3431 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003432 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 goto end;
3434 }
3435
3436 /*
3437 * When skipping, evaluate the function once, to find the end of the
3438 * arguments.
3439 * When the function takes a range, this is discovered after the first
3440 * call, and the loop is broken.
3441 */
3442 if (eap->skip)
3443 {
3444 ++emsg_skip;
3445 lnum = eap->line2; /* do it once, also with an invalid range */
3446 }
3447 else
3448 lnum = eap->line1;
3449 for ( ; lnum <= eap->line2; ++lnum)
3450 {
3451 if (!eap->skip && eap->addr_count > 0)
3452 {
3453 curwin->w_cursor.lnum = lnum;
3454 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003455#ifdef FEAT_VIRTUALEDIT
3456 curwin->w_cursor.coladd = 0;
3457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
3459 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003460 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003461 eap->line1, eap->line2, &doesrange,
3462 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
3464 failed = TRUE;
3465 break;
3466 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003467
3468 /* Handle a function returning a Funcref, Dictionary or List. */
3469 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3470 {
3471 failed = TRUE;
3472 break;
3473 }
3474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003475 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (doesrange || eap->skip)
3477 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003478
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003480 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003481 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003482 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (aborting())
3484 break;
3485 }
3486 if (eap->skip)
3487 --emsg_skip;
3488
3489 if (!failed)
3490 {
3491 /* Check for trailing illegal characters and a following command. */
3492 if (!ends_excmd(*arg))
3493 {
3494 emsg_severe = TRUE;
3495 EMSG(_(e_trailing));
3496 }
3497 else
3498 eap->nextcmd = check_nextcmd(arg);
3499 }
3500
3501end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003502 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003503 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504}
3505
3506/*
3507 * ":unlet[!] var1 ... " command.
3508 */
3509 void
3510ex_unlet(eap)
3511 exarg_T *eap;
3512{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003513 ex_unletlock(eap, eap->arg, 0);
3514}
3515
3516/*
3517 * ":lockvar" and ":unlockvar" commands
3518 */
3519 void
3520ex_lockvar(eap)
3521 exarg_T *eap;
3522{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003524 int deep = 2;
3525
3526 if (eap->forceit)
3527 deep = -1;
3528 else if (vim_isdigit(*arg))
3529 {
3530 deep = getdigits(&arg);
3531 arg = skipwhite(arg);
3532 }
3533
3534 ex_unletlock(eap, arg, deep);
3535}
3536
3537/*
3538 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3539 */
3540 static void
3541ex_unletlock(eap, argstart, deep)
3542 exarg_T *eap;
3543 char_u *argstart;
3544 int deep;
3545{
3546 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003549 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
3551 do
3552 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003553 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003554 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3555 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003556 if (lv.ll_name == NULL)
3557 error = TRUE; /* error but continue parsing */
3558 if (name_end == NULL || (!vim_iswhite(*name_end)
3559 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003561 if (name_end != NULL)
3562 {
3563 emsg_severe = TRUE;
3564 EMSG(_(e_trailing));
3565 }
3566 if (!(eap->skip || error))
3567 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 break;
3569 }
3570
3571 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003572 {
3573 if (eap->cmdidx == CMD_unlet)
3574 {
3575 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3576 error = TRUE;
3577 }
3578 else
3579 {
3580 if (do_lock_var(&lv, name_end, deep,
3581 eap->cmdidx == CMD_lockvar) == FAIL)
3582 error = TRUE;
3583 }
3584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003586 if (!eap->skip)
3587 clear_lval(&lv);
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 arg = skipwhite(name_end);
3590 } while (!ends_excmd(*arg));
3591
3592 eap->nextcmd = check_nextcmd(arg);
3593}
3594
Bram Moolenaar8c711452005-01-14 21:53:12 +00003595 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003597 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 int forceit;
3600{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601 int ret = OK;
3602 int cc;
3603
3604 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 cc = *name_end;
3607 *name_end = NUL;
3608
3609 /* Normal name or expanded name. */
3610 if (check_changedtick(lp->ll_name))
3611 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003612 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3617 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 else if (lp->ll_range)
3619 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003620 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621
3622 /* Delete a range of List items. */
3623 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3624 {
3625 li = lp->ll_li->li_next;
3626 listitem_remove(lp->ll_list, lp->ll_li);
3627 lp->ll_li = li;
3628 ++lp->ll_n1;
3629 }
3630 }
3631 else
3632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 /* unlet a List item. */
3635 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003638 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 }
3640
3641 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003642}
3643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644/*
3645 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 */
3648 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003649do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003651 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
Bram Moolenaar33570922005-01-25 22:26:29 +00003653 hashtab_T *ht;
3654 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003655 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003656 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 ht = find_var_ht(name, &varname);
3659 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003661 hi = hash_find(ht, varname);
3662 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003663 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003664 di = HI2DI(hi);
3665 if (var_check_fixed(di->di_flags, name)
3666 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667 return FAIL;
3668 delete_var(ht, hi);
3669 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 if (forceit)
3673 return OK;
3674 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 return FAIL;
3676}
3677
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678/*
3679 * Lock or unlock variable indicated by "lp".
3680 * "deep" is the levels to go (-1 for unlimited);
3681 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3682 */
3683 static int
3684do_lock_var(lp, name_end, deep, lock)
3685 lval_T *lp;
3686 char_u *name_end;
3687 int deep;
3688 int lock;
3689{
3690 int ret = OK;
3691 int cc;
3692 dictitem_T *di;
3693
3694 if (deep == 0) /* nothing to do */
3695 return OK;
3696
3697 if (lp->ll_tv == NULL)
3698 {
3699 cc = *name_end;
3700 *name_end = NUL;
3701
3702 /* Normal name or expanded name. */
3703 if (check_changedtick(lp->ll_name))
3704 ret = FAIL;
3705 else
3706 {
3707 di = find_var(lp->ll_name, NULL);
3708 if (di == NULL)
3709 ret = FAIL;
3710 else
3711 {
3712 if (lock)
3713 di->di_flags |= DI_FLAGS_LOCK;
3714 else
3715 di->di_flags &= ~DI_FLAGS_LOCK;
3716 item_lock(&di->di_tv, deep, lock);
3717 }
3718 }
3719 *name_end = cc;
3720 }
3721 else if (lp->ll_range)
3722 {
3723 listitem_T *li = lp->ll_li;
3724
3725 /* (un)lock a range of List items. */
3726 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3727 {
3728 item_lock(&li->li_tv, deep, lock);
3729 li = li->li_next;
3730 ++lp->ll_n1;
3731 }
3732 }
3733 else if (lp->ll_list != NULL)
3734 /* (un)lock a List item. */
3735 item_lock(&lp->ll_li->li_tv, deep, lock);
3736 else
3737 /* un(lock) a Dictionary item. */
3738 item_lock(&lp->ll_di->di_tv, deep, lock);
3739
3740 return ret;
3741}
3742
3743/*
3744 * Lock or unlock an item. "deep" is nr of levels to go.
3745 */
3746 static void
3747item_lock(tv, deep, lock)
3748 typval_T *tv;
3749 int deep;
3750 int lock;
3751{
3752 static int recurse = 0;
3753 list_T *l;
3754 listitem_T *li;
3755 dict_T *d;
3756 hashitem_T *hi;
3757 int todo;
3758
3759 if (recurse >= DICT_MAXNEST)
3760 {
3761 EMSG(_("E743: variable nested too deep for (un)lock"));
3762 return;
3763 }
3764 if (deep == 0)
3765 return;
3766 ++recurse;
3767
3768 /* lock/unlock the item itself */
3769 if (lock)
3770 tv->v_lock |= VAR_LOCKED;
3771 else
3772 tv->v_lock &= ~VAR_LOCKED;
3773
3774 switch (tv->v_type)
3775 {
3776 case VAR_LIST:
3777 if ((l = tv->vval.v_list) != NULL)
3778 {
3779 if (lock)
3780 l->lv_lock |= VAR_LOCKED;
3781 else
3782 l->lv_lock &= ~VAR_LOCKED;
3783 if (deep < 0 || deep > 1)
3784 /* recursive: lock/unlock the items the List contains */
3785 for (li = l->lv_first; li != NULL; li = li->li_next)
3786 item_lock(&li->li_tv, deep - 1, lock);
3787 }
3788 break;
3789 case VAR_DICT:
3790 if ((d = tv->vval.v_dict) != NULL)
3791 {
3792 if (lock)
3793 d->dv_lock |= VAR_LOCKED;
3794 else
3795 d->dv_lock &= ~VAR_LOCKED;
3796 if (deep < 0 || deep > 1)
3797 {
3798 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003799 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003800 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3801 {
3802 if (!HASHITEM_EMPTY(hi))
3803 {
3804 --todo;
3805 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3806 }
3807 }
3808 }
3809 }
3810 }
3811 --recurse;
3812}
3813
Bram Moolenaara40058a2005-07-11 22:42:07 +00003814/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003815 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3816 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003817 */
3818 static int
3819tv_islocked(tv)
3820 typval_T *tv;
3821{
3822 return (tv->v_lock & VAR_LOCKED)
3823 || (tv->v_type == VAR_LIST
3824 && tv->vval.v_list != NULL
3825 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3826 || (tv->v_type == VAR_DICT
3827 && tv->vval.v_dict != NULL
3828 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3829}
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3832/*
3833 * Delete all "menutrans_" variables.
3834 */
3835 void
3836del_menutrans_vars()
3837{
Bram Moolenaar33570922005-01-25 22:26:29 +00003838 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003839 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
Bram Moolenaar33570922005-01-25 22:26:29 +00003841 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003842 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003843 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003844 {
3845 if (!HASHITEM_EMPTY(hi))
3846 {
3847 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3849 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 }
3851 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003852 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853}
3854#endif
3855
3856#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3857
3858/*
3859 * Local string buffer for the next two functions to store a variable name
3860 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3861 * get_user_var_name().
3862 */
3863
3864static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3865
3866static char_u *varnamebuf = NULL;
3867static int varnamebuflen = 0;
3868
3869/*
3870 * Function to concatenate a prefix and a variable name.
3871 */
3872 static char_u *
3873cat_prefix_varname(prefix, name)
3874 int prefix;
3875 char_u *name;
3876{
3877 int len;
3878
3879 len = (int)STRLEN(name) + 3;
3880 if (len > varnamebuflen)
3881 {
3882 vim_free(varnamebuf);
3883 len += 10; /* some additional space */
3884 varnamebuf = alloc(len);
3885 if (varnamebuf == NULL)
3886 {
3887 varnamebuflen = 0;
3888 return NULL;
3889 }
3890 varnamebuflen = len;
3891 }
3892 *varnamebuf = prefix;
3893 varnamebuf[1] = ':';
3894 STRCPY(varnamebuf + 2, name);
3895 return varnamebuf;
3896}
3897
3898/*
3899 * Function given to ExpandGeneric() to obtain the list of user defined
3900 * (global/buffer/window/built-in) variable names.
3901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 char_u *
3903get_user_var_name(xp, idx)
3904 expand_T *xp;
3905 int idx;
3906{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003907 static long_u gdone;
3908 static long_u bdone;
3909 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003910#ifdef FEAT_WINDOWS
3911 static long_u tdone;
3912#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003913 static int vidx;
3914 static hashitem_T *hi;
3915 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
3917 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003918 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003919 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003920#ifdef FEAT_WINDOWS
3921 tdone = 0;
3922#endif
3923 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003924
3925 /* Global variables */
3926 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003929 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003930 else
3931 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 while (HASHITEM_EMPTY(hi))
3933 ++hi;
3934 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3935 return cat_prefix_varname('g', hi->hi_key);
3936 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003938
3939 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003940 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003945 else
3946 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 while (HASHITEM_EMPTY(hi))
3948 ++hi;
3949 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 return (char_u *)"b:changedtick";
3955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003956
3957 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003958 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003961 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003963 else
3964 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 while (HASHITEM_EMPTY(hi))
3966 ++hi;
3967 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003969
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003970#ifdef FEAT_WINDOWS
3971 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003972 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003973 if (tdone < ht->ht_used)
3974 {
3975 if (tdone++ == 0)
3976 hi = ht->ht_array;
3977 else
3978 ++hi;
3979 while (HASHITEM_EMPTY(hi))
3980 ++hi;
3981 return cat_prefix_varname('t', hi->hi_key);
3982 }
3983#endif
3984
Bram Moolenaar33570922005-01-25 22:26:29 +00003985 /* v: variables */
3986 if (vidx < VV_LEN)
3987 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988
3989 vim_free(varnamebuf);
3990 varnamebuf = NULL;
3991 varnamebuflen = 0;
3992 return NULL;
3993}
3994
3995#endif /* FEAT_CMDL_COMPL */
3996
3997/*
3998 * types for expressions.
3999 */
4000typedef enum
4001{
4002 TYPE_UNKNOWN = 0
4003 , TYPE_EQUAL /* == */
4004 , TYPE_NEQUAL /* != */
4005 , TYPE_GREATER /* > */
4006 , TYPE_GEQUAL /* >= */
4007 , TYPE_SMALLER /* < */
4008 , TYPE_SEQUAL /* <= */
4009 , TYPE_MATCH /* =~ */
4010 , TYPE_NOMATCH /* !~ */
4011} exptype_T;
4012
4013/*
4014 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4017 */
4018
4019/*
4020 * Handle zero level expression.
4021 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004023 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 * Return OK or FAIL.
4025 */
4026 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 char_u **nextcmd;
4031 int evaluate;
4032{
4033 int ret;
4034 char_u *p;
4035
4036 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004037 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 if (ret == FAIL || !ends_excmd(*p))
4039 {
4040 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004041 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 /*
4043 * Report the invalid expression unless the expression evaluation has
4044 * been cancelled due to an aborting error, an interrupt, or an
4045 * exception.
4046 */
4047 if (!aborting())
4048 EMSG2(_(e_invexpr2), arg);
4049 ret = FAIL;
4050 }
4051 if (nextcmd != NULL)
4052 *nextcmd = check_nextcmd(p);
4053
4054 return ret;
4055}
4056
4057/*
4058 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004059 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 *
4061 * "arg" must point to the first non-white of the expression.
4062 * "arg" is advanced to the next non-white after the recognized expression.
4063 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004064 * Note: "rettv.v_lock" is not set.
4065 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 * Return OK or FAIL.
4067 */
4068 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 int evaluate;
4073{
4074 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004075 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077 /*
4078 * Get the first variable.
4079 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 return FAIL;
4082
4083 if ((*arg)[0] == '?')
4084 {
4085 result = FALSE;
4086 if (evaluate)
4087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 int error = FALSE;
4089
4090 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004093 if (error)
4094 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096
4097 /*
4098 * Get the second variable.
4099 */
4100 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 return FAIL;
4103
4104 /*
4105 * Check for the ":".
4106 */
4107 if ((*arg)[0] != ':')
4108 {
4109 EMSG(_("E109: Missing ':' after '?'"));
4110 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 return FAIL;
4113 }
4114
4115 /*
4116 * Get the third variable.
4117 */
4118 *arg = skipwhite(*arg + 1);
4119 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4120 {
4121 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124 }
4125 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004126 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
4128
4129 return OK;
4130}
4131
4132/*
4133 * Handle first level expression:
4134 * expr2 || expr2 || expr2 logical OR
4135 *
4136 * "arg" must point to the first non-white of the expression.
4137 * "arg" is advanced to the next non-white after the recognized expression.
4138 *
4139 * Return OK or FAIL.
4140 */
4141 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 int evaluate;
4146{
Bram Moolenaar33570922005-01-25 22:26:29 +00004147 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 long result;
4149 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004150 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
4152 /*
4153 * Get the first variable.
4154 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 return FAIL;
4157
4158 /*
4159 * Repeat until there is no following "||".
4160 */
4161 first = TRUE;
4162 result = FALSE;
4163 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4164 {
4165 if (evaluate && first)
4166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004170 if (error)
4171 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 first = FALSE;
4173 }
4174
4175 /*
4176 * Get the second variable.
4177 */
4178 *arg = skipwhite(*arg + 2);
4179 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4180 return FAIL;
4181
4182 /*
4183 * Compute the result.
4184 */
4185 if (evaluate && !result)
4186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (error)
4191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 if (evaluate)
4194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 rettv->v_type = VAR_NUMBER;
4196 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198 }
4199
4200 return OK;
4201}
4202
4203/*
4204 * Handle second level expression:
4205 * expr3 && expr3 && expr3 logical AND
4206 *
4207 * "arg" must point to the first non-white of the expression.
4208 * "arg" is advanced to the next non-white after the recognized expression.
4209 *
4210 * Return OK or FAIL.
4211 */
4212 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 int evaluate;
4217{
Bram Moolenaar33570922005-01-25 22:26:29 +00004218 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 long result;
4220 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004221 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223 /*
4224 * Get the first variable.
4225 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 return FAIL;
4228
4229 /*
4230 * Repeat until there is no following "&&".
4231 */
4232 first = TRUE;
4233 result = TRUE;
4234 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4235 {
4236 if (evaluate && first)
4237 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 if (error)
4242 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 first = FALSE;
4244 }
4245
4246 /*
4247 * Get the second variable.
4248 */
4249 *arg = skipwhite(*arg + 2);
4250 if (eval4(arg, &var2, evaluate && result) == FAIL)
4251 return FAIL;
4252
4253 /*
4254 * Compute the result.
4255 */
4256 if (evaluate && result)
4257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (error)
4262 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264 if (evaluate)
4265 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 rettv->v_type = VAR_NUMBER;
4267 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 }
4270
4271 return OK;
4272}
4273
4274/*
4275 * Handle third level expression:
4276 * var1 == var2
4277 * var1 =~ var2
4278 * var1 != var2
4279 * var1 !~ var2
4280 * var1 > var2
4281 * var1 >= var2
4282 * var1 < var2
4283 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004284 * var1 is var2
4285 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 *
4287 * "arg" must point to the first non-white of the expression.
4288 * "arg" is advanced to the next non-white after the recognized expression.
4289 *
4290 * Return OK or FAIL.
4291 */
4292 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 int evaluate;
4297{
Bram Moolenaar33570922005-01-25 22:26:29 +00004298 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 char_u *p;
4300 int i;
4301 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004302 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 int len = 2;
4304 long n1, n2;
4305 char_u *s1, *s2;
4306 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4307 regmatch_T regmatch;
4308 int ic;
4309 char_u *save_cpo;
4310
4311 /*
4312 * Get the first variable.
4313 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 return FAIL;
4316
4317 p = *arg;
4318 switch (p[0])
4319 {
4320 case '=': if (p[1] == '=')
4321 type = TYPE_EQUAL;
4322 else if (p[1] == '~')
4323 type = TYPE_MATCH;
4324 break;
4325 case '!': if (p[1] == '=')
4326 type = TYPE_NEQUAL;
4327 else if (p[1] == '~')
4328 type = TYPE_NOMATCH;
4329 break;
4330 case '>': if (p[1] != '=')
4331 {
4332 type = TYPE_GREATER;
4333 len = 1;
4334 }
4335 else
4336 type = TYPE_GEQUAL;
4337 break;
4338 case '<': if (p[1] != '=')
4339 {
4340 type = TYPE_SMALLER;
4341 len = 1;
4342 }
4343 else
4344 type = TYPE_SEQUAL;
4345 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004346 case 'i': if (p[1] == 's')
4347 {
4348 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4349 len = 5;
4350 if (!vim_isIDc(p[len]))
4351 {
4352 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4353 type_is = TRUE;
4354 }
4355 }
4356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 }
4358
4359 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004360 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 */
4362 if (type != TYPE_UNKNOWN)
4363 {
4364 /* extra question mark appended: ignore case */
4365 if (p[len] == '?')
4366 {
4367 ic = TRUE;
4368 ++len;
4369 }
4370 /* extra '#' appended: match case */
4371 else if (p[len] == '#')
4372 {
4373 ic = FALSE;
4374 ++len;
4375 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004376 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 else
4378 ic = p_ic;
4379
4380 /*
4381 * Get the second variable.
4382 */
4383 *arg = skipwhite(p + len);
4384 if (eval5(arg, &var2, evaluate) == FAIL)
4385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004386 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 return FAIL;
4388 }
4389
4390 if (evaluate)
4391 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004392 if (type_is && rettv->v_type != var2.v_type)
4393 {
4394 /* For "is" a different type always means FALSE, for "notis"
4395 * it means TRUE. */
4396 n1 = (type == TYPE_NEQUAL);
4397 }
4398 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4399 {
4400 if (type_is)
4401 {
4402 n1 = (rettv->v_type == var2.v_type
4403 && rettv->vval.v_list == var2.vval.v_list);
4404 if (type == TYPE_NEQUAL)
4405 n1 = !n1;
4406 }
4407 else if (rettv->v_type != var2.v_type
4408 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4409 {
4410 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004411 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004412 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004413 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004414 clear_tv(rettv);
4415 clear_tv(&var2);
4416 return FAIL;
4417 }
4418 else
4419 {
4420 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004421 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4422 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004423 if (type == TYPE_NEQUAL)
4424 n1 = !n1;
4425 }
4426 }
4427
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004428 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4429 {
4430 if (type_is)
4431 {
4432 n1 = (rettv->v_type == var2.v_type
4433 && rettv->vval.v_dict == var2.vval.v_dict);
4434 if (type == TYPE_NEQUAL)
4435 n1 = !n1;
4436 }
4437 else if (rettv->v_type != var2.v_type
4438 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4439 {
4440 if (rettv->v_type != var2.v_type)
4441 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4442 else
4443 EMSG(_("E736: Invalid operation for Dictionary"));
4444 clear_tv(rettv);
4445 clear_tv(&var2);
4446 return FAIL;
4447 }
4448 else
4449 {
4450 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004451 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4452 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004453 if (type == TYPE_NEQUAL)
4454 n1 = !n1;
4455 }
4456 }
4457
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004458 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4459 {
4460 if (rettv->v_type != var2.v_type
4461 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4462 {
4463 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004464 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004466 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004467 clear_tv(rettv);
4468 clear_tv(&var2);
4469 return FAIL;
4470 }
4471 else
4472 {
4473 /* Compare two Funcrefs for being equal or unequal. */
4474 if (rettv->vval.v_string == NULL
4475 || var2.vval.v_string == NULL)
4476 n1 = FALSE;
4477 else
4478 n1 = STRCMP(rettv->vval.v_string,
4479 var2.vval.v_string) == 0;
4480 if (type == TYPE_NEQUAL)
4481 n1 = !n1;
4482 }
4483 }
4484
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004485#ifdef FEAT_FLOAT
4486 /*
4487 * If one of the two variables is a float, compare as a float.
4488 * When using "=~" or "!~", always compare as string.
4489 */
4490 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4491 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4492 {
4493 float_T f1, f2;
4494
4495 if (rettv->v_type == VAR_FLOAT)
4496 f1 = rettv->vval.v_float;
4497 else
4498 f1 = get_tv_number(rettv);
4499 if (var2.v_type == VAR_FLOAT)
4500 f2 = var2.vval.v_float;
4501 else
4502 f2 = get_tv_number(&var2);
4503 n1 = FALSE;
4504 switch (type)
4505 {
4506 case TYPE_EQUAL: n1 = (f1 == f2); break;
4507 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4508 case TYPE_GREATER: n1 = (f1 > f2); break;
4509 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4510 case TYPE_SMALLER: n1 = (f1 < f2); break;
4511 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4512 case TYPE_UNKNOWN:
4513 case TYPE_MATCH:
4514 case TYPE_NOMATCH: break; /* avoid gcc warning */
4515 }
4516 }
4517#endif
4518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 /*
4520 * If one of the two variables is a number, compare as a number.
4521 * When using "=~" or "!~", always compare as string.
4522 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004523 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004526 n1 = get_tv_number(rettv);
4527 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 switch (type)
4529 {
4530 case TYPE_EQUAL: n1 = (n1 == n2); break;
4531 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4532 case TYPE_GREATER: n1 = (n1 > n2); break;
4533 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4534 case TYPE_SMALLER: n1 = (n1 < n2); break;
4535 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4536 case TYPE_UNKNOWN:
4537 case TYPE_MATCH:
4538 case TYPE_NOMATCH: break; /* avoid gcc warning */
4539 }
4540 }
4541 else
4542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004543 s1 = get_tv_string_buf(rettv, buf1);
4544 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4546 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4547 else
4548 i = 0;
4549 n1 = FALSE;
4550 switch (type)
4551 {
4552 case TYPE_EQUAL: n1 = (i == 0); break;
4553 case TYPE_NEQUAL: n1 = (i != 0); break;
4554 case TYPE_GREATER: n1 = (i > 0); break;
4555 case TYPE_GEQUAL: n1 = (i >= 0); break;
4556 case TYPE_SMALLER: n1 = (i < 0); break;
4557 case TYPE_SEQUAL: n1 = (i <= 0); break;
4558
4559 case TYPE_MATCH:
4560 case TYPE_NOMATCH:
4561 /* avoid 'l' flag in 'cpoptions' */
4562 save_cpo = p_cpo;
4563 p_cpo = (char_u *)"";
4564 regmatch.regprog = vim_regcomp(s2,
4565 RE_MAGIC + RE_STRING);
4566 regmatch.rm_ic = ic;
4567 if (regmatch.regprog != NULL)
4568 {
4569 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004570 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 if (type == TYPE_NOMATCH)
4572 n1 = !n1;
4573 }
4574 p_cpo = save_cpo;
4575 break;
4576
4577 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4578 }
4579 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004580 clear_tv(rettv);
4581 clear_tv(&var2);
4582 rettv->v_type = VAR_NUMBER;
4583 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
4585 }
4586
4587 return OK;
4588}
4589
4590/*
4591 * Handle fourth level expression:
4592 * + number addition
4593 * - number subtraction
4594 * . string concatenation
4595 *
4596 * "arg" must point to the first non-white of the expression.
4597 * "arg" is advanced to the next non-white after the recognized expression.
4598 *
4599 * Return OK or FAIL.
4600 */
4601 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004602eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 int evaluate;
4606{
Bram Moolenaar33570922005-01-25 22:26:29 +00004607 typval_T var2;
4608 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 int op;
4610 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004611#ifdef FEAT_FLOAT
4612 float_T f1 = 0, f2 = 0;
4613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 char_u *s1, *s2;
4615 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4616 char_u *p;
4617
4618 /*
4619 * Get the first variable.
4620 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004621 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 return FAIL;
4623
4624 /*
4625 * Repeat computing, until no '+', '-' or '.' is following.
4626 */
4627 for (;;)
4628 {
4629 op = **arg;
4630 if (op != '+' && op != '-' && op != '.')
4631 break;
4632
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004633 if ((op != '+' || rettv->v_type != VAR_LIST)
4634#ifdef FEAT_FLOAT
4635 && (op == '.' || rettv->v_type != VAR_FLOAT)
4636#endif
4637 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004638 {
4639 /* For "list + ...", an illegal use of the first operand as
4640 * a number cannot be determined before evaluating the 2nd
4641 * operand: if this is also a list, all is ok.
4642 * For "something . ...", "something - ..." or "non-list + ...",
4643 * we know that the first operand needs to be a string or number
4644 * without evaluating the 2nd operand. So check before to avoid
4645 * side effects after an error. */
4646 if (evaluate && get_tv_string_chk(rettv) == NULL)
4647 {
4648 clear_tv(rettv);
4649 return FAIL;
4650 }
4651 }
4652
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 /*
4654 * Get the second variable.
4655 */
4656 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004657 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004659 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 return FAIL;
4661 }
4662
4663 if (evaluate)
4664 {
4665 /*
4666 * Compute the result.
4667 */
4668 if (op == '.')
4669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004670 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4671 s2 = get_tv_string_buf_chk(&var2, buf2);
4672 if (s2 == NULL) /* type error ? */
4673 {
4674 clear_tv(rettv);
4675 clear_tv(&var2);
4676 return FAIL;
4677 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004678 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004679 clear_tv(rettv);
4680 rettv->v_type = VAR_STRING;
4681 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004683 else if (op == '+' && rettv->v_type == VAR_LIST
4684 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004685 {
4686 /* concatenate Lists */
4687 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4688 &var3) == FAIL)
4689 {
4690 clear_tv(rettv);
4691 clear_tv(&var2);
4692 return FAIL;
4693 }
4694 clear_tv(rettv);
4695 *rettv = var3;
4696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 else
4698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004699 int error = FALSE;
4700
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004701#ifdef FEAT_FLOAT
4702 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004703 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004704 f1 = rettv->vval.v_float;
4705 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004706 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004707 else
4708#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004709 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004710 n1 = get_tv_number_chk(rettv, &error);
4711 if (error)
4712 {
4713 /* This can only happen for "list + non-list". For
4714 * "non-list + ..." or "something - ...", we returned
4715 * before evaluating the 2nd operand. */
4716 clear_tv(rettv);
4717 return FAIL;
4718 }
4719#ifdef FEAT_FLOAT
4720 if (var2.v_type == VAR_FLOAT)
4721 f1 = n1;
4722#endif
4723 }
4724#ifdef FEAT_FLOAT
4725 if (var2.v_type == VAR_FLOAT)
4726 {
4727 f2 = var2.vval.v_float;
4728 n2 = 0;
4729 }
4730 else
4731#endif
4732 {
4733 n2 = get_tv_number_chk(&var2, &error);
4734 if (error)
4735 {
4736 clear_tv(rettv);
4737 clear_tv(&var2);
4738 return FAIL;
4739 }
4740#ifdef FEAT_FLOAT
4741 if (rettv->v_type == VAR_FLOAT)
4742 f2 = n2;
4743#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004744 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004745 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746
4747#ifdef FEAT_FLOAT
4748 /* If there is a float on either side the result is a float. */
4749 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4750 {
4751 if (op == '+')
4752 f1 = f1 + f2;
4753 else
4754 f1 = f1 - f2;
4755 rettv->v_type = VAR_FLOAT;
4756 rettv->vval.v_float = f1;
4757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004759#endif
4760 {
4761 if (op == '+')
4762 n1 = n1 + n2;
4763 else
4764 n1 = n1 - n2;
4765 rettv->v_type = VAR_NUMBER;
4766 rettv->vval.v_number = n1;
4767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771 }
4772 return OK;
4773}
4774
4775/*
4776 * Handle fifth level expression:
4777 * * number multiplication
4778 * / number division
4779 * % number modulo
4780 *
4781 * "arg" must point to the first non-white of the expression.
4782 * "arg" is advanced to the next non-white after the recognized expression.
4783 *
4784 * Return OK or FAIL.
4785 */
4786 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004787eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004791 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
Bram Moolenaar33570922005-01-25 22:26:29 +00004793 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 int op;
4795 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796#ifdef FEAT_FLOAT
4797 int use_float = FALSE;
4798 float_T f1 = 0, f2;
4799#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
4802 /*
4803 * Get the first variable.
4804 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004805 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return FAIL;
4807
4808 /*
4809 * Repeat computing, until no '*', '/' or '%' is following.
4810 */
4811 for (;;)
4812 {
4813 op = **arg;
4814 if (op != '*' && op != '/' && op != '%')
4815 break;
4816
4817 if (evaluate)
4818 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004819#ifdef FEAT_FLOAT
4820 if (rettv->v_type == VAR_FLOAT)
4821 {
4822 f1 = rettv->vval.v_float;
4823 use_float = TRUE;
4824 n1 = 0;
4825 }
4826 else
4827#endif
4828 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830 if (error)
4831 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 }
4833 else
4834 n1 = 0;
4835
4836 /*
4837 * Get the second variable.
4838 */
4839 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004840 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 return FAIL;
4842
4843 if (evaluate)
4844 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845#ifdef FEAT_FLOAT
4846 if (var2.v_type == VAR_FLOAT)
4847 {
4848 if (!use_float)
4849 {
4850 f1 = n1;
4851 use_float = TRUE;
4852 }
4853 f2 = var2.vval.v_float;
4854 n2 = 0;
4855 }
4856 else
4857#endif
4858 {
4859 n2 = get_tv_number_chk(&var2, &error);
4860 clear_tv(&var2);
4861 if (error)
4862 return FAIL;
4863#ifdef FEAT_FLOAT
4864 if (use_float)
4865 f2 = n2;
4866#endif
4867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
4869 /*
4870 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004871 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873#ifdef FEAT_FLOAT
4874 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876 if (op == '*')
4877 f1 = f1 * f2;
4878 else if (op == '/')
4879 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004880# ifdef VMS
4881 /* VMS crashes on divide by zero, work around it */
4882 if (f2 == 0.0)
4883 {
4884 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004885 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004886 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004887 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004888 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004889 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004890 }
4891 else
4892 f1 = f1 / f2;
4893# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894 /* We rely on the floating point library to handle divide
4895 * by zero to result in "inf" and not a crash. */
4896 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004897# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004900 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004901 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004902 return FAIL;
4903 }
4904 rettv->v_type = VAR_FLOAT;
4905 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910 if (op == '*')
4911 n1 = n1 * n2;
4912 else if (op == '/')
4913 {
4914 if (n2 == 0) /* give an error message? */
4915 {
4916 if (n1 == 0)
4917 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4918 else if (n1 < 0)
4919 n1 = -0x7fffffffL;
4920 else
4921 n1 = 0x7fffffffL;
4922 }
4923 else
4924 n1 = n1 / n2;
4925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927 {
4928 if (n2 == 0) /* give an error message? */
4929 n1 = 0;
4930 else
4931 n1 = n1 % n2;
4932 }
4933 rettv->v_type = VAR_NUMBER;
4934 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937 }
4938
4939 return OK;
4940}
4941
4942/*
4943 * Handle sixth level expression:
4944 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004945 * "string" string constant
4946 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 * &option-name option value
4948 * @r register contents
4949 * identifier variable value
4950 * function() function call
4951 * $VAR environment variable
4952 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004953 * [expr, expr] List
4954 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 *
4956 * Also handle:
4957 * ! in front logical NOT
4958 * - in front unary minus
4959 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 * trailing [] subscript in String or List
4961 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 *
4963 * "arg" must point to the first non-white of the expression.
4964 * "arg" is advanced to the next non-white after the recognized expression.
4965 *
4966 * Return OK or FAIL.
4967 */
4968 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004969eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004973 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 long n;
4976 int len;
4977 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 char_u *start_leader, *end_leader;
4979 int ret = OK;
4980 char_u *alias;
4981
4982 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004983 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004984 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004986 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987
4988 /*
4989 * Skip '!' and '-' characters. They are handled later.
4990 */
4991 start_leader = *arg;
4992 while (**arg == '!' || **arg == '-' || **arg == '+')
4993 *arg = skipwhite(*arg + 1);
4994 end_leader = *arg;
4995
4996 switch (**arg)
4997 {
4998 /*
4999 * Number constant.
5000 */
5001 case '0':
5002 case '1':
5003 case '2':
5004 case '3':
5005 case '4':
5006 case '5':
5007 case '6':
5008 case '7':
5009 case '8':
5010 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 {
5012#ifdef FEAT_FLOAT
5013 char_u *p = skipdigits(*arg + 1);
5014 int get_float = FALSE;
5015
5016 /* We accept a float when the format matches
5017 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005018 * strict to avoid backwards compatibility problems.
5019 * Don't look for a float after the "." operator, so that
5020 * ":let vers = 1.2.3" doesn't fail. */
5021 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005023 get_float = TRUE;
5024 p = skipdigits(p + 2);
5025 if (*p == 'e' || *p == 'E')
5026 {
5027 ++p;
5028 if (*p == '-' || *p == '+')
5029 ++p;
5030 if (!vim_isdigit(*p))
5031 get_float = FALSE;
5032 else
5033 p = skipdigits(p + 1);
5034 }
5035 if (ASCII_ISALPHA(*p) || *p == '.')
5036 get_float = FALSE;
5037 }
5038 if (get_float)
5039 {
5040 float_T f;
5041
5042 *arg += string2float(*arg, &f);
5043 if (evaluate)
5044 {
5045 rettv->v_type = VAR_FLOAT;
5046 rettv->vval.v_float = f;
5047 }
5048 }
5049 else
5050#endif
5051 {
5052 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5053 *arg += len;
5054 if (evaluate)
5055 {
5056 rettv->v_type = VAR_NUMBER;
5057 rettv->vval.v_number = n;
5058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
5063 /*
5064 * String constant: "string".
5065 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005066 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 break;
5068
5069 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005072 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005073 break;
5074
5075 /*
5076 * List: [expr, expr]
5077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 break;
5080
5081 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 * Dictionary: {key: val, key: val}
5083 */
5084 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5085 break;
5086
5087 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005088 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005090 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 break;
5092
5093 /*
5094 * Environment variable: $VAR.
5095 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005096 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098
5099 /*
5100 * Register contents: @r.
5101 */
5102 case '@': ++*arg;
5103 if (evaluate)
5104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005105 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005106 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 if (**arg != NUL)
5109 ++*arg;
5110 break;
5111
5112 /*
5113 * nested expression: (expression).
5114 */
5115 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005116 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (**arg == ')')
5118 ++*arg;
5119 else if (ret == OK)
5120 {
5121 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005122 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 ret = FAIL;
5124 }
5125 break;
5126
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 break;
5129 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130
5131 if (ret == NOTDONE)
5132 {
5133 /*
5134 * Must be a variable or function name.
5135 * Can also be a curly-braces kind of name: {expr}.
5136 */
5137 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005138 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 if (alias != NULL)
5140 s = alias;
5141
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005142 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 ret = FAIL;
5144 else
5145 {
5146 if (**arg == '(') /* recursive! */
5147 {
5148 /* If "s" is the name of a variable of type VAR_FUNC
5149 * use its contents. */
5150 s = deref_func_name(s, &len);
5151
5152 /* Invoke the function. */
5153 ret = get_func_tv(s, len, rettv, arg,
5154 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005155 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005156
5157 /* If evaluate is FALSE rettv->v_type was not set in
5158 * get_func_tv, but it's needed in handle_subscript() to parse
5159 * what follows. So set it here. */
5160 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5161 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005162 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005163 rettv->v_type = VAR_FUNC;
5164 }
5165
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 /* Stop the expression evaluation when immediately
5167 * aborting on error, or when an interrupt occurred or
5168 * an exception was thrown but not caught. */
5169 if (aborting())
5170 {
5171 if (ret == OK)
5172 clear_tv(rettv);
5173 ret = FAIL;
5174 }
5175 }
5176 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005177 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005178 else
5179 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005181 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 }
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 *arg = skipwhite(*arg);
5185
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005186 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5187 * expr(expr). */
5188 if (ret == OK)
5189 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190
5191 /*
5192 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5193 */
5194 if (ret == OK && evaluate && end_leader > start_leader)
5195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005196 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005197 int val = 0;
5198#ifdef FEAT_FLOAT
5199 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005200
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005201 if (rettv->v_type == VAR_FLOAT)
5202 f = rettv->vval.v_float;
5203 else
5204#endif
5205 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 clear_tv(rettv);
5209 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211 else
5212 {
5213 while (end_leader > start_leader)
5214 {
5215 --end_leader;
5216 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005217 {
5218#ifdef FEAT_FLOAT
5219 if (rettv->v_type == VAR_FLOAT)
5220 f = !f;
5221 else
5222#endif
5223 val = !val;
5224 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005225 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005226 {
5227#ifdef FEAT_FLOAT
5228 if (rettv->v_type == VAR_FLOAT)
5229 f = -f;
5230 else
5231#endif
5232 val = -val;
5233 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005234 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005235#ifdef FEAT_FLOAT
5236 if (rettv->v_type == VAR_FLOAT)
5237 {
5238 clear_tv(rettv);
5239 rettv->vval.v_float = f;
5240 }
5241 else
5242#endif
5243 {
5244 clear_tv(rettv);
5245 rettv->v_type = VAR_NUMBER;
5246 rettv->vval.v_number = val;
5247 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250
5251 return ret;
5252}
5253
5254/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005255 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5256 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5258 */
5259 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005260eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005262 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005264 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265{
5266 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005267 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 long len = -1;
5270 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005274 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005276 if (verbose)
5277 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 return FAIL;
5279 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005280#ifdef FEAT_FLOAT
5281 else if (rettv->v_type == VAR_FLOAT)
5282 {
5283 if (verbose)
5284 EMSG(_(e_float_as_string));
5285 return FAIL;
5286 }
5287#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 /*
5292 * dict.name
5293 */
5294 key = *arg + 1;
5295 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5296 ;
5297 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005299 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 }
5301 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 /*
5304 * something[idx]
5305 *
5306 * Get the (first) variable from inside the [].
5307 */
5308 *arg = skipwhite(*arg + 1);
5309 if (**arg == ':')
5310 empty1 = TRUE;
5311 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5312 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5314 {
5315 /* not a number or string */
5316 clear_tv(&var1);
5317 return FAIL;
5318 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005319
5320 /*
5321 * Get the second variable from inside the [:].
5322 */
5323 if (**arg == ':')
5324 {
5325 range = TRUE;
5326 *arg = skipwhite(*arg + 1);
5327 if (**arg == ']')
5328 empty2 = TRUE;
5329 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 if (!empty1)
5332 clear_tv(&var1);
5333 return FAIL;
5334 }
5335 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5336 {
5337 /* not a number or string */
5338 if (!empty1)
5339 clear_tv(&var1);
5340 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 return FAIL;
5342 }
5343 }
5344
5345 /* Check for the ']'. */
5346 if (**arg != ']')
5347 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005348 if (verbose)
5349 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005350 clear_tv(&var1);
5351 if (range)
5352 clear_tv(&var2);
5353 return FAIL;
5354 }
5355 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 }
5357
5358 if (evaluate)
5359 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005360 n1 = 0;
5361 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005363 n1 = get_tv_number(&var1);
5364 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 }
5366 if (range)
5367 {
5368 if (empty2)
5369 n2 = -1;
5370 else
5371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 n2 = get_tv_number(&var2);
5373 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 }
5375 }
5376
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005377 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 {
5379 case VAR_NUMBER:
5380 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005381 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 len = (long)STRLEN(s);
5383 if (range)
5384 {
5385 /* The resulting variable is a substring. If the indexes
5386 * are out of range the result is empty. */
5387 if (n1 < 0)
5388 {
5389 n1 = len + n1;
5390 if (n1 < 0)
5391 n1 = 0;
5392 }
5393 if (n2 < 0)
5394 n2 = len + n2;
5395 else if (n2 >= len)
5396 n2 = len;
5397 if (n1 >= len || n2 < 0 || n1 > n2)
5398 s = NULL;
5399 else
5400 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5401 }
5402 else
5403 {
5404 /* The resulting variable is a string of a single
5405 * character. If the index is too big or negative the
5406 * result is empty. */
5407 if (n1 >= len || n1 < 0)
5408 s = NULL;
5409 else
5410 s = vim_strnsave(s + n1, 1);
5411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005412 clear_tv(rettv);
5413 rettv->v_type = VAR_STRING;
5414 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 break;
5416
5417 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419 if (n1 < 0)
5420 n1 = len + n1;
5421 if (!empty1 && (n1 < 0 || n1 >= len))
5422 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005423 /* For a range we allow invalid values and return an empty
5424 * list. A list index out of range is an error. */
5425 if (!range)
5426 {
5427 if (verbose)
5428 EMSGN(_(e_listidx), n1);
5429 return FAIL;
5430 }
5431 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 }
5433 if (range)
5434 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005435 list_T *l;
5436 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437
5438 if (n2 < 0)
5439 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005440 else if (n2 >= len)
5441 n2 = len - 1;
5442 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005443 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 l = list_alloc();
5445 if (l == NULL)
5446 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 n1 <= n2; ++n1)
5449 {
5450 if (list_append_tv(l, &item->li_tv) == FAIL)
5451 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005452 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 return FAIL;
5454 }
5455 item = item->li_next;
5456 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 clear_tv(rettv);
5458 rettv->v_type = VAR_LIST;
5459 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005460 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 }
5462 else
5463 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005464 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 clear_tv(rettv);
5466 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 }
5468 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469
5470 case VAR_DICT:
5471 if (range)
5472 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005473 if (verbose)
5474 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475 if (len == -1)
5476 clear_tv(&var1);
5477 return FAIL;
5478 }
5479 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005480 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481
5482 if (len == -1)
5483 {
5484 key = get_tv_string(&var1);
5485 if (*key == NUL)
5486 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005487 if (verbose)
5488 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 clear_tv(&var1);
5490 return FAIL;
5491 }
5492 }
5493
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005494 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005495
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005496 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005497 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 if (len == -1)
5499 clear_tv(&var1);
5500 if (item == NULL)
5501 return FAIL;
5502
5503 copy_tv(&item->di_tv, &var1);
5504 clear_tv(rettv);
5505 *rettv = var1;
5506 }
5507 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 }
5509 }
5510
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511 return OK;
5512}
5513
5514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 * Get an option value.
5516 * "arg" points to the '&' or '+' before the option name.
5517 * "arg" is advanced to character after the option name.
5518 * Return OK or FAIL.
5519 */
5520 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005523 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 int evaluate;
5525{
5526 char_u *option_end;
5527 long numval;
5528 char_u *stringval;
5529 int opt_type;
5530 int c;
5531 int working = (**arg == '+'); /* has("+option") */
5532 int ret = OK;
5533 int opt_flags;
5534
5535 /*
5536 * Isolate the option name and find its value.
5537 */
5538 option_end = find_option_end(arg, &opt_flags);
5539 if (option_end == NULL)
5540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 EMSG2(_("E112: Option name missing: %s"), *arg);
5543 return FAIL;
5544 }
5545
5546 if (!evaluate)
5547 {
5548 *arg = option_end;
5549 return OK;
5550 }
5551
5552 c = *option_end;
5553 *option_end = NUL;
5554 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
5557 if (opt_type == -3) /* invalid name */
5558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 EMSG2(_("E113: Unknown option: %s"), *arg);
5561 ret = FAIL;
5562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 {
5565 if (opt_type == -2) /* hidden string option */
5566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 rettv->v_type = VAR_STRING;
5568 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570 else if (opt_type == -1) /* hidden number option */
5571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 rettv->v_type = VAR_NUMBER;
5573 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
5575 else if (opt_type == 1) /* number option */
5576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 rettv->v_type = VAR_NUMBER;
5578 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
5580 else /* string option */
5581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005582 rettv->v_type = VAR_STRING;
5583 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
5585 }
5586 else if (working && (opt_type == -2 || opt_type == -1))
5587 ret = FAIL;
5588
5589 *option_end = c; /* put back for error messages */
5590 *arg = option_end;
5591
5592 return ret;
5593}
5594
5595/*
5596 * Allocate a variable for a string constant.
5597 * Return OK or FAIL.
5598 */
5599 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005600get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 int evaluate;
5604{
5605 char_u *p;
5606 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 int extra = 0;
5608
5609 /*
5610 * Find the end of the string, skipping backslashed characters.
5611 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005612 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 {
5614 if (*p == '\\' && p[1] != NUL)
5615 {
5616 ++p;
5617 /* A "\<x>" form occupies at least 4 characters, and produces up
5618 * to 6 characters: reserve space for 2 extra */
5619 if (*p == '<')
5620 extra += 2;
5621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 }
5623
5624 if (*p != '"')
5625 {
5626 EMSG2(_("E114: Missing quote: %s"), *arg);
5627 return FAIL;
5628 }
5629
5630 /* If only parsing, set *arg and return here */
5631 if (!evaluate)
5632 {
5633 *arg = p + 1;
5634 return OK;
5635 }
5636
5637 /*
5638 * Copy the string into allocated memory, handling backslashed
5639 * characters.
5640 */
5641 name = alloc((unsigned)(p - *arg + extra));
5642 if (name == NULL)
5643 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 rettv->v_type = VAR_STRING;
5645 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
5649 if (*p == '\\')
5650 {
5651 switch (*++p)
5652 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005653 case 'b': *name++ = BS; ++p; break;
5654 case 'e': *name++ = ESC; ++p; break;
5655 case 'f': *name++ = FF; ++p; break;
5656 case 'n': *name++ = NL; ++p; break;
5657 case 'r': *name++ = CAR; ++p; break;
5658 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
5660 case 'X': /* hex: "\x1", "\x12" */
5661 case 'x':
5662 case 'u': /* Unicode: "\u0023" */
5663 case 'U':
5664 if (vim_isxdigit(p[1]))
5665 {
5666 int n, nr;
5667 int c = toupper(*p);
5668
5669 if (c == 'X')
5670 n = 2;
5671 else
5672 n = 4;
5673 nr = 0;
5674 while (--n >= 0 && vim_isxdigit(p[1]))
5675 {
5676 ++p;
5677 nr = (nr << 4) + hex2nr(*p);
5678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680#ifdef FEAT_MBYTE
5681 /* For "\u" store the number according to
5682 * 'encoding'. */
5683 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 else
5686#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005687 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 break;
5690
5691 /* octal: "\1", "\12", "\123" */
5692 case '0':
5693 case '1':
5694 case '2':
5695 case '3':
5696 case '4':
5697 case '5':
5698 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 case '7': *name = *p++ - '0';
5700 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 *name = (*name << 3) + *p++ - '0';
5703 if (*p >= '0' && *p <= '7')
5704 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 break;
5708
5709 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 if (extra != 0)
5712 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 break;
5715 }
5716 /* FALLTHROUGH */
5717
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 break;
5720 }
5721 }
5722 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 *arg = p + 1;
5728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 return OK;
5730}
5731
5732/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 * Return OK or FAIL.
5735 */
5736 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005737get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 int evaluate;
5741{
5742 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005744 int reduce = 0;
5745
5746 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005748 */
5749 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5750 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 break;
5755 ++reduce;
5756 ++p;
5757 }
5758 }
5759
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005763 return FAIL;
5764 }
5765
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005767 if (!evaluate)
5768 {
5769 *arg = p + 1;
5770 return OK;
5771 }
5772
5773 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 */
5776 str = alloc((unsigned)((p - *arg) - reduce));
5777 if (str == NULL)
5778 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 rettv->v_type = VAR_STRING;
5780 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005787 break;
5788 ++p;
5789 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005791 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793 *arg = p + 1;
5794
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 return OK;
5796}
5797
5798/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 * Allocate a variable for a List and fill it from "*arg".
5800 * Return OK or FAIL.
5801 */
5802 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005803get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005805 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 int evaluate;
5807{
Bram Moolenaar33570922005-01-25 22:26:29 +00005808 list_T *l = NULL;
5809 typval_T tv;
5810 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811
5812 if (evaluate)
5813 {
5814 l = list_alloc();
5815 if (l == NULL)
5816 return FAIL;
5817 }
5818
5819 *arg = skipwhite(*arg + 1);
5820 while (**arg != ']' && **arg != NUL)
5821 {
5822 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5823 goto failret;
5824 if (evaluate)
5825 {
5826 item = listitem_alloc();
5827 if (item != NULL)
5828 {
5829 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005830 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831 list_append(l, item);
5832 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005833 else
5834 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 }
5836
5837 if (**arg == ']')
5838 break;
5839 if (**arg != ',')
5840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 goto failret;
5843 }
5844 *arg = skipwhite(*arg + 1);
5845 }
5846
5847 if (**arg != ']')
5848 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850failret:
5851 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005852 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 return FAIL;
5854 }
5855
5856 *arg = skipwhite(*arg + 1);
5857 if (evaluate)
5858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005859 rettv->v_type = VAR_LIST;
5860 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861 ++l->lv_refcount;
5862 }
5863
5864 return OK;
5865}
5866
5867/*
5868 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005869 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005871 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872list_alloc()
5873{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005874 list_T *l;
5875
5876 l = (list_T *)alloc_clear(sizeof(list_T));
5877 if (l != NULL)
5878 {
5879 /* Prepend the list to the list of lists for garbage collection. */
5880 if (first_list != NULL)
5881 first_list->lv_used_prev = l;
5882 l->lv_used_prev = NULL;
5883 l->lv_used_next = first_list;
5884 first_list = l;
5885 }
5886 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887}
5888
5889/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005890 * Allocate an empty list for a return value.
5891 * Returns OK or FAIL.
5892 */
5893 static int
5894rettv_list_alloc(rettv)
5895 typval_T *rettv;
5896{
5897 list_T *l = list_alloc();
5898
5899 if (l == NULL)
5900 return FAIL;
5901
5902 rettv->vval.v_list = l;
5903 rettv->v_type = VAR_LIST;
5904 ++l->lv_refcount;
5905 return OK;
5906}
5907
5908/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909 * Unreference a list: decrement the reference count and free it when it
5910 * becomes zero.
5911 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005912 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005916 if (l != NULL && --l->lv_refcount <= 0)
5917 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918}
5919
5920/*
5921 * Free a list, including all items it points to.
5922 * Ignores the reference count.
5923 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005924 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005925list_free(l, recurse)
5926 list_T *l;
5927 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928{
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005931 /* Remove the list from the list of lists for garbage collection. */
5932 if (l->lv_used_prev == NULL)
5933 first_list = l->lv_used_next;
5934 else
5935 l->lv_used_prev->lv_used_next = l->lv_used_next;
5936 if (l->lv_used_next != NULL)
5937 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5938
Bram Moolenaard9fba312005-06-26 22:34:35 +00005939 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005941 /* Remove the item before deleting it. */
5942 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005943 if (recurse || (item->li_tv.v_type != VAR_LIST
5944 && item->li_tv.v_type != VAR_DICT))
5945 clear_tv(&item->li_tv);
5946 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 }
5948 vim_free(l);
5949}
5950
5951/*
5952 * Allocate a list item.
5953 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005954 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955listitem_alloc()
5956{
Bram Moolenaar33570922005-01-25 22:26:29 +00005957 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958}
5959
5960/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005961 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005963 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005965 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005967 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 vim_free(item);
5969}
5970
5971/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005972 * Remove a list item from a List and free it. Also clears the value.
5973 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005974 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005975listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005976 list_T *l;
5977 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005978{
5979 list_remove(l, item, item);
5980 listitem_free(item);
5981}
5982
5983/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 * Get the number of items in a list.
5985 */
5986 static long
5987list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 if (l == NULL)
5991 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005992 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993}
5994
5995/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005996 * Return TRUE when two lists have exactly the same values.
5997 */
5998 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005999list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 list_T *l1;
6001 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006003 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006004{
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006006
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006007 if (l1 == NULL || l2 == NULL)
6008 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006009 if (l1 == l2)
6010 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006011 if (list_len(l1) != list_len(l2))
6012 return FALSE;
6013
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014 for (item1 = l1->lv_first, item2 = l2->lv_first;
6015 item1 != NULL && item2 != NULL;
6016 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006017 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018 return FALSE;
6019 return item1 == NULL && item2 == NULL;
6020}
6021
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006022#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6023 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006024/*
6025 * Return the dictitem that an entry in a hashtable points to.
6026 */
6027 dictitem_T *
6028dict_lookup(hi)
6029 hashitem_T *hi;
6030{
6031 return HI2DI(hi);
6032}
6033#endif
6034
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006035/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006036 * Return TRUE when two dictionaries have exactly the same key/values.
6037 */
6038 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006039dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006040 dict_T *d1;
6041 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006042 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006043 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006044{
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 hashitem_T *hi;
6046 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006047 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006048
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006049 if (d1 == NULL || d2 == NULL)
6050 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006051 if (d1 == d2)
6052 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006053 if (dict_len(d1) != dict_len(d2))
6054 return FALSE;
6055
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006056 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006058 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006059 if (!HASHITEM_EMPTY(hi))
6060 {
6061 item2 = dict_find(d2, hi->hi_key, -1);
6062 if (item2 == NULL)
6063 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006064 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006065 return FALSE;
6066 --todo;
6067 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006068 }
6069 return TRUE;
6070}
6071
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006072static int tv_equal_recurse_limit;
6073
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006074/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006075 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006076 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006077 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006078 */
6079 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006080tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 typval_T *tv1;
6082 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006083 int ic; /* ignore case */
6084 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006085{
6086 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006087 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006088 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006089 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006091 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006092 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006093
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006094 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 * recursiveness to a limit. We guess they are equal then.
6096 * A fixed limit has the problem of still taking an awful long time.
6097 * Reduce the limit every time running into it. That should work fine for
6098 * deeply linked structures that are not recursively linked and catch
6099 * recursiveness quickly. */
6100 if (!recursive)
6101 tv_equal_recurse_limit = 1000;
6102 if (recursive_cnt >= tv_equal_recurse_limit)
6103 {
6104 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006105 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006106 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006107
6108 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006110 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111 ++recursive_cnt;
6112 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6113 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006114 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006115
6116 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 ++recursive_cnt;
6118 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6119 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006120 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006121
6122 case VAR_FUNC:
6123 return (tv1->vval.v_string != NULL
6124 && tv2->vval.v_string != NULL
6125 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6126
6127 case VAR_NUMBER:
6128 return tv1->vval.v_number == tv2->vval.v_number;
6129
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006130#ifdef FEAT_FLOAT
6131 case VAR_FLOAT:
6132 return tv1->vval.v_float == tv2->vval.v_float;
6133#endif
6134
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006135 case VAR_STRING:
6136 s1 = get_tv_string_buf(tv1, buf1);
6137 s2 = get_tv_string_buf(tv2, buf2);
6138 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006139 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006140
6141 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006142 return TRUE;
6143}
6144
6145/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146 * Locate item with index "n" in list "l" and return it.
6147 * A negative index is counted from the end; -1 is the last item.
6148 * Returns NULL when "n" is out of range.
6149 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006150 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 long n;
6154{
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006156 long idx;
6157
6158 if (l == NULL)
6159 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006160
6161 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006162 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006163 n = l->lv_len + n;
6164
6165 /* Check for index out of range. */
6166 if (n < 0 || n >= l->lv_len)
6167 return NULL;
6168
6169 /* When there is a cached index may start search from there. */
6170 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006171 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006172 if (n < l->lv_idx / 2)
6173 {
6174 /* closest to the start of the list */
6175 item = l->lv_first;
6176 idx = 0;
6177 }
6178 else if (n > (l->lv_idx + l->lv_len) / 2)
6179 {
6180 /* closest to the end of the list */
6181 item = l->lv_last;
6182 idx = l->lv_len - 1;
6183 }
6184 else
6185 {
6186 /* closest to the cached index */
6187 item = l->lv_idx_item;
6188 idx = l->lv_idx;
6189 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006190 }
6191 else
6192 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006193 if (n < l->lv_len / 2)
6194 {
6195 /* closest to the start of the list */
6196 item = l->lv_first;
6197 idx = 0;
6198 }
6199 else
6200 {
6201 /* closest to the end of the list */
6202 item = l->lv_last;
6203 idx = l->lv_len - 1;
6204 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006205 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006206
6207 while (n > idx)
6208 {
6209 /* search forward */
6210 item = item->li_next;
6211 ++idx;
6212 }
6213 while (n < idx)
6214 {
6215 /* search backward */
6216 item = item->li_prev;
6217 --idx;
6218 }
6219
6220 /* cache the used index */
6221 l->lv_idx = idx;
6222 l->lv_idx_item = item;
6223
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224 return item;
6225}
6226
6227/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006228 * Get list item "l[idx]" as a number.
6229 */
6230 static long
6231list_find_nr(l, idx, errorp)
6232 list_T *l;
6233 long idx;
6234 int *errorp; /* set to TRUE when something wrong */
6235{
6236 listitem_T *li;
6237
6238 li = list_find(l, idx);
6239 if (li == NULL)
6240 {
6241 if (errorp != NULL)
6242 *errorp = TRUE;
6243 return -1L;
6244 }
6245 return get_tv_number_chk(&li->li_tv, errorp);
6246}
6247
6248/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006249 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6250 */
6251 char_u *
6252list_find_str(l, idx)
6253 list_T *l;
6254 long idx;
6255{
6256 listitem_T *li;
6257
6258 li = list_find(l, idx - 1);
6259 if (li == NULL)
6260 {
6261 EMSGN(_(e_listidx), idx);
6262 return NULL;
6263 }
6264 return get_tv_string(&li->li_tv);
6265}
6266
6267/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006268 * Locate "item" list "l" and return its index.
6269 * Returns -1 when "item" is not in the list.
6270 */
6271 static long
6272list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006273 list_T *l;
6274 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006275{
6276 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006277 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006278
6279 if (l == NULL)
6280 return -1;
6281 idx = 0;
6282 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6283 ++idx;
6284 if (li == NULL)
6285 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006286 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006287}
6288
6289/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290 * Append item "item" to the end of list "l".
6291 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006292 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006294 list_T *l;
6295 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296{
6297 if (l->lv_last == NULL)
6298 {
6299 /* empty list */
6300 l->lv_first = item;
6301 l->lv_last = item;
6302 item->li_prev = NULL;
6303 }
6304 else
6305 {
6306 l->lv_last->li_next = item;
6307 item->li_prev = l->lv_last;
6308 l->lv_last = item;
6309 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006310 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311 item->li_next = NULL;
6312}
6313
6314/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006315 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006316 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006318 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 list_T *l;
6321 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006323 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324
Bram Moolenaar05159a02005-02-26 23:04:13 +00006325 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006327 copy_tv(tv, &li->li_tv);
6328 list_append(l, li);
6329 return OK;
6330}
6331
6332/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006333 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006334 * Return FAIL when out of memory.
6335 */
6336 int
6337list_append_dict(list, dict)
6338 list_T *list;
6339 dict_T *dict;
6340{
6341 listitem_T *li = listitem_alloc();
6342
6343 if (li == NULL)
6344 return FAIL;
6345 li->li_tv.v_type = VAR_DICT;
6346 li->li_tv.v_lock = 0;
6347 li->li_tv.vval.v_dict = dict;
6348 list_append(list, li);
6349 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006350 return OK;
6351}
6352
6353/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006354 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006355 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006356 * Returns FAIL when out of memory.
6357 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006358 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006359list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006360 list_T *l;
6361 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006362 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006363{
6364 listitem_T *li = listitem_alloc();
6365
6366 if (li == NULL)
6367 return FAIL;
6368 list_append(l, li);
6369 li->li_tv.v_type = VAR_STRING;
6370 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006371 if (str == NULL)
6372 li->li_tv.vval.v_string = NULL;
6373 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006374 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006375 return FAIL;
6376 return OK;
6377}
6378
6379/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006380 * Append "n" to list "l".
6381 * Returns FAIL when out of memory.
6382 */
6383 static int
6384list_append_number(l, n)
6385 list_T *l;
6386 varnumber_T n;
6387{
6388 listitem_T *li;
6389
6390 li = listitem_alloc();
6391 if (li == NULL)
6392 return FAIL;
6393 li->li_tv.v_type = VAR_NUMBER;
6394 li->li_tv.v_lock = 0;
6395 li->li_tv.vval.v_number = n;
6396 list_append(l, li);
6397 return OK;
6398}
6399
6400/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402 * If "item" is NULL append at the end.
6403 * Return FAIL when out of memory.
6404 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006405 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006406list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 list_T *l;
6408 typval_T *tv;
6409 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006410{
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006412
6413 if (ni == NULL)
6414 return FAIL;
6415 copy_tv(tv, &ni->li_tv);
6416 if (item == NULL)
6417 /* Append new item at end of list. */
6418 list_append(l, ni);
6419 else
6420 {
6421 /* Insert new item before existing item. */
6422 ni->li_prev = item->li_prev;
6423 ni->li_next = item;
6424 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006425 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006427 ++l->lv_idx;
6428 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006430 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006432 l->lv_idx_item = NULL;
6433 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006435 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436 }
6437 return OK;
6438}
6439
6440/*
6441 * Extend "l1" with "l2".
6442 * If "bef" is NULL append at the end, otherwise insert before this item.
6443 * Returns FAIL when out of memory.
6444 */
6445 static int
6446list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006447 list_T *l1;
6448 list_T *l2;
6449 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006450{
Bram Moolenaar33570922005-01-25 22:26:29 +00006451 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006452 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006454 /* We also quit the loop when we have inserted the original item count of
6455 * the list, avoid a hang when we extend a list with itself. */
6456 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6458 return FAIL;
6459 return OK;
6460}
6461
6462/*
6463 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6464 * Return FAIL when out of memory.
6465 */
6466 static int
6467list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 list_T *l1;
6469 list_T *l2;
6470 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006471{
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006473
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006474 if (l1 == NULL || l2 == NULL)
6475 return FAIL;
6476
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006477 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006478 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479 if (l == NULL)
6480 return FAIL;
6481 tv->v_type = VAR_LIST;
6482 tv->vval.v_list = l;
6483
6484 /* append all items from the second list */
6485 return list_extend(l, l2, NULL);
6486}
6487
6488/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006489 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006491 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492 * Returns NULL when out of memory.
6493 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006495list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006496 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006498 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499{
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 list_T *copy;
6501 listitem_T *item;
6502 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503
6504 if (orig == NULL)
6505 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506
6507 copy = list_alloc();
6508 if (copy != NULL)
6509 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006510 if (copyID != 0)
6511 {
6512 /* Do this before adding the items, because one of the items may
6513 * refer back to this list. */
6514 orig->lv_copyID = copyID;
6515 orig->lv_copylist = copy;
6516 }
6517 for (item = orig->lv_first; item != NULL && !got_int;
6518 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519 {
6520 ni = listitem_alloc();
6521 if (ni == NULL)
6522 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006523 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006524 {
6525 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6526 {
6527 vim_free(ni);
6528 break;
6529 }
6530 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006532 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533 list_append(copy, ni);
6534 }
6535 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006536 if (item != NULL)
6537 {
6538 list_unref(copy);
6539 copy = NULL;
6540 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006541 }
6542
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006543 return copy;
6544}
6545
6546/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006548 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006549 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006550 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006551list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006552 list_T *l;
6553 listitem_T *item;
6554 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555{
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558 /* notify watchers */
6559 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006561 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562 list_fix_watch(l, ip);
6563 if (ip == item2)
6564 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566
6567 if (item2->li_next == NULL)
6568 l->lv_last = item->li_prev;
6569 else
6570 item2->li_next->li_prev = item->li_prev;
6571 if (item->li_prev == NULL)
6572 l->lv_first = item2->li_next;
6573 else
6574 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006575 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576}
6577
6578/*
6579 * Return an allocated string with the string representation of a list.
6580 * May return NULL.
6581 */
6582 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006583list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006585 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586{
6587 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588
6589 if (tv->vval.v_list == NULL)
6590 return NULL;
6591 ga_init2(&ga, (int)sizeof(char), 80);
6592 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006593 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 {
6595 vim_free(ga.ga_data);
6596 return NULL;
6597 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 ga_append(&ga, ']');
6599 ga_append(&ga, NUL);
6600 return (char_u *)ga.ga_data;
6601}
6602
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006603typedef struct join_S {
6604 char_u *s;
6605 char_u *tofree;
6606} join_T;
6607
6608 static int
6609list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6610 garray_T *gap; /* to store the result in */
6611 list_T *l;
6612 char_u *sep;
6613 int echo_style;
6614 int copyID;
6615 garray_T *join_gap; /* to keep each list item string */
6616{
6617 int i;
6618 join_T *p;
6619 int len;
6620 int sumlen = 0;
6621 int first = TRUE;
6622 char_u *tofree;
6623 char_u numbuf[NUMBUFLEN];
6624 listitem_T *item;
6625 char_u *s;
6626
6627 /* Stringify each item in the list. */
6628 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6629 {
6630 if (echo_style)
6631 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6632 else
6633 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6634 if (s == NULL)
6635 return FAIL;
6636
6637 len = (int)STRLEN(s);
6638 sumlen += len;
6639
6640 ga_grow(join_gap, 1);
6641 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6642 if (tofree != NULL || s != numbuf)
6643 {
6644 p->s = s;
6645 p->tofree = tofree;
6646 }
6647 else
6648 {
6649 p->s = vim_strnsave(s, len);
6650 p->tofree = p->s;
6651 }
6652
6653 line_breakcheck();
6654 }
6655
6656 /* Allocate result buffer with its total size, avoid re-allocation and
6657 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6658 if (join_gap->ga_len >= 2)
6659 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6660 if (ga_grow(gap, sumlen + 2) == FAIL)
6661 return FAIL;
6662
6663 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6664 {
6665 if (first)
6666 first = FALSE;
6667 else
6668 ga_concat(gap, sep);
6669 p = ((join_T *)join_gap->ga_data) + i;
6670
6671 if (p->s != NULL)
6672 ga_concat(gap, p->s);
6673 line_breakcheck();
6674 }
6675
6676 return OK;
6677}
6678
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006680 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006681 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006682 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006683 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006684 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006685list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006686 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006687 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006688 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006689 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006690 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006691{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006692 garray_T join_ga;
6693 int retval;
6694 join_T *p;
6695 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006696
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006697 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6698 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6699
6700 /* Dispose each item in join_ga. */
6701 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006702 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006703 p = (join_T *)join_ga.ga_data;
6704 for (i = 0; i < join_ga.ga_len; ++i)
6705 {
6706 vim_free(p->tofree);
6707 ++p;
6708 }
6709 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006710 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006711
6712 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006713}
6714
6715/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006716 * Garbage collection for lists and dictionaries.
6717 *
6718 * We use reference counts to be able to free most items right away when they
6719 * are no longer used. But for composite items it's possible that it becomes
6720 * unused while the reference count is > 0: When there is a recursive
6721 * reference. Example:
6722 * :let l = [1, 2, 3]
6723 * :let d = {9: l}
6724 * :let l[1] = d
6725 *
6726 * Since this is quite unusual we handle this with garbage collection: every
6727 * once in a while find out which lists and dicts are not referenced from any
6728 * variable.
6729 *
6730 * Here is a good reference text about garbage collection (refers to Python
6731 * but it applies to all reference-counting mechanisms):
6732 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006733 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006734
6735/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 * Do garbage collection for lists and dicts.
6737 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006738 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006739 int
6740garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006741{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006742 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743 buf_T *buf;
6744 win_T *wp;
6745 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006746 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006747 int did_free;
6748 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006749#ifdef FEAT_WINDOWS
6750 tabpage_T *tp;
6751#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006752
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006753 /* Only do this once. */
6754 want_garbage_collect = FALSE;
6755 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006756 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006757
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006758 /* We advance by two because we add one for items referenced through
6759 * previous_funccal. */
6760 current_copyID += COPYID_INC;
6761 copyID = current_copyID;
6762
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006763 /*
6764 * 1. Go through all accessible variables and mark all lists and dicts
6765 * with copyID.
6766 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006767
6768 /* Don't free variables in the previous_funccal list unless they are only
6769 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006770 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006771 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6772 {
6773 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6774 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6775 }
6776
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006777 /* script-local variables */
6778 for (i = 1; i <= ga_scripts.ga_len; ++i)
6779 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6780
6781 /* buffer-local variables */
6782 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006783 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784
6785 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006786 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006787 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006788#ifdef FEAT_AUTOCMD
6789 if (aucmd_win != NULL)
6790 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6791#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006792
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006793#ifdef FEAT_WINDOWS
6794 /* tabpage-local variables */
6795 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006796 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006797#endif
6798
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006799 /* global variables */
6800 set_ref_in_ht(&globvarht, copyID);
6801
6802 /* function-local variables */
6803 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6804 {
6805 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6806 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6807 }
6808
Bram Moolenaard812df62008-11-09 12:46:09 +00006809 /* v: vars */
6810 set_ref_in_ht(&vimvarht, copyID);
6811
Bram Moolenaar1dced572012-04-05 16:54:08 +02006812#ifdef FEAT_LUA
6813 set_ref_in_lua(copyID);
6814#endif
6815
Bram Moolenaardb913952012-06-29 12:54:53 +02006816#ifdef FEAT_PYTHON
6817 set_ref_in_python(copyID);
6818#endif
6819
6820#ifdef FEAT_PYTHON3
6821 set_ref_in_python3(copyID);
6822#endif
6823
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006824 /*
6825 * 2. Free lists and dictionaries that are not referenced.
6826 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006827 did_free = free_unref_items(copyID);
6828
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006829 /*
6830 * 3. Check if any funccal can be freed now.
6831 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006832 for (pfc = &previous_funccal; *pfc != NULL; )
6833 {
6834 if (can_free_funccal(*pfc, copyID))
6835 {
6836 fc = *pfc;
6837 *pfc = fc->caller;
6838 free_funccal(fc, TRUE);
6839 did_free = TRUE;
6840 did_free_funccal = TRUE;
6841 }
6842 else
6843 pfc = &(*pfc)->caller;
6844 }
6845 if (did_free_funccal)
6846 /* When a funccal was freed some more items might be garbage
6847 * collected, so run again. */
6848 (void)garbage_collect();
6849
6850 return did_free;
6851}
6852
6853/*
6854 * Free lists and dictionaries that are no longer referenced.
6855 */
6856 static int
6857free_unref_items(copyID)
6858 int copyID;
6859{
6860 dict_T *dd;
6861 list_T *ll;
6862 int did_free = FALSE;
6863
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866 */
6867 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006868 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006870 /* Free the Dictionary and ordinary items it contains, but don't
6871 * recurse into Lists and Dictionaries, they will be in the list
6872 * of dicts or list of lists. */
6873 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874 did_free = TRUE;
6875
6876 /* restart, next dict may also have been freed */
6877 dd = first_dict;
6878 }
6879 else
6880 dd = dd->dv_used_next;
6881
6882 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883 * Go through the list of lists and free items without the copyID.
6884 * But don't free a list that has a watcher (used in a for loop), these
6885 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 */
6887 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006888 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6889 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006891 /* Free the List and ordinary items it contains, but don't recurse
6892 * into Lists and Dictionaries, they will be in the list of dicts
6893 * or list of lists. */
6894 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 did_free = TRUE;
6896
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006897 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 ll = first_list;
6899 }
6900 else
6901 ll = ll->lv_used_next;
6902
6903 return did_free;
6904}
6905
6906/*
6907 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6908 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006909 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910set_ref_in_ht(ht, copyID)
6911 hashtab_T *ht;
6912 int copyID;
6913{
6914 int todo;
6915 hashitem_T *hi;
6916
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006917 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006918 for (hi = ht->ht_array; todo > 0; ++hi)
6919 if (!HASHITEM_EMPTY(hi))
6920 {
6921 --todo;
6922 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6923 }
6924}
6925
6926/*
6927 * Mark all lists and dicts referenced through list "l" with "copyID".
6928 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006929 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930set_ref_in_list(l, copyID)
6931 list_T *l;
6932 int copyID;
6933{
6934 listitem_T *li;
6935
6936 for (li = l->lv_first; li != NULL; li = li->li_next)
6937 set_ref_in_item(&li->li_tv, copyID);
6938}
6939
6940/*
6941 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6942 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006943 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944set_ref_in_item(tv, copyID)
6945 typval_T *tv;
6946 int copyID;
6947{
6948 dict_T *dd;
6949 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006950
6951 switch (tv->v_type)
6952 {
6953 case VAR_DICT:
6954 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006955 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006956 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957 /* Didn't see this dict yet. */
6958 dd->dv_copyID = copyID;
6959 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006960 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006961 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006962
6963 case VAR_LIST:
6964 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006965 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006966 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 /* Didn't see this list yet. */
6968 ll->lv_copyID = copyID;
6969 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006970 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006971 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006972 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006974}
6975
6976/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006977 * Allocate an empty header for a dictionary.
6978 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006979 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006980dict_alloc()
6981{
Bram Moolenaar33570922005-01-25 22:26:29 +00006982 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006983
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006985 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006986 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006987 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 if (first_dict != NULL)
6989 first_dict->dv_used_prev = d;
6990 d->dv_used_next = first_dict;
6991 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006992 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993
Bram Moolenaar33570922005-01-25 22:26:29 +00006994 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006995 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006996 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006997 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006998 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006999 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007000 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007001}
7002
7003/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007004 * Allocate an empty dict for a return value.
7005 * Returns OK or FAIL.
7006 */
7007 static int
7008rettv_dict_alloc(rettv)
7009 typval_T *rettv;
7010{
7011 dict_T *d = dict_alloc();
7012
7013 if (d == NULL)
7014 return FAIL;
7015
7016 rettv->vval.v_dict = d;
7017 rettv->v_type = VAR_DICT;
7018 ++d->dv_refcount;
7019 return OK;
7020}
7021
7022
7023/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007024 * Unreference a Dictionary: decrement the reference count and free it when it
7025 * becomes zero.
7026 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007027 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007028dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007029 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007030{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007031 if (d != NULL && --d->dv_refcount <= 0)
7032 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033}
7034
7035/*
7036 * Free a Dictionary, including all items it contains.
7037 * Ignores the reference count.
7038 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007039 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007040dict_free(d, recurse)
7041 dict_T *d;
7042 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007043{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007044 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007045 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007046 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007047
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 /* Remove the dict from the list of dicts for garbage collection. */
7049 if (d->dv_used_prev == NULL)
7050 first_dict = d->dv_used_next;
7051 else
7052 d->dv_used_prev->dv_used_next = d->dv_used_next;
7053 if (d->dv_used_next != NULL)
7054 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7055
7056 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007057 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007058 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007059 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007060 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007061 if (!HASHITEM_EMPTY(hi))
7062 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007063 /* Remove the item before deleting it, just in case there is
7064 * something recursive causing trouble. */
7065 di = HI2DI(hi);
7066 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007067 if (recurse || (di->di_tv.v_type != VAR_LIST
7068 && di->di_tv.v_type != VAR_DICT))
7069 clear_tv(&di->di_tv);
7070 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007071 --todo;
7072 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007073 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007074 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007075 vim_free(d);
7076}
7077
7078/*
7079 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007080 * The "key" is copied to the new item.
7081 * Note that the value of the item "di_tv" still needs to be initialized!
7082 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007083 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007084 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007085dictitem_alloc(key)
7086 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007087{
Bram Moolenaar33570922005-01-25 22:26:29 +00007088 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007089
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007090 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007091 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007092 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007093 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007094 di->di_flags = 0;
7095 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007096 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007097}
7098
7099/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007100 * Make a copy of a Dictionary item.
7101 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007102 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007105{
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007107
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007108 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7109 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007110 if (di != NULL)
7111 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007113 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007114 copy_tv(&org->di_tv, &di->di_tv);
7115 }
7116 return di;
7117}
7118
7119/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120 * Remove item "item" from Dictionary "dict" and free it.
7121 */
7122 static void
7123dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 dict_T *dict;
7125 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007126{
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007128
Bram Moolenaar33570922005-01-25 22:26:29 +00007129 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007130 if (HASHITEM_EMPTY(hi))
7131 EMSG2(_(e_intern2), "dictitem_remove()");
7132 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007134 dictitem_free(item);
7135}
7136
7137/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007138 * Free a dict item. Also clears the value.
7139 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007140 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007141dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007142 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007143{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007144 clear_tv(&item->di_tv);
7145 vim_free(item);
7146}
7147
7148/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007149 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7150 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007151 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152 * Returns NULL when out of memory.
7153 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007155dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007156 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007158 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007159{
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 dict_T *copy;
7161 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007162 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007164
7165 if (orig == NULL)
7166 return NULL;
7167
7168 copy = dict_alloc();
7169 if (copy != NULL)
7170 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007171 if (copyID != 0)
7172 {
7173 orig->dv_copyID = copyID;
7174 orig->dv_copydict = copy;
7175 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007176 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007177 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007178 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007179 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007181 --todo;
7182
7183 di = dictitem_alloc(hi->hi_key);
7184 if (di == NULL)
7185 break;
7186 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007187 {
7188 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7189 copyID) == FAIL)
7190 {
7191 vim_free(di);
7192 break;
7193 }
7194 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 else
7196 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7197 if (dict_add(copy, di) == FAIL)
7198 {
7199 dictitem_free(di);
7200 break;
7201 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007204
Bram Moolenaare9a41262005-01-15 22:18:47 +00007205 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007206 if (todo > 0)
7207 {
7208 dict_unref(copy);
7209 copy = NULL;
7210 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211 }
7212
7213 return copy;
7214}
7215
7216/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007218 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007220 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007222 dict_T *d;
7223 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224{
Bram Moolenaar33570922005-01-25 22:26:29 +00007225 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007226}
7227
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007229 * Add a number or string entry to dictionary "d".
7230 * When "str" is NULL use number "nr", otherwise use "str".
7231 * Returns FAIL when out of memory and when key already exists.
7232 */
7233 int
7234dict_add_nr_str(d, key, nr, str)
7235 dict_T *d;
7236 char *key;
7237 long nr;
7238 char_u *str;
7239{
7240 dictitem_T *item;
7241
7242 item = dictitem_alloc((char_u *)key);
7243 if (item == NULL)
7244 return FAIL;
7245 item->di_tv.v_lock = 0;
7246 if (str == NULL)
7247 {
7248 item->di_tv.v_type = VAR_NUMBER;
7249 item->di_tv.vval.v_number = nr;
7250 }
7251 else
7252 {
7253 item->di_tv.v_type = VAR_STRING;
7254 item->di_tv.vval.v_string = vim_strsave(str);
7255 }
7256 if (dict_add(d, item) == FAIL)
7257 {
7258 dictitem_free(item);
7259 return FAIL;
7260 }
7261 return OK;
7262}
7263
7264/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007265 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007266 * Returns FAIL when out of memory and when key already exists.
7267 */
7268 int
7269dict_add_list(d, key, list)
7270 dict_T *d;
7271 char *key;
7272 list_T *list;
7273{
7274 dictitem_T *item;
7275
7276 item = dictitem_alloc((char_u *)key);
7277 if (item == NULL)
7278 return FAIL;
7279 item->di_tv.v_lock = 0;
7280 item->di_tv.v_type = VAR_LIST;
7281 item->di_tv.vval.v_list = list;
7282 if (dict_add(d, item) == FAIL)
7283 {
7284 dictitem_free(item);
7285 return FAIL;
7286 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007287 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007288 return OK;
7289}
7290
7291/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007292 * Get the number of items in a Dictionary.
7293 */
7294 static long
7295dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298 if (d == NULL)
7299 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007300 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301}
7302
7303/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 * Find item "key[len]" in Dictionary "d".
7305 * If "len" is negative use strlen(key).
7306 * Returns NULL when not found.
7307 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007308 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 char_u *key;
7312 int len;
7313{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314#define AKEYLEN 200
7315 char_u buf[AKEYLEN];
7316 char_u *akey;
7317 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007318 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007319
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 if (len < 0)
7321 akey = key;
7322 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007323 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 tofree = akey = vim_strnsave(key, len);
7325 if (akey == NULL)
7326 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007327 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 else
7329 {
7330 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007331 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007332 akey = buf;
7333 }
7334
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336 vim_free(tofree);
7337 if (HASHITEM_EMPTY(hi))
7338 return NULL;
7339 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340}
7341
7342/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007343 * Get a string item from a dictionary.
7344 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007345 * Returns NULL if the entry doesn't exist or out of memory.
7346 */
7347 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007348get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007349 dict_T *d;
7350 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007351 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007352{
7353 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007354 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007355
7356 di = dict_find(d, key, -1);
7357 if (di == NULL)
7358 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007359 s = get_tv_string(&di->di_tv);
7360 if (save && s != NULL)
7361 s = vim_strsave(s);
7362 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007363}
7364
7365/*
7366 * Get a number item from a dictionary.
7367 * Returns 0 if the entry doesn't exist or out of memory.
7368 */
7369 long
7370get_dict_number(d, key)
7371 dict_T *d;
7372 char_u *key;
7373{
7374 dictitem_T *di;
7375
7376 di = dict_find(d, key, -1);
7377 if (di == NULL)
7378 return 0;
7379 return get_tv_number(&di->di_tv);
7380}
7381
7382/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007383 * Return an allocated string with the string representation of a Dictionary.
7384 * May return NULL.
7385 */
7386 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007387dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007389 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390{
7391 garray_T ga;
7392 int first = TRUE;
7393 char_u *tofree;
7394 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007397 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007399
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401 return NULL;
7402 ga_init2(&ga, (int)sizeof(char), 80);
7403 ga_append(&ga, '{');
7404
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007405 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007406 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007408 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007410 --todo;
7411
7412 if (first)
7413 first = FALSE;
7414 else
7415 ga_concat(&ga, (char_u *)", ");
7416
7417 tofree = string_quote(hi->hi_key, FALSE);
7418 if (tofree != NULL)
7419 {
7420 ga_concat(&ga, tofree);
7421 vim_free(tofree);
7422 }
7423 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007424 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425 if (s != NULL)
7426 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007428 if (s == NULL)
7429 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007432 if (todo > 0)
7433 {
7434 vim_free(ga.ga_data);
7435 return NULL;
7436 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437
7438 ga_append(&ga, '}');
7439 ga_append(&ga, NUL);
7440 return (char_u *)ga.ga_data;
7441}
7442
7443/*
7444 * Allocate a variable for a Dictionary and fill it from "*arg".
7445 * Return OK or FAIL. Returns NOTDONE for {expr}.
7446 */
7447 static int
7448get_dict_tv(arg, rettv, evaluate)
7449 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 int evaluate;
7452{
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 dict_T *d = NULL;
7454 typval_T tvkey;
7455 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007456 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007457 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007459 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460
7461 /*
7462 * First check if it's not a curly-braces thing: {expr}.
7463 * Must do this without evaluating, otherwise a function may be called
7464 * twice. Unfortunately this means we need to call eval1() twice for the
7465 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007466 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 if (*start != '}')
7469 {
7470 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7471 return FAIL;
7472 if (*start == '}')
7473 return NOTDONE;
7474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475
7476 if (evaluate)
7477 {
7478 d = dict_alloc();
7479 if (d == NULL)
7480 return FAIL;
7481 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007482 tvkey.v_type = VAR_UNKNOWN;
7483 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484
7485 *arg = skipwhite(*arg + 1);
7486 while (**arg != '}' && **arg != NUL)
7487 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007488 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 goto failret;
7490 if (**arg != ':')
7491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007492 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007493 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 goto failret;
7495 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007496 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007498 key = get_tv_string_buf_chk(&tvkey, buf);
7499 if (key == NULL || *key == NUL)
7500 {
7501 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7502 if (key != NULL)
7503 EMSG(_(e_emptykey));
7504 clear_tv(&tvkey);
7505 goto failret;
7506 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007507 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508
7509 *arg = skipwhite(*arg + 1);
7510 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7511 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007512 if (evaluate)
7513 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 goto failret;
7515 }
7516 if (evaluate)
7517 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519 if (item != NULL)
7520 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007521 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007522 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 clear_tv(&tv);
7524 goto failret;
7525 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007526 item = dictitem_alloc(key);
7527 clear_tv(&tvkey);
7528 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007531 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 if (dict_add(d, item) == FAIL)
7533 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 }
7535 }
7536
7537 if (**arg == '}')
7538 break;
7539 if (**arg != ',')
7540 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007541 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542 goto failret;
7543 }
7544 *arg = skipwhite(*arg + 1);
7545 }
7546
7547 if (**arg != '}')
7548 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007549 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550failret:
7551 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007552 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007553 return FAIL;
7554 }
7555
7556 *arg = skipwhite(*arg + 1);
7557 if (evaluate)
7558 {
7559 rettv->v_type = VAR_DICT;
7560 rettv->vval.v_dict = d;
7561 ++d->dv_refcount;
7562 }
7563
7564 return OK;
7565}
7566
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007568 * Return a string with the string representation of a variable.
7569 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007570 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007571 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007572 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007573 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007574 */
7575 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007576echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007578 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007579 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007580 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007581{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007582 static int recurse = 0;
7583 char_u *r = NULL;
7584
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007586 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007587 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007588 *tofree = NULL;
7589 return NULL;
7590 }
7591 ++recurse;
7592
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007593 switch (tv->v_type)
7594 {
7595 case VAR_FUNC:
7596 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007597 r = tv->vval.v_string;
7598 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007599
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007600 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007601 if (tv->vval.v_list == NULL)
7602 {
7603 *tofree = NULL;
7604 r = NULL;
7605 }
7606 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7607 {
7608 *tofree = NULL;
7609 r = (char_u *)"[...]";
7610 }
7611 else
7612 {
7613 tv->vval.v_list->lv_copyID = copyID;
7614 *tofree = list2string(tv, copyID);
7615 r = *tofree;
7616 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007618
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620 if (tv->vval.v_dict == NULL)
7621 {
7622 *tofree = NULL;
7623 r = NULL;
7624 }
7625 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7626 {
7627 *tofree = NULL;
7628 r = (char_u *)"{...}";
7629 }
7630 else
7631 {
7632 tv->vval.v_dict->dv_copyID = copyID;
7633 *tofree = dict2string(tv, copyID);
7634 r = *tofree;
7635 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007636 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007638 case VAR_STRING:
7639 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007640 *tofree = NULL;
7641 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007642 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007643
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007644#ifdef FEAT_FLOAT
7645 case VAR_FLOAT:
7646 *tofree = NULL;
7647 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7648 r = numbuf;
7649 break;
7650#endif
7651
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007653 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007654 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007655 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007656
7657 --recurse;
7658 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007659}
7660
7661/*
7662 * Return a string with the string representation of a variable.
7663 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7664 * "numbuf" is used for a number.
7665 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007666 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007667 */
7668 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007669tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007671 char_u **tofree;
7672 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007673 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674{
7675 switch (tv->v_type)
7676 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007677 case VAR_FUNC:
7678 *tofree = string_quote(tv->vval.v_string, TRUE);
7679 return *tofree;
7680 case VAR_STRING:
7681 *tofree = string_quote(tv->vval.v_string, FALSE);
7682 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007683#ifdef FEAT_FLOAT
7684 case VAR_FLOAT:
7685 *tofree = NULL;
7686 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7687 return numbuf;
7688#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007689 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007692 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007693 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007694 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007695 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007696 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007697}
7698
7699/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 * Return string "str" in ' quotes, doubling ' characters.
7701 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007703 */
7704 static char_u *
7705string_quote(str, function)
7706 char_u *str;
7707 int function;
7708{
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007710 char_u *p, *r, *s;
7711
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 len = (function ? 13 : 3);
7713 if (str != NULL)
7714 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007715 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 for (p = str; *p != NUL; mb_ptr_adv(p))
7717 if (*p == '\'')
7718 ++len;
7719 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007720 s = r = alloc(len);
7721 if (r != NULL)
7722 {
7723 if (function)
7724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007726 r += 10;
7727 }
7728 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007730 if (str != NULL)
7731 for (p = str; *p != NUL; )
7732 {
7733 if (*p == '\'')
7734 *r++ = '\'';
7735 MB_COPY_CHAR(p, r);
7736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007738 if (function)
7739 *r++ = ')';
7740 *r++ = NUL;
7741 }
7742 return s;
7743}
7744
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007745#ifdef FEAT_FLOAT
7746/*
7747 * Convert the string "text" to a floating point number.
7748 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7749 * this always uses a decimal point.
7750 * Returns the length of the text that was consumed.
7751 */
7752 static int
7753string2float(text, value)
7754 char_u *text;
7755 float_T *value; /* result stored here */
7756{
7757 char *s = (char *)text;
7758 float_T f;
7759
7760 f = strtod(s, &s);
7761 *value = f;
7762 return (int)((char_u *)s - text);
7763}
7764#endif
7765
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 * Get the value of an environment variable.
7768 * "arg" is pointing to the '$'. It is advanced to after the name.
7769 * If the environment variable was not set, silently assume it is empty.
7770 * Always return OK.
7771 */
7772 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007773get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 int evaluate;
7777{
7778 char_u *string = NULL;
7779 int len;
7780 int cc;
7781 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007782 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783
7784 ++*arg;
7785 name = *arg;
7786 len = get_env_len(arg);
7787 if (evaluate)
7788 {
7789 if (len != 0)
7790 {
7791 cc = name[len];
7792 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007793 /* first try vim_getenv(), fast for normal environment vars */
7794 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007796 {
7797 if (!mustfree)
7798 string = vim_strsave(string);
7799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 else
7801 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007802 if (mustfree)
7803 vim_free(string);
7804
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 /* next try expanding things like $VIM and ${HOME} */
7806 string = expand_env_save(name - 1);
7807 if (string != NULL && *string == '$')
7808 {
7809 vim_free(string);
7810 string = NULL;
7811 }
7812 }
7813 name[len] = cc;
7814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007815 rettv->v_type = VAR_STRING;
7816 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 }
7818
7819 return OK;
7820}
7821
7822/*
7823 * Array with names and number of arguments of all internal functions
7824 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7825 */
7826static struct fst
7827{
7828 char *f_name; /* function name */
7829 char f_min_argc; /* minimal number of arguments */
7830 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007831 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007832 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833} functions[] =
7834{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007835#ifdef FEAT_FLOAT
7836 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007837 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007839 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007840 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {"append", 2, 2, f_append},
7842 {"argc", 0, 0, f_argc},
7843 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007844 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007845#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007846 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007847 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007848 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007851 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {"bufexists", 1, 1, f_bufexists},
7853 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7854 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7855 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7856 {"buflisted", 1, 1, f_buflisted},
7857 {"bufloaded", 1, 1, f_bufloaded},
7858 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007859 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {"bufwinnr", 1, 1, f_bufwinnr},
7861 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007862 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007863 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007864#ifdef FEAT_FLOAT
7865 {"ceil", 1, 1, f_ceil},
7866#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007867 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007868 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007870 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007872#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007873 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007874 {"complete_add", 1, 1, f_complete_add},
7875 {"complete_check", 0, 0, f_complete_check},
7876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007878 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007879#ifdef FEAT_FLOAT
7880 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007881 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007882#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007883 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007885 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007886 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"delete", 1, 1, f_delete},
7888 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007889 {"diff_filler", 1, 1, f_diff_filler},
7890 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007891 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"eventhandler", 0, 0, f_eventhandler},
7895 {"executable", 1, 1, f_executable},
7896 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007897#ifdef FEAT_FLOAT
7898 {"exp", 1, 1, f_exp},
7899#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007900 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007901 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007902 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7904 {"filereadable", 1, 1, f_filereadable},
7905 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007907 {"finddir", 1, 3, f_finddir},
7908 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007909#ifdef FEAT_FLOAT
7910 {"float2nr", 1, 1, f_float2nr},
7911 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007912 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007913#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007914 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"fnamemodify", 2, 2, f_fnamemodify},
7916 {"foldclosed", 1, 1, f_foldclosed},
7917 {"foldclosedend", 1, 1, f_foldclosedend},
7918 {"foldlevel", 1, 1, f_foldlevel},
7919 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007920 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007922 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007923 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007924 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007925 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007926 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"getchar", 0, 1, f_getchar},
7928 {"getcharmod", 0, 0, f_getcharmod},
7929 {"getcmdline", 0, 0, f_getcmdline},
7930 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007931 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007933 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007934 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {"getfsize", 1, 1, f_getfsize},
7936 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007937 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007938 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007939 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007940 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007941 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007942 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007943 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007944 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007946 {"gettabvar", 2, 3, f_gettabvar},
7947 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"getwinposx", 0, 0, f_getwinposx},
7949 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007950 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007951 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007952 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007954 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007955 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007956 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7958 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7959 {"histadd", 2, 2, f_histadd},
7960 {"histdel", 1, 2, f_histdel},
7961 {"histget", 1, 2, f_histget},
7962 {"histnr", 1, 1, f_histnr},
7963 {"hlID", 1, 1, f_hlID},
7964 {"hlexists", 1, 1, f_hlexists},
7965 {"hostname", 0, 0, f_hostname},
7966 {"iconv", 3, 3, f_iconv},
7967 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007968 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007969 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007971 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 {"inputrestore", 0, 0, f_inputrestore},
7973 {"inputsave", 0, 0, f_inputsave},
7974 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007975 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007976 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007978 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007979 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007980 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007981 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007983 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {"libcall", 3, 3, f_libcall},
7985 {"libcallnr", 3, 3, f_libcallnr},
7986 {"line", 1, 1, f_line},
7987 {"line2byte", 1, 1, f_line2byte},
7988 {"lispindent", 1, 1, f_lispindent},
7989 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007990#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007991 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007992 {"log10", 1, 1, f_log10},
7993#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007994#ifdef FEAT_LUA
7995 {"luaeval", 1, 2, f_luaeval},
7996#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007997 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007998 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007999 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008000 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008001 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008002 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008003 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008004 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008005 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008006 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008007 {"max", 1, 1, f_max},
8008 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008009#ifdef vim_mkdir
8010 {"mkdir", 1, 3, f_mkdir},
8011#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008012 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008013#ifdef FEAT_MZSCHEME
8014 {"mzeval", 1, 1, f_mzeval},
8015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008017 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008018 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008019 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008020#ifdef FEAT_FLOAT
8021 {"pow", 2, 2, f_pow},
8022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008024 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008025 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008026#ifdef FEAT_PYTHON3
8027 {"py3eval", 1, 1, f_py3eval},
8028#endif
8029#ifdef FEAT_PYTHON
8030 {"pyeval", 1, 1, f_pyeval},
8031#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008032 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008033 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008034 {"reltime", 0, 2, f_reltime},
8035 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {"remote_expr", 2, 3, f_remote_expr},
8037 {"remote_foreground", 1, 1, f_remote_foreground},
8038 {"remote_peek", 1, 2, f_remote_peek},
8039 {"remote_read", 1, 1, f_remote_read},
8040 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008041 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008043 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008045 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008046#ifdef FEAT_FLOAT
8047 {"round", 1, 1, f_round},
8048#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008049 {"screenattr", 2, 2, f_screenattr},
8050 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008051 {"screencol", 0, 0, f_screencol},
8052 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008053 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008054 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008055 {"searchpair", 3, 7, f_searchpair},
8056 {"searchpairpos", 3, 7, f_searchpairpos},
8057 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"server2client", 2, 2, f_server2client},
8059 {"serverlist", 0, 0, f_serverlist},
8060 {"setbufvar", 3, 3, f_setbufvar},
8061 {"setcmdpos", 1, 1, f_setcmdpos},
8062 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008063 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008064 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008065 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008066 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008068 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008069 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008071#ifdef FEAT_CRYPT
8072 {"sha256", 1, 1, f_sha256},
8073#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008074 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008075 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008077#ifdef FEAT_FLOAT
8078 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008079 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008080#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008081 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008082 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008083 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008084 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008085 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008086#ifdef FEAT_FLOAT
8087 {"sqrt", 1, 1, f_sqrt},
8088 {"str2float", 1, 1, f_str2float},
8089#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008090 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008091 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008092 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093#ifdef HAVE_STRFTIME
8094 {"strftime", 1, 2, f_strftime},
8095#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008096 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008097 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"strlen", 1, 1, f_strlen},
8099 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008100 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008102 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"submatch", 1, 1, f_submatch},
8104 {"substitute", 4, 4, f_substitute},
8105 {"synID", 3, 3, f_synID},
8106 {"synIDattr", 2, 3, f_synIDattr},
8107 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008108 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008109 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008110 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008111 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008112 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008113 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008114 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008115 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008116#ifdef FEAT_FLOAT
8117 {"tan", 1, 1, f_tan},
8118 {"tanh", 1, 1, f_tanh},
8119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008121 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"tolower", 1, 1, f_tolower},
8123 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008124 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008125#ifdef FEAT_FLOAT
8126 {"trunc", 1, 1, f_trunc},
8127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008129 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008130 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008131 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"virtcol", 1, 1, f_virtcol},
8133 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008134 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"winbufnr", 1, 1, f_winbufnr},
8136 {"wincol", 0, 0, f_wincol},
8137 {"winheight", 1, 1, f_winheight},
8138 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008139 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008141 {"winrestview", 1, 1, f_winrestview},
8142 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008144 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008145 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146};
8147
8148#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8149
8150/*
8151 * Function given to ExpandGeneric() to obtain the list of internal
8152 * or user defined function names.
8153 */
8154 char_u *
8155get_function_name(xp, idx)
8156 expand_T *xp;
8157 int idx;
8158{
8159 static int intidx = -1;
8160 char_u *name;
8161
8162 if (idx == 0)
8163 intidx = -1;
8164 if (intidx < 0)
8165 {
8166 name = get_user_func_name(xp, idx);
8167 if (name != NULL)
8168 return name;
8169 }
8170 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8171 {
8172 STRCPY(IObuff, functions[intidx].f_name);
8173 STRCAT(IObuff, "(");
8174 if (functions[intidx].f_max_argc == 0)
8175 STRCAT(IObuff, ")");
8176 return IObuff;
8177 }
8178
8179 return NULL;
8180}
8181
8182/*
8183 * Function given to ExpandGeneric() to obtain the list of internal or
8184 * user defined variable or function names.
8185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 char_u *
8187get_expr_name(xp, idx)
8188 expand_T *xp;
8189 int idx;
8190{
8191 static int intidx = -1;
8192 char_u *name;
8193
8194 if (idx == 0)
8195 intidx = -1;
8196 if (intidx < 0)
8197 {
8198 name = get_function_name(xp, idx);
8199 if (name != NULL)
8200 return name;
8201 }
8202 return get_user_var_name(xp, ++intidx);
8203}
8204
8205#endif /* FEAT_CMDL_COMPL */
8206
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008207#if defined(EBCDIC) || defined(PROTO)
8208/*
8209 * Compare struct fst by function name.
8210 */
8211 static int
8212compare_func_name(s1, s2)
8213 const void *s1;
8214 const void *s2;
8215{
8216 struct fst *p1 = (struct fst *)s1;
8217 struct fst *p2 = (struct fst *)s2;
8218
8219 return STRCMP(p1->f_name, p2->f_name);
8220}
8221
8222/*
8223 * Sort the function table by function name.
8224 * The sorting of the table above is ASCII dependant.
8225 * On machines using EBCDIC we have to sort it.
8226 */
8227 static void
8228sortFunctions()
8229{
8230 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8231
8232 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8233}
8234#endif
8235
8236
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237/*
8238 * Find internal function in table above.
8239 * Return index, or -1 if not found
8240 */
8241 static int
8242find_internal_func(name)
8243 char_u *name; /* name of the function */
8244{
8245 int first = 0;
8246 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8247 int cmp;
8248 int x;
8249
8250 /*
8251 * Find the function name in the table. Binary search.
8252 */
8253 while (first <= last)
8254 {
8255 x = first + ((unsigned)(last - first) >> 1);
8256 cmp = STRCMP(name, functions[x].f_name);
8257 if (cmp < 0)
8258 last = x - 1;
8259 else if (cmp > 0)
8260 first = x + 1;
8261 else
8262 return x;
8263 }
8264 return -1;
8265}
8266
8267/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008268 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8269 * name it contains, otherwise return "name".
8270 */
8271 static char_u *
8272deref_func_name(name, lenp)
8273 char_u *name;
8274 int *lenp;
8275{
Bram Moolenaar33570922005-01-25 22:26:29 +00008276 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008277 int cc;
8278
8279 cc = name[*lenp];
8280 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008281 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008282 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008283 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008284 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008285 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008286 {
8287 *lenp = 0;
8288 return (char_u *)""; /* just in case */
8289 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008290 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008291 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008292 }
8293
8294 return name;
8295}
8296
8297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 * Allocate a variable for the result of a function.
8299 * Return OK or FAIL.
8300 */
8301 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008302get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8303 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 char_u *name; /* name of the function */
8305 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 char_u **arg; /* argument, pointing to the '(' */
8308 linenr_T firstline; /* first line of range */
8309 linenr_T lastline; /* last line of range */
8310 int *doesrange; /* return: function handled range */
8311 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008312 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313{
8314 char_u *argp;
8315 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008316 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 int argcount = 0; /* number of arguments found */
8318
8319 /*
8320 * Get the arguments.
8321 */
8322 argp = *arg;
8323 while (argcount < MAX_FUNC_ARGS)
8324 {
8325 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8326 if (*argp == ')' || *argp == ',' || *argp == NUL)
8327 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8329 {
8330 ret = FAIL;
8331 break;
8332 }
8333 ++argcount;
8334 if (*argp != ',')
8335 break;
8336 }
8337 if (*argp == ')')
8338 ++argp;
8339 else
8340 ret = FAIL;
8341
8342 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008344 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008346 {
8347 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008348 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008350 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352
8353 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008354 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355
8356 *arg = skipwhite(argp);
8357 return ret;
8358}
8359
8360
8361/*
8362 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008363 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008364 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 */
8366 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008367call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008368 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008369 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008371 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008373 typval_T *argvars; /* vars for arguments, must have "argcount"
8374 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 linenr_T firstline; /* first line of range */
8376 linenr_T lastline; /* last line of range */
8377 int *doesrange; /* return: function handled range */
8378 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008379 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380{
8381 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382#define ERROR_UNKNOWN 0
8383#define ERROR_TOOMANY 1
8384#define ERROR_TOOFEW 2
8385#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008386#define ERROR_DICT 4
8387#define ERROR_NONE 5
8388#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 int error = ERROR_NONE;
8390 int i;
8391 int llen;
8392 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393#define FLEN_FIXED 40
8394 char_u fname_buf[FLEN_FIXED + 1];
8395 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008396 char_u *name;
8397
8398 /* Make a copy of the name, if it comes from a funcref variable it could
8399 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008400 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008401 if (name == NULL)
8402 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403
8404 /*
8405 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8406 * Change <SNR>123_name() to K_SNR 123_name().
8407 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 llen = eval_fname_script(name);
8410 if (llen > 0)
8411 {
8412 fname_buf[0] = K_SPECIAL;
8413 fname_buf[1] = KS_EXTRA;
8414 fname_buf[2] = (int)KE_SNR;
8415 i = 3;
8416 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8417 {
8418 if (current_SID <= 0)
8419 error = ERROR_SCRIPT;
8420 else
8421 {
8422 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8423 i = (int)STRLEN(fname_buf);
8424 }
8425 }
8426 if (i + STRLEN(name + llen) < FLEN_FIXED)
8427 {
8428 STRCPY(fname_buf + i, name + llen);
8429 fname = fname_buf;
8430 }
8431 else
8432 {
8433 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8434 if (fname == NULL)
8435 error = ERROR_OTHER;
8436 else
8437 {
8438 mch_memmove(fname, fname_buf, (size_t)i);
8439 STRCPY(fname + i, name + llen);
8440 }
8441 }
8442 }
8443 else
8444 fname = name;
8445
8446 *doesrange = FALSE;
8447
8448
8449 /* execute the function if no errors detected and executing */
8450 if (evaluate && error == ERROR_NONE)
8451 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008452 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8453 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 error = ERROR_UNKNOWN;
8455
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008456 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {
8458 /*
8459 * User defined function.
8460 */
8461 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008462
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008464 /* Trigger FuncUndefined event, may load the function. */
8465 if (fp == NULL
8466 && apply_autocmds(EVENT_FUNCUNDEFINED,
8467 fname, fname, TRUE, NULL)
8468 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008470 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 fp = find_func(fname);
8472 }
8473#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008474 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008475 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008476 {
8477 /* loaded a package, search for the function again */
8478 fp = find_func(fname);
8479 }
8480
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 if (fp != NULL)
8482 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008483 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008485 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008487 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008489 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008490 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 else
8492 {
8493 /*
8494 * Call the user function.
8495 * Save and restore search patterns, script variables and
8496 * redo buffer.
8497 */
8498 save_search_patterns();
8499 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008500 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008501 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008502 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008503 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8504 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8505 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008506 /* Function was unreferenced while being used, free it
8507 * now. */
8508 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 restoreRedobuff();
8510 restore_search_patterns();
8511 error = ERROR_NONE;
8512 }
8513 }
8514 }
8515 else
8516 {
8517 /*
8518 * Find the function name in the table, call its implementation.
8519 */
8520 i = find_internal_func(fname);
8521 if (i >= 0)
8522 {
8523 if (argcount < functions[i].f_min_argc)
8524 error = ERROR_TOOFEW;
8525 else if (argcount > functions[i].f_max_argc)
8526 error = ERROR_TOOMANY;
8527 else
8528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008530 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 error = ERROR_NONE;
8532 }
8533 }
8534 }
8535 /*
8536 * The function call (or "FuncUndefined" autocommand sequence) might
8537 * have been aborted by an error, an interrupt, or an explicitly thrown
8538 * exception that has not been caught so far. This situation can be
8539 * tested for by calling aborting(). For an error in an internal
8540 * function or for the "E132" error in call_user_func(), however, the
8541 * throw point at which the "force_abort" flag (temporarily reset by
8542 * emsg()) is normally updated has not been reached yet. We need to
8543 * update that flag first to make aborting() reliable.
8544 */
8545 update_force_abort();
8546 }
8547 if (error == ERROR_NONE)
8548 ret = OK;
8549
8550 /*
8551 * Report an error unless the argument evaluation or function call has been
8552 * cancelled due to an aborting error, an interrupt, or an exception.
8553 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008554 if (!aborting())
8555 {
8556 switch (error)
8557 {
8558 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008559 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008560 break;
8561 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008562 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008563 break;
8564 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008565 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008566 name);
8567 break;
8568 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008569 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008570 name);
8571 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008572 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008573 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008574 name);
8575 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008576 }
8577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 if (fname != name && fname != fname_buf)
8580 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008581 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582
8583 return ret;
8584}
8585
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008586/*
8587 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008588 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008589 */
8590 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008591emsg_funcname(ermsg, name)
8592 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008593 char_u *name;
8594{
8595 char_u *p;
8596
8597 if (*name == K_SPECIAL)
8598 p = concat_str((char_u *)"<SNR>", name + 3);
8599 else
8600 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008601 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008602 if (p != name)
8603 vim_free(p);
8604}
8605
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008606/*
8607 * Return TRUE for a non-zero Number and a non-empty String.
8608 */
8609 static int
8610non_zero_arg(argvars)
8611 typval_T *argvars;
8612{
8613 return ((argvars[0].v_type == VAR_NUMBER
8614 && argvars[0].vval.v_number != 0)
8615 || (argvars[0].v_type == VAR_STRING
8616 && argvars[0].vval.v_string != NULL
8617 && *argvars[0].vval.v_string != NUL));
8618}
8619
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620/*********************************************
8621 * Implementation of the built-in functions
8622 */
8623
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008624#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008625static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8626
8627/*
8628 * Get the float value of "argvars[0]" into "f".
8629 * Returns FAIL when the argument is not a Number or Float.
8630 */
8631 static int
8632get_float_arg(argvars, f)
8633 typval_T *argvars;
8634 float_T *f;
8635{
8636 if (argvars[0].v_type == VAR_FLOAT)
8637 {
8638 *f = argvars[0].vval.v_float;
8639 return OK;
8640 }
8641 if (argvars[0].v_type == VAR_NUMBER)
8642 {
8643 *f = (float_T)argvars[0].vval.v_number;
8644 return OK;
8645 }
8646 EMSG(_("E808: Number or Float required"));
8647 return FAIL;
8648}
8649
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008650/*
8651 * "abs(expr)" function
8652 */
8653 static void
8654f_abs(argvars, rettv)
8655 typval_T *argvars;
8656 typval_T *rettv;
8657{
8658 if (argvars[0].v_type == VAR_FLOAT)
8659 {
8660 rettv->v_type = VAR_FLOAT;
8661 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8662 }
8663 else
8664 {
8665 varnumber_T n;
8666 int error = FALSE;
8667
8668 n = get_tv_number_chk(&argvars[0], &error);
8669 if (error)
8670 rettv->vval.v_number = -1;
8671 else if (n > 0)
8672 rettv->vval.v_number = n;
8673 else
8674 rettv->vval.v_number = -n;
8675 }
8676}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008677
8678/*
8679 * "acos()" function
8680 */
8681 static void
8682f_acos(argvars, rettv)
8683 typval_T *argvars;
8684 typval_T *rettv;
8685{
8686 float_T f;
8687
8688 rettv->v_type = VAR_FLOAT;
8689 if (get_float_arg(argvars, &f) == OK)
8690 rettv->vval.v_float = acos(f);
8691 else
8692 rettv->vval.v_float = 0.0;
8693}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008694#endif
8695
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008697 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 */
8699 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008700f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008701 typval_T *argvars;
8702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703{
Bram Moolenaar33570922005-01-25 22:26:29 +00008704 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008706 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008707 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008709 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008710 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008711 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008713 }
8714 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008715 EMSG(_(e_listreq));
8716}
8717
8718/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008719 * "and(expr, expr)" function
8720 */
8721 static void
8722f_and(argvars, rettv)
8723 typval_T *argvars;
8724 typval_T *rettv;
8725{
8726 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8727 & get_tv_number_chk(&argvars[1], NULL);
8728}
8729
8730/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008731 * "append(lnum, string/list)" function
8732 */
8733 static void
8734f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008735 typval_T *argvars;
8736 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008737{
8738 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008740 list_T *l = NULL;
8741 listitem_T *li = NULL;
8742 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008743 long added = 0;
8744
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008745 /* When coming here from Insert mode, sync undo, so that this can be
8746 * undone separately from what was previously inserted. */
8747 if (u_sync_once == 2)
8748 {
8749 u_sync_once = 1; /* notify that u_sync() was called */
8750 u_sync(TRUE);
8751 }
8752
Bram Moolenaar0d660222005-01-07 21:51:51 +00008753 lnum = get_tv_lnum(argvars);
8754 if (lnum >= 0
8755 && lnum <= curbuf->b_ml.ml_line_count
8756 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008757 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008758 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008759 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008760 l = argvars[1].vval.v_list;
8761 if (l == NULL)
8762 return;
8763 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008764 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008765 for (;;)
8766 {
8767 if (l == NULL)
8768 tv = &argvars[1]; /* append a string */
8769 else if (li == NULL)
8770 break; /* end of list */
8771 else
8772 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008773 line = get_tv_string_chk(tv);
8774 if (line == NULL) /* type error */
8775 {
8776 rettv->vval.v_number = 1; /* Failed */
8777 break;
8778 }
8779 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008780 ++added;
8781 if (l == NULL)
8782 break;
8783 li = li->li_next;
8784 }
8785
8786 appended_lines_mark(lnum, added);
8787 if (curwin->w_cursor.lnum > lnum)
8788 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008790 else
8791 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792}
8793
8794/*
8795 * "argc()" function
8796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008798f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008799 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803}
8804
8805/*
8806 * "argidx()" function
8807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008809f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008810 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008813 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814}
8815
8816/*
8817 * "argv(nr)" function
8818 */
8819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008820f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008821 typval_T *argvars;
8822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823{
8824 int idx;
8825
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008826 if (argvars[0].v_type != VAR_UNKNOWN)
8827 {
8828 idx = get_tv_number_chk(&argvars[0], NULL);
8829 if (idx >= 0 && idx < ARGCOUNT)
8830 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8831 else
8832 rettv->vval.v_string = NULL;
8833 rettv->v_type = VAR_STRING;
8834 }
8835 else if (rettv_list_alloc(rettv) == OK)
8836 for (idx = 0; idx < ARGCOUNT; ++idx)
8837 list_append_string(rettv->vval.v_list,
8838 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839}
8840
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008841#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008842/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008843 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008844 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008845 static void
8846f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008847 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008848 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008849{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008850 float_T f;
8851
8852 rettv->v_type = VAR_FLOAT;
8853 if (get_float_arg(argvars, &f) == OK)
8854 rettv->vval.v_float = asin(f);
8855 else
8856 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008857}
8858
8859/*
8860 * "atan()" function
8861 */
8862 static void
8863f_atan(argvars, rettv)
8864 typval_T *argvars;
8865 typval_T *rettv;
8866{
8867 float_T f;
8868
8869 rettv->v_type = VAR_FLOAT;
8870 if (get_float_arg(argvars, &f) == OK)
8871 rettv->vval.v_float = atan(f);
8872 else
8873 rettv->vval.v_float = 0.0;
8874}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008875
8876/*
8877 * "atan2()" function
8878 */
8879 static void
8880f_atan2(argvars, rettv)
8881 typval_T *argvars;
8882 typval_T *rettv;
8883{
8884 float_T fx, fy;
8885
8886 rettv->v_type = VAR_FLOAT;
8887 if (get_float_arg(argvars, &fx) == OK
8888 && get_float_arg(&argvars[1], &fy) == OK)
8889 rettv->vval.v_float = atan2(fx, fy);
8890 else
8891 rettv->vval.v_float = 0.0;
8892}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008893#endif
8894
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895/*
8896 * "browse(save, title, initdir, default)" function
8897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008900 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902{
8903#ifdef FEAT_BROWSE
8904 int save;
8905 char_u *title;
8906 char_u *initdir;
8907 char_u *defname;
8908 char_u buf[NUMBUFLEN];
8909 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008912 save = get_tv_number_chk(&argvars[0], &error);
8913 title = get_tv_string_chk(&argvars[1]);
8914 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8915 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 if (error || title == NULL || initdir == NULL || defname == NULL)
8918 rettv->vval.v_string = NULL;
8919 else
8920 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008921 do_browse(save ? BROWSE_SAVE : 0,
8922 title, defname, NULL, initdir, NULL, curbuf);
8923#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008925#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008926 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008927}
8928
8929/*
8930 * "browsedir(title, initdir)" function
8931 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008933f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008934 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008935 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008936{
8937#ifdef FEAT_BROWSE
8938 char_u *title;
8939 char_u *initdir;
8940 char_u buf[NUMBUFLEN];
8941
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008942 title = get_tv_string_chk(&argvars[0]);
8943 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008944
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008945 if (title == NULL || initdir == NULL)
8946 rettv->vval.v_string = NULL;
8947 else
8948 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008949 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008951 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954}
8955
Bram Moolenaar33570922005-01-25 22:26:29 +00008956static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008957
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958/*
8959 * Find a buffer by number or exact name.
8960 */
8961 static buf_T *
8962find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008963 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964{
8965 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008967 if (avar->v_type == VAR_NUMBER)
8968 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008969 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008971 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008972 if (buf == NULL)
8973 {
8974 /* No full path name match, try a match with a URL or a "nofile"
8975 * buffer, these don't use the full path. */
8976 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8977 if (buf->b_fname != NULL
8978 && (path_with_url(buf->b_fname)
8979#ifdef FEAT_QUICKFIX
8980 || bt_nofile(buf)
8981#endif
8982 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008983 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008984 break;
8985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 }
8987 return buf;
8988}
8989
8990/*
8991 * "bufexists(expr)" function
8992 */
8993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008995 typval_T *argvars;
8996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999}
9000
9001/*
9002 * "buflisted(expr)" function
9003 */
9004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009006 typval_T *argvars;
9007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
9009 buf_T *buf;
9010
9011 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013}
9014
9015/*
9016 * "bufloaded(expr)" function
9017 */
9018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009019f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009020 typval_T *argvars;
9021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022{
9023 buf_T *buf;
9024
9025 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027}
9028
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009029static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009030
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031/*
9032 * Get buffer by number or pattern.
9033 */
9034 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009035get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009036 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009037 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 int save_magic;
9041 char_u *save_cpo;
9042 buf_T *buf;
9043
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009044 if (tv->v_type == VAR_NUMBER)
9045 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009046 if (tv->v_type != VAR_STRING)
9047 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 if (name == NULL || *name == NUL)
9049 return curbuf;
9050 if (name[0] == '$' && name[1] == NUL)
9051 return lastbuf;
9052
9053 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9054 save_magic = p_magic;
9055 p_magic = TRUE;
9056 save_cpo = p_cpo;
9057 p_cpo = (char_u *)"";
9058
9059 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009060 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061
9062 p_magic = save_magic;
9063 p_cpo = save_cpo;
9064
9065 /* If not found, try expanding the name, like done for bufexists(). */
9066 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009067 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068
9069 return buf;
9070}
9071
9072/*
9073 * "bufname(expr)" function
9074 */
9075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009076f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009077 typval_T *argvars;
9078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079{
9080 buf_T *buf;
9081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009082 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009084 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 --emsg_off;
9091}
9092
9093/*
9094 * "bufnr(expr)" function
9095 */
9096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009098 typval_T *argvars;
9099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100{
9101 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009102 int error = FALSE;
9103 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009105 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009107 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009108 --emsg_off;
9109
9110 /* If the buffer isn't found and the second argument is not zero create a
9111 * new buffer. */
9112 if (buf == NULL
9113 && argvars[1].v_type != VAR_UNKNOWN
9114 && get_tv_number_chk(&argvars[1], &error) != 0
9115 && !error
9116 && (name = get_tv_string_chk(&argvars[0])) != NULL
9117 && !error)
9118 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9119
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124}
9125
9126/*
9127 * "bufwinnr(nr)" function
9128 */
9129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009130f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009131 typval_T *argvars;
9132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133{
9134#ifdef FEAT_WINDOWS
9135 win_T *wp;
9136 int winnr = 0;
9137#endif
9138 buf_T *buf;
9139
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009140 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009142 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143#ifdef FEAT_WINDOWS
9144 for (wp = firstwin; wp; wp = wp->w_next)
9145 {
9146 ++winnr;
9147 if (wp->w_buffer == buf)
9148 break;
9149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009150 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153#endif
9154 --emsg_off;
9155}
9156
9157/*
9158 * "byte2line(byte)" function
9159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009162 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
9165#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167#else
9168 long boff = 0;
9169
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009170 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009172 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 (linenr_T)0, &boff);
9176#endif
9177}
9178
9179/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009180 * "byteidx()" function
9181 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009184 typval_T *argvars;
9185 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009186{
9187#ifdef FEAT_MBYTE
9188 char_u *t;
9189#endif
9190 char_u *str;
9191 long idx;
9192
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009193 str = get_tv_string_chk(&argvars[0]);
9194 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009196 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009197 return;
9198
9199#ifdef FEAT_MBYTE
9200 t = str;
9201 for ( ; idx > 0; idx--)
9202 {
9203 if (*t == NUL) /* EOL reached */
9204 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009205 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009206 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009207 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009208#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009209 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009210 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009211#endif
9212}
9213
Bram Moolenaardb913952012-06-29 12:54:53 +02009214 int
9215func_call(name, args, selfdict, rettv)
9216 char_u *name;
9217 typval_T *args;
9218 dict_T *selfdict;
9219 typval_T *rettv;
9220{
9221 listitem_T *item;
9222 typval_T argv[MAX_FUNC_ARGS + 1];
9223 int argc = 0;
9224 int dummy;
9225 int r = 0;
9226
9227 for (item = args->vval.v_list->lv_first; item != NULL;
9228 item = item->li_next)
9229 {
9230 if (argc == MAX_FUNC_ARGS)
9231 {
9232 EMSG(_("E699: Too many arguments"));
9233 break;
9234 }
9235 /* Make a copy of each argument. This is needed to be able to set
9236 * v_lock to VAR_FIXED in the copy without changing the original list.
9237 */
9238 copy_tv(&item->li_tv, &argv[argc++]);
9239 }
9240
9241 if (item == NULL)
9242 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9243 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9244 &dummy, TRUE, selfdict);
9245
9246 /* Free the arguments. */
9247 while (argc > 0)
9248 clear_tv(&argv[--argc]);
9249
9250 return r;
9251}
9252
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009253/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009254 * "call(func, arglist)" function
9255 */
9256 static void
9257f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009258 typval_T *argvars;
9259 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009260{
9261 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009262 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009263
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009264 if (argvars[1].v_type != VAR_LIST)
9265 {
9266 EMSG(_(e_listreq));
9267 return;
9268 }
9269 if (argvars[1].vval.v_list == NULL)
9270 return;
9271
9272 if (argvars[0].v_type == VAR_FUNC)
9273 func = argvars[0].vval.v_string;
9274 else
9275 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009276 if (*func == NUL)
9277 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009278
Bram Moolenaare9a41262005-01-15 22:18:47 +00009279 if (argvars[2].v_type != VAR_UNKNOWN)
9280 {
9281 if (argvars[2].v_type != VAR_DICT)
9282 {
9283 EMSG(_(e_dictreq));
9284 return;
9285 }
9286 selfdict = argvars[2].vval.v_dict;
9287 }
9288
Bram Moolenaardb913952012-06-29 12:54:53 +02009289 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009290}
9291
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009292#ifdef FEAT_FLOAT
9293/*
9294 * "ceil({float})" function
9295 */
9296 static void
9297f_ceil(argvars, rettv)
9298 typval_T *argvars;
9299 typval_T *rettv;
9300{
9301 float_T f;
9302
9303 rettv->v_type = VAR_FLOAT;
9304 if (get_float_arg(argvars, &f) == OK)
9305 rettv->vval.v_float = ceil(f);
9306 else
9307 rettv->vval.v_float = 0.0;
9308}
9309#endif
9310
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009311/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009312 * "changenr()" function
9313 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009314 static void
9315f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009316 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009317 typval_T *rettv;
9318{
9319 rettv->vval.v_number = curbuf->b_u_seq_cur;
9320}
9321
9322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 * "char2nr(string)" function
9324 */
9325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009327 typval_T *argvars;
9328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329{
9330#ifdef FEAT_MBYTE
9331 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009332 {
9333 int utf8 = 0;
9334
9335 if (argvars[1].v_type != VAR_UNKNOWN)
9336 utf8 = get_tv_number_chk(&argvars[1], NULL);
9337
9338 if (utf8)
9339 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9340 else
9341 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 else
9344#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009345 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346}
9347
9348/*
9349 * "cindent(lnum)" function
9350 */
9351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009353 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355{
9356#ifdef FEAT_CINDENT
9357 pos_T pos;
9358 linenr_T lnum;
9359
9360 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9363 {
9364 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009365 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 curwin->w_cursor = pos;
9367 }
9368 else
9369#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371}
9372
9373/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009374 * "clearmatches()" function
9375 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009376 static void
9377f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009378 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009379 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009380{
9381#ifdef FEAT_SEARCH_EXTRA
9382 clear_matches(curwin);
9383#endif
9384}
9385
9386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 * "col(string)" function
9388 */
9389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009390f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009391 typval_T *argvars;
9392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
9394 colnr_T col = 0;
9395 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009396 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009398 fp = var2fpos(&argvars[0], FALSE, &fnum);
9399 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 {
9401 if (fp->col == MAXCOL)
9402 {
9403 /* '> can be MAXCOL, get the length of the line then */
9404 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009405 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 else
9407 col = MAXCOL;
9408 }
9409 else
9410 {
9411 col = fp->col + 1;
9412#ifdef FEAT_VIRTUALEDIT
9413 /* col(".") when the cursor is on the NUL at the end of the line
9414 * because of "coladd" can be seen as an extra column. */
9415 if (virtual_active() && fp == &curwin->w_cursor)
9416 {
9417 char_u *p = ml_get_cursor();
9418
9419 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9420 curwin->w_virtcol - curwin->w_cursor.coladd))
9421 {
9422# ifdef FEAT_MBYTE
9423 int l;
9424
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009425 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 col += l;
9427# else
9428 if (*p != NUL && p[1] == NUL)
9429 ++col;
9430# endif
9431 }
9432 }
9433#endif
9434 }
9435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437}
9438
Bram Moolenaar572cb562005-08-05 21:35:02 +00009439#if defined(FEAT_INS_EXPAND)
9440/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009441 * "complete()" function
9442 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009443 static void
9444f_complete(argvars, rettv)
9445 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009446 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009447{
9448 int startcol;
9449
9450 if ((State & INSERT) == 0)
9451 {
9452 EMSG(_("E785: complete() can only be used in Insert mode"));
9453 return;
9454 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009455
9456 /* Check for undo allowed here, because if something was already inserted
9457 * the line was already saved for undo and this check isn't done. */
9458 if (!undo_allowed())
9459 return;
9460
Bram Moolenaarade00832006-03-10 21:46:58 +00009461 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9462 {
9463 EMSG(_(e_invarg));
9464 return;
9465 }
9466
9467 startcol = get_tv_number_chk(&argvars[0], NULL);
9468 if (startcol <= 0)
9469 return;
9470
9471 set_completion(startcol - 1, argvars[1].vval.v_list);
9472}
9473
9474/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009475 * "complete_add()" function
9476 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009477 static void
9478f_complete_add(argvars, rettv)
9479 typval_T *argvars;
9480 typval_T *rettv;
9481{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009482 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009483}
9484
9485/*
9486 * "complete_check()" function
9487 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009488 static void
9489f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009490 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009491 typval_T *rettv;
9492{
9493 int saved = RedrawingDisabled;
9494
9495 RedrawingDisabled = 0;
9496 ins_compl_check_keys(0);
9497 rettv->vval.v_number = compl_interrupted;
9498 RedrawingDisabled = saved;
9499}
9500#endif
9501
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502/*
9503 * "confirm(message, buttons[, default [, type]])" function
9504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009507 typval_T *argvars UNUSED;
9508 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
9510#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9511 char_u *message;
9512 char_u *buttons = NULL;
9513 char_u buf[NUMBUFLEN];
9514 char_u buf2[NUMBUFLEN];
9515 int def = 1;
9516 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009517 char_u *typestr;
9518 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009520 message = get_tv_string_chk(&argvars[0]);
9521 if (message == NULL)
9522 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009523 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009525 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9526 if (buttons == NULL)
9527 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009528 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009530 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009531 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009533 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9534 if (typestr == NULL)
9535 error = TRUE;
9536 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009538 switch (TOUPPER_ASC(*typestr))
9539 {
9540 case 'E': type = VIM_ERROR; break;
9541 case 'Q': type = VIM_QUESTION; break;
9542 case 'I': type = VIM_INFO; break;
9543 case 'W': type = VIM_WARNING; break;
9544 case 'G': type = VIM_GENERIC; break;
9545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 }
9547 }
9548 }
9549 }
9550
9551 if (buttons == NULL || *buttons == NUL)
9552 buttons = (char_u *)_("&Ok");
9553
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009554 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009555 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009556 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557#endif
9558}
9559
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009560/*
9561 * "copy()" function
9562 */
9563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009565 typval_T *argvars;
9566 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009567{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009568 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009571#ifdef FEAT_FLOAT
9572/*
9573 * "cos()" function
9574 */
9575 static void
9576f_cos(argvars, rettv)
9577 typval_T *argvars;
9578 typval_T *rettv;
9579{
9580 float_T f;
9581
9582 rettv->v_type = VAR_FLOAT;
9583 if (get_float_arg(argvars, &f) == OK)
9584 rettv->vval.v_float = cos(f);
9585 else
9586 rettv->vval.v_float = 0.0;
9587}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009588
9589/*
9590 * "cosh()" function
9591 */
9592 static void
9593f_cosh(argvars, rettv)
9594 typval_T *argvars;
9595 typval_T *rettv;
9596{
9597 float_T f;
9598
9599 rettv->v_type = VAR_FLOAT;
9600 if (get_float_arg(argvars, &f) == OK)
9601 rettv->vval.v_float = cosh(f);
9602 else
9603 rettv->vval.v_float = 0.0;
9604}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009605#endif
9606
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009608 * "count()" function
9609 */
9610 static void
9611f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009612 typval_T *argvars;
9613 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009614{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009615 long n = 0;
9616 int ic = FALSE;
9617
Bram Moolenaare9a41262005-01-15 22:18:47 +00009618 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009619 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009620 listitem_T *li;
9621 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009622 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009623
Bram Moolenaare9a41262005-01-15 22:18:47 +00009624 if ((l = argvars[0].vval.v_list) != NULL)
9625 {
9626 li = l->lv_first;
9627 if (argvars[2].v_type != VAR_UNKNOWN)
9628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009629 int error = FALSE;
9630
9631 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009632 if (argvars[3].v_type != VAR_UNKNOWN)
9633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634 idx = get_tv_number_chk(&argvars[3], &error);
9635 if (!error)
9636 {
9637 li = list_find(l, idx);
9638 if (li == NULL)
9639 EMSGN(_(e_listidx), idx);
9640 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009641 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009642 if (error)
9643 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009644 }
9645
9646 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009647 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009648 ++n;
9649 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009650 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009651 else if (argvars[0].v_type == VAR_DICT)
9652 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009653 int todo;
9654 dict_T *d;
9655 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009656
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009657 if ((d = argvars[0].vval.v_dict) != NULL)
9658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009659 int error = FALSE;
9660
Bram Moolenaare9a41262005-01-15 22:18:47 +00009661 if (argvars[2].v_type != VAR_UNKNOWN)
9662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009663 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009664 if (argvars[3].v_type != VAR_UNKNOWN)
9665 EMSG(_(e_invarg));
9666 }
9667
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009668 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009669 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009670 {
9671 if (!HASHITEM_EMPTY(hi))
9672 {
9673 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009674 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009675 ++n;
9676 }
9677 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009678 }
9679 }
9680 else
9681 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009682 rettv->vval.v_number = n;
9683}
9684
9685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9687 *
9688 * Checks the existence of a cscope connection.
9689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009692 typval_T *argvars UNUSED;
9693 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694{
9695#ifdef FEAT_CSCOPE
9696 int num = 0;
9697 char_u *dbpath = NULL;
9698 char_u *prepend = NULL;
9699 char_u buf[NUMBUFLEN];
9700
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009701 if (argvars[0].v_type != VAR_UNKNOWN
9702 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 num = (int)get_tv_number(&argvars[0]);
9705 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009706 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 }
9709
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009710 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711#endif
9712}
9713
9714/*
9715 * "cursor(lnum, col)" function
9716 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009717 * Moves the cursor to the specified line and column.
9718 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009722 typval_T *argvars;
9723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724{
9725 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009726#ifdef FEAT_VIRTUALEDIT
9727 long coladd = 0;
9728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009730 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009731 if (argvars[1].v_type == VAR_UNKNOWN)
9732 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009733 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009734
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009735 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009736 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009737 line = pos.lnum;
9738 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009739#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009740 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009741#endif
9742 }
9743 else
9744 {
9745 line = get_tv_lnum(argvars);
9746 col = get_tv_number_chk(&argvars[1], NULL);
9747#ifdef FEAT_VIRTUALEDIT
9748 if (argvars[2].v_type != VAR_UNKNOWN)
9749 coladd = get_tv_number_chk(&argvars[2], NULL);
9750#endif
9751 }
9752 if (line < 0 || col < 0
9753#ifdef FEAT_VIRTUALEDIT
9754 || coladd < 0
9755#endif
9756 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009757 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 if (line > 0)
9759 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 if (col > 0)
9761 curwin->w_cursor.col = col - 1;
9762#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009763 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764#endif
9765
9766 /* Make sure the cursor is in a valid position. */
9767 check_cursor();
9768#ifdef FEAT_MBYTE
9769 /* Correct cursor for multi-byte character. */
9770 if (has_mbyte)
9771 mb_adjust_cursor();
9772#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009773
9774 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009775 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776}
9777
9778/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009779 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 */
9781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009783 typval_T *argvars;
9784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009786 int noref = 0;
9787
9788 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009789 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009790 if (noref < 0 || noref > 1)
9791 EMSG(_(e_invarg));
9792 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009793 {
9794 current_copyID += COPYID_INC;
9795 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797}
9798
9799/*
9800 * "delete()" function
9801 */
9802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009804 typval_T *argvars;
9805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806{
9807 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009810 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811}
9812
9813/*
9814 * "did_filetype()" function
9815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009818 typval_T *argvars UNUSED;
9819 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820{
9821#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823#endif
9824}
9825
9826/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009827 * "diff_filler()" function
9828 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009831 typval_T *argvars UNUSED;
9832 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009833{
9834#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009836#endif
9837}
9838
9839/*
9840 * "diff_hlID()" function
9841 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009844 typval_T *argvars UNUSED;
9845 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009846{
9847#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009849 static linenr_T prev_lnum = 0;
9850 static int changedtick = 0;
9851 static int fnum = 0;
9852 static int change_start = 0;
9853 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009854 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009855 int filler_lines;
9856 int col;
9857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009858 if (lnum < 0) /* ignore type error in {lnum} arg */
9859 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009860 if (lnum != prev_lnum
9861 || changedtick != curbuf->b_changedtick
9862 || fnum != curbuf->b_fnum)
9863 {
9864 /* New line, buffer, change: need to get the values. */
9865 filler_lines = diff_check(curwin, lnum);
9866 if (filler_lines < 0)
9867 {
9868 if (filler_lines == -1)
9869 {
9870 change_start = MAXCOL;
9871 change_end = -1;
9872 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9873 hlID = HLF_ADD; /* added line */
9874 else
9875 hlID = HLF_CHD; /* changed line */
9876 }
9877 else
9878 hlID = HLF_ADD; /* added line */
9879 }
9880 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009881 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009882 prev_lnum = lnum;
9883 changedtick = curbuf->b_changedtick;
9884 fnum = curbuf->b_fnum;
9885 }
9886
9887 if (hlID == HLF_CHD || hlID == HLF_TXD)
9888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009889 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009890 if (col >= change_start && col <= change_end)
9891 hlID = HLF_TXD; /* changed text */
9892 else
9893 hlID = HLF_CHD; /* changed line */
9894 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009895 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009896#endif
9897}
9898
9899/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009900 * "empty({expr})" function
9901 */
9902 static void
9903f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009904 typval_T *argvars;
9905 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009906{
9907 int n;
9908
9909 switch (argvars[0].v_type)
9910 {
9911 case VAR_STRING:
9912 case VAR_FUNC:
9913 n = argvars[0].vval.v_string == NULL
9914 || *argvars[0].vval.v_string == NUL;
9915 break;
9916 case VAR_NUMBER:
9917 n = argvars[0].vval.v_number == 0;
9918 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009919#ifdef FEAT_FLOAT
9920 case VAR_FLOAT:
9921 n = argvars[0].vval.v_float == 0.0;
9922 break;
9923#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009924 case VAR_LIST:
9925 n = argvars[0].vval.v_list == NULL
9926 || argvars[0].vval.v_list->lv_first == NULL;
9927 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 case VAR_DICT:
9929 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009930 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009931 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009932 default:
9933 EMSG2(_(e_intern2), "f_empty()");
9934 n = 0;
9935 }
9936
9937 rettv->vval.v_number = n;
9938}
9939
9940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 * "escape({string}, {chars})" function
9942 */
9943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009945 typval_T *argvars;
9946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947{
9948 char_u buf[NUMBUFLEN];
9949
Bram Moolenaar758711c2005-02-02 23:11:38 +00009950 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9951 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009952 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953}
9954
9955/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009956 * "eval()" function
9957 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009958 static void
9959f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009960 typval_T *argvars;
9961 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009962{
9963 char_u *s;
9964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009965 s = get_tv_string_chk(&argvars[0]);
9966 if (s != NULL)
9967 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009968
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009969 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9970 {
9971 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009972 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009973 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009974 else if (*s != NUL)
9975 EMSG(_(e_trailing));
9976}
9977
9978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 * "eventhandler()" function
9980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009983 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987}
9988
9989/*
9990 * "executable()" function
9991 */
9992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009993f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009994 typval_T *argvars;
9995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009997 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998}
9999
10000/*
10001 * "exists()" function
10002 */
10003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010004f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010005 typval_T *argvars;
10006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007{
10008 char_u *p;
10009 char_u *name;
10010 int n = FALSE;
10011 int len = 0;
10012
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010013 no_autoload = TRUE;
10014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010015 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 if (*p == '$') /* environment variable */
10017 {
10018 /* first try "normal" environment variables (fast) */
10019 if (mch_getenv(p + 1) != NULL)
10020 n = TRUE;
10021 else
10022 {
10023 /* try expanding things like $VIM and ${HOME} */
10024 p = expand_env_save(p);
10025 if (p != NULL && *p != '$')
10026 n = TRUE;
10027 vim_free(p);
10028 }
10029 }
10030 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010033 if (*skipwhite(p) != NUL)
10034 n = FALSE; /* trailing garbage */
10035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 else if (*p == '*') /* internal or user defined function */
10037 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010038 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 }
10040 else if (*p == ':')
10041 {
10042 n = cmd_exists(p + 1);
10043 }
10044 else if (*p == '#')
10045 {
10046#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010047 if (p[1] == '#')
10048 n = autocmd_supported(p + 2);
10049 else
10050 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051#endif
10052 }
10053 else /* internal variable */
10054 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010055 char_u *tofree;
10056 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010058 /* get_name_len() takes care of expanding curly braces */
10059 name = p;
10060 len = get_name_len(&p, &tofree, TRUE, FALSE);
10061 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010063 if (tofree != NULL)
10064 name = tofree;
10065 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10066 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010068 /* handle d.key, l[idx], f(expr) */
10069 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10070 if (n)
10071 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 }
10073 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010074 if (*p != NUL)
10075 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010077 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 }
10079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010080 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010081
10082 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083}
10084
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010085#ifdef FEAT_FLOAT
10086/*
10087 * "exp()" function
10088 */
10089 static void
10090f_exp(argvars, rettv)
10091 typval_T *argvars;
10092 typval_T *rettv;
10093{
10094 float_T f;
10095
10096 rettv->v_type = VAR_FLOAT;
10097 if (get_float_arg(argvars, &f) == OK)
10098 rettv->vval.v_float = exp(f);
10099 else
10100 rettv->vval.v_float = 0.0;
10101}
10102#endif
10103
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104/*
10105 * "expand()" function
10106 */
10107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010109 typval_T *argvars;
10110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111{
10112 char_u *s;
10113 int len;
10114 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010115 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010117 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010118 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010121 if (argvars[1].v_type != VAR_UNKNOWN
10122 && argvars[2].v_type != VAR_UNKNOWN
10123 && get_tv_number_chk(&argvars[2], &error)
10124 && !error)
10125 {
10126 rettv->v_type = VAR_LIST;
10127 rettv->vval.v_list = NULL;
10128 }
10129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010130 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 if (*s == '%' || *s == '#' || *s == '<')
10132 {
10133 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010134 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010136 if (rettv->v_type == VAR_LIST)
10137 {
10138 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10139 list_append_string(rettv->vval.v_list, result, -1);
10140 else
10141 vim_free(result);
10142 }
10143 else
10144 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 }
10146 else
10147 {
10148 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010149 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010150 if (argvars[1].v_type != VAR_UNKNOWN
10151 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010152 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010153 if (!error)
10154 {
10155 ExpandInit(&xpc);
10156 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010157 if (p_wic)
10158 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010159 if (rettv->v_type == VAR_STRING)
10160 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10161 options, WILD_ALL);
10162 else if (rettv_list_alloc(rettv) != FAIL)
10163 {
10164 int i;
10165
10166 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10167 for (i = 0; i < xpc.xp_numfiles; i++)
10168 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10169 ExpandCleanup(&xpc);
10170 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010171 }
10172 else
10173 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 }
10175}
10176
10177/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010178 * Go over all entries in "d2" and add them to "d1".
10179 * When "action" is "error" then a duplicate key is an error.
10180 * When "action" is "force" then a duplicate key is overwritten.
10181 * Otherwise duplicate keys are ignored ("action" is "keep").
10182 */
10183 void
10184dict_extend(d1, d2, action)
10185 dict_T *d1;
10186 dict_T *d2;
10187 char_u *action;
10188{
10189 dictitem_T *di1;
10190 hashitem_T *hi2;
10191 int todo;
10192
10193 todo = (int)d2->dv_hashtab.ht_used;
10194 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10195 {
10196 if (!HASHITEM_EMPTY(hi2))
10197 {
10198 --todo;
10199 di1 = dict_find(d1, hi2->hi_key, -1);
10200 if (d1->dv_scope != 0)
10201 {
10202 /* Disallow replacing a builtin function in l: and g:.
10203 * Check the key to be valid when adding to any
10204 * scope. */
10205 if (d1->dv_scope == VAR_DEF_SCOPE
10206 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10207 && var_check_func_name(hi2->hi_key,
10208 di1 == NULL))
10209 break;
10210 if (!valid_varname(hi2->hi_key))
10211 break;
10212 }
10213 if (di1 == NULL)
10214 {
10215 di1 = dictitem_copy(HI2DI(hi2));
10216 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10217 dictitem_free(di1);
10218 }
10219 else if (*action == 'e')
10220 {
10221 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10222 break;
10223 }
10224 else if (*action == 'f' && HI2DI(hi2) != di1)
10225 {
10226 clear_tv(&di1->di_tv);
10227 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10228 }
10229 }
10230 }
10231}
10232
10233/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010234 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010235 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010236 */
10237 static void
10238f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010239 typval_T *argvars;
10240 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010241{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010242 char *arg_errmsg = N_("extend() argument");
10243
Bram Moolenaare9a41262005-01-15 22:18:47 +000010244 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010245 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010246 list_T *l1, *l2;
10247 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010248 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010249 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010250
Bram Moolenaare9a41262005-01-15 22:18:47 +000010251 l1 = argvars[0].vval.v_list;
10252 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010253 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010254 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010255 {
10256 if (argvars[2].v_type != VAR_UNKNOWN)
10257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010258 before = get_tv_number_chk(&argvars[2], &error);
10259 if (error)
10260 return; /* type error; errmsg already given */
10261
Bram Moolenaar758711c2005-02-02 23:11:38 +000010262 if (before == l1->lv_len)
10263 item = NULL;
10264 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010265 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010266 item = list_find(l1, before);
10267 if (item == NULL)
10268 {
10269 EMSGN(_(e_listidx), before);
10270 return;
10271 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010272 }
10273 }
10274 else
10275 item = NULL;
10276 list_extend(l1, l2, item);
10277
Bram Moolenaare9a41262005-01-15 22:18:47 +000010278 copy_tv(&argvars[0], rettv);
10279 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010280 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010281 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10282 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010283 dict_T *d1, *d2;
10284 char_u *action;
10285 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010286
10287 d1 = argvars[0].vval.v_dict;
10288 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010289 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010290 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010291 {
10292 /* Check the third argument. */
10293 if (argvars[2].v_type != VAR_UNKNOWN)
10294 {
10295 static char *(av[]) = {"keep", "force", "error"};
10296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010297 action = get_tv_string_chk(&argvars[2]);
10298 if (action == NULL)
10299 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010300 for (i = 0; i < 3; ++i)
10301 if (STRCMP(action, av[i]) == 0)
10302 break;
10303 if (i == 3)
10304 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010305 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010306 return;
10307 }
10308 }
10309 else
10310 action = (char_u *)"force";
10311
Bram Moolenaara9922d62013-05-30 13:01:18 +020010312 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010313
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314 copy_tv(&argvars[0], rettv);
10315 }
10316 }
10317 else
10318 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010319}
10320
10321/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010322 * "feedkeys()" function
10323 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010324 static void
10325f_feedkeys(argvars, rettv)
10326 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010327 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010328{
10329 int remap = TRUE;
10330 char_u *keys, *flags;
10331 char_u nbuf[NUMBUFLEN];
10332 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010333 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010334
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010335 /* This is not allowed in the sandbox. If the commands would still be
10336 * executed in the sandbox it would be OK, but it probably happens later,
10337 * when "sandbox" is no longer set. */
10338 if (check_secure())
10339 return;
10340
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010341 keys = get_tv_string(&argvars[0]);
10342 if (*keys != NUL)
10343 {
10344 if (argvars[1].v_type != VAR_UNKNOWN)
10345 {
10346 flags = get_tv_string_buf(&argvars[1], nbuf);
10347 for ( ; *flags != NUL; ++flags)
10348 {
10349 switch (*flags)
10350 {
10351 case 'n': remap = FALSE; break;
10352 case 'm': remap = TRUE; break;
10353 case 't': typed = TRUE; break;
10354 }
10355 }
10356 }
10357
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010358 /* Need to escape K_SPECIAL and CSI before putting the string in the
10359 * typeahead buffer. */
10360 keys_esc = vim_strsave_escape_csi(keys);
10361 if (keys_esc != NULL)
10362 {
10363 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010364 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010365 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010366 if (vgetc_busy)
10367 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010368 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010369 }
10370}
10371
10372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 * "filereadable()" function
10374 */
10375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010376f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010377 typval_T *argvars;
10378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010380 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 char_u *p;
10382 int n;
10383
Bram Moolenaarc236c162008-07-13 17:41:49 +000010384#ifndef O_NONBLOCK
10385# define O_NONBLOCK 0
10386#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010387 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010388 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10389 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 {
10391 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010392 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 }
10394 else
10395 n = FALSE;
10396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010397 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398}
10399
10400/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010401 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 * rights to write into.
10403 */
10404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010405f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010406 typval_T *argvars;
10407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010409 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410}
10411
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010412static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010413
10414 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010415findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010416 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010417 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010418 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010419{
10420#ifdef FEAT_SEARCHPATH
10421 char_u *fname;
10422 char_u *fresult = NULL;
10423 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10424 char_u *p;
10425 char_u pathbuf[NUMBUFLEN];
10426 int count = 1;
10427 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010428 int error = FALSE;
10429#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010430
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010431 rettv->vval.v_string = NULL;
10432 rettv->v_type = VAR_STRING;
10433
10434#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010435 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010436
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010437 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10440 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010441 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010442 else
10443 {
10444 if (*p != NUL)
10445 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010448 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010449 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010450 }
10451
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010452 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10453 error = TRUE;
10454
10455 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010457 do
10458 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010459 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010460 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010461 fresult = find_file_in_path_option(first ? fname : NULL,
10462 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010463 0, first, path,
10464 find_what,
10465 curbuf->b_ffname,
10466 find_what == FINDFILE_DIR
10467 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010468 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010469
10470 if (fresult != NULL && rettv->v_type == VAR_LIST)
10471 list_append_string(rettv->vval.v_list, fresult, -1);
10472
10473 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010474 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010475
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010476 if (rettv->v_type == VAR_STRING)
10477 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010478#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010479}
10480
Bram Moolenaar33570922005-01-25 22:26:29 +000010481static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10482static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010483
10484/*
10485 * Implementation of map() and filter().
10486 */
10487 static void
10488filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010489 typval_T *argvars;
10490 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010491 int map;
10492{
10493 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010494 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010495 listitem_T *li, *nli;
10496 list_T *l = NULL;
10497 dictitem_T *di;
10498 hashtab_T *ht;
10499 hashitem_T *hi;
10500 dict_T *d = NULL;
10501 typval_T save_val;
10502 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010503 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010504 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010505 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10506 char *arg_errmsg = (map ? N_("map() argument")
10507 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010508 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010509 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010510
Bram Moolenaare9a41262005-01-15 22:18:47 +000010511 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010512 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010513 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010514 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010515 return;
10516 }
10517 else if (argvars[0].v_type == VAR_DICT)
10518 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010519 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010520 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010521 return;
10522 }
10523 else
10524 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010525 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010526 return;
10527 }
10528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010529 expr = get_tv_string_buf_chk(&argvars[1], buf);
10530 /* On type errors, the preceding call has already displayed an error
10531 * message. Avoid a misleading error message for an empty string that
10532 * was not passed as argument. */
10533 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010535 prepare_vimvar(VV_VAL, &save_val);
10536 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010537
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010538 /* We reset "did_emsg" to be able to detect whether an error
10539 * occurred during evaluation of the expression. */
10540 save_did_emsg = did_emsg;
10541 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010542
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010543 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010545 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010546 vimvars[VV_KEY].vv_type = VAR_STRING;
10547
10548 ht = &d->dv_hashtab;
10549 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010550 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010551 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010553 if (!HASHITEM_EMPTY(hi))
10554 {
10555 --todo;
10556 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010557 if (tv_check_lock(di->di_tv.v_lock,
10558 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010559 break;
10560 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010561 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010562 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010563 break;
10564 if (!map && rem)
10565 dictitem_remove(d, di);
10566 clear_tv(&vimvars[VV_KEY].vv_tv);
10567 }
10568 }
10569 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 }
10571 else
10572 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010573 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010575 for (li = l->lv_first; li != NULL; li = nli)
10576 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010577 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010578 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010579 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010580 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010581 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010582 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010583 break;
10584 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010585 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010586 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010587 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010588 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010589
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010590 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010591 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010592
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010593 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010594 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010595
10596 copy_tv(&argvars[0], rettv);
10597}
10598
10599 static int
10600filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010601 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010602 char_u *expr;
10603 int map;
10604 int *remp;
10605{
Bram Moolenaar33570922005-01-25 22:26:29 +000010606 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010607 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010608 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010609
Bram Moolenaar33570922005-01-25 22:26:29 +000010610 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010611 s = expr;
10612 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010613 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010614 if (*s != NUL) /* check for trailing chars after expr */
10615 {
10616 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010617 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010618 }
10619 if (map)
10620 {
10621 /* map(): replace the list item value */
10622 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010623 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010624 *tv = rettv;
10625 }
10626 else
10627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010628 int error = FALSE;
10629
Bram Moolenaare9a41262005-01-15 22:18:47 +000010630 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010631 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010632 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010633 /* On type error, nothing has been removed; return FAIL to stop the
10634 * loop. The error message was given by get_tv_number_chk(). */
10635 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010636 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010637 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010638 retval = OK;
10639theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010640 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010641 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010642}
10643
10644/*
10645 * "filter()" function
10646 */
10647 static void
10648f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010649 typval_T *argvars;
10650 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010651{
10652 filter_map(argvars, rettv, FALSE);
10653}
10654
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010655/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010656 * "finddir({fname}[, {path}[, {count}]])" function
10657 */
10658 static void
10659f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010660 typval_T *argvars;
10661 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010662{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010663 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010664}
10665
10666/*
10667 * "findfile({fname}[, {path}[, {count}]])" function
10668 */
10669 static void
10670f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 typval_T *argvars;
10672 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010673{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010674 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010675}
10676
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010677#ifdef FEAT_FLOAT
10678/*
10679 * "float2nr({float})" function
10680 */
10681 static void
10682f_float2nr(argvars, rettv)
10683 typval_T *argvars;
10684 typval_T *rettv;
10685{
10686 float_T f;
10687
10688 if (get_float_arg(argvars, &f) == OK)
10689 {
10690 if (f < -0x7fffffff)
10691 rettv->vval.v_number = -0x7fffffff;
10692 else if (f > 0x7fffffff)
10693 rettv->vval.v_number = 0x7fffffff;
10694 else
10695 rettv->vval.v_number = (varnumber_T)f;
10696 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010697}
10698
10699/*
10700 * "floor({float})" function
10701 */
10702 static void
10703f_floor(argvars, rettv)
10704 typval_T *argvars;
10705 typval_T *rettv;
10706{
10707 float_T f;
10708
10709 rettv->v_type = VAR_FLOAT;
10710 if (get_float_arg(argvars, &f) == OK)
10711 rettv->vval.v_float = floor(f);
10712 else
10713 rettv->vval.v_float = 0.0;
10714}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010715
10716/*
10717 * "fmod()" function
10718 */
10719 static void
10720f_fmod(argvars, rettv)
10721 typval_T *argvars;
10722 typval_T *rettv;
10723{
10724 float_T fx, fy;
10725
10726 rettv->v_type = VAR_FLOAT;
10727 if (get_float_arg(argvars, &fx) == OK
10728 && get_float_arg(&argvars[1], &fy) == OK)
10729 rettv->vval.v_float = fmod(fx, fy);
10730 else
10731 rettv->vval.v_float = 0.0;
10732}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010733#endif
10734
Bram Moolenaar0d660222005-01-07 21:51:51 +000010735/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010736 * "fnameescape({string})" function
10737 */
10738 static void
10739f_fnameescape(argvars, rettv)
10740 typval_T *argvars;
10741 typval_T *rettv;
10742{
10743 rettv->vval.v_string = vim_strsave_fnameescape(
10744 get_tv_string(&argvars[0]), FALSE);
10745 rettv->v_type = VAR_STRING;
10746}
10747
10748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 * "fnamemodify({fname}, {mods})" function
10750 */
10751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010753 typval_T *argvars;
10754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755{
10756 char_u *fname;
10757 char_u *mods;
10758 int usedlen = 0;
10759 int len;
10760 char_u *fbuf = NULL;
10761 char_u buf[NUMBUFLEN];
10762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010763 fname = get_tv_string_chk(&argvars[0]);
10764 mods = get_tv_string_buf_chk(&argvars[1], buf);
10765 if (fname == NULL || mods == NULL)
10766 fname = NULL;
10767 else
10768 {
10769 len = (int)STRLEN(fname);
10770 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010773 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010775 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 vim_free(fbuf);
10779}
10780
Bram Moolenaar33570922005-01-25 22:26:29 +000010781static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782
10783/*
10784 * "foldclosed()" function
10785 */
10786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010787foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010788 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010789 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010790 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791{
10792#ifdef FEAT_FOLDING
10793 linenr_T lnum;
10794 linenr_T first, last;
10795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10798 {
10799 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10800 {
10801 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010802 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010804 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 return;
10806 }
10807 }
10808#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810}
10811
10812/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010813 * "foldclosed()" function
10814 */
10815 static void
10816f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010817 typval_T *argvars;
10818 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010819{
10820 foldclosed_both(argvars, rettv, FALSE);
10821}
10822
10823/*
10824 * "foldclosedend()" function
10825 */
10826 static void
10827f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010828 typval_T *argvars;
10829 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010830{
10831 foldclosed_both(argvars, rettv, TRUE);
10832}
10833
10834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 * "foldlevel()" function
10836 */
10837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010839 typval_T *argvars UNUSED;
10840 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841{
10842#ifdef FEAT_FOLDING
10843 linenr_T lnum;
10844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010847 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849}
10850
10851/*
10852 * "foldtext()" function
10853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010855f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010856 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858{
10859#ifdef FEAT_FOLDING
10860 linenr_T lnum;
10861 char_u *s;
10862 char_u *r;
10863 int len;
10864 char *txt;
10865#endif
10866
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867 rettv->v_type = VAR_STRING;
10868 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010870 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10871 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10872 <= curbuf->b_ml.ml_line_count
10873 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 {
10875 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010876 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10877 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 {
10879 if (!linewhite(lnum))
10880 break;
10881 ++lnum;
10882 }
10883
10884 /* Find interesting text in this line. */
10885 s = skipwhite(ml_get(lnum));
10886 /* skip C comment-start */
10887 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010890 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010891 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010892 {
10893 s = skipwhite(ml_get(lnum + 1));
10894 if (*s == '*')
10895 s = skipwhite(s + 1);
10896 }
10897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 txt = _("+-%s%3ld lines: ");
10899 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010900 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 + 20 /* for %3ld */
10902 + STRLEN(s))); /* concatenated */
10903 if (r != NULL)
10904 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010905 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10906 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10907 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 len = (int)STRLEN(r);
10909 STRCAT(r, s);
10910 /* remove 'foldmarker' and 'commentstring' */
10911 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 }
10914 }
10915#endif
10916}
10917
10918/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010919 * "foldtextresult(lnum)" function
10920 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010923 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010924 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010925{
10926#ifdef FEAT_FOLDING
10927 linenr_T lnum;
10928 char_u *text;
10929 char_u buf[51];
10930 foldinfo_T foldinfo;
10931 int fold_count;
10932#endif
10933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010934 rettv->v_type = VAR_STRING;
10935 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010936#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010938 /* treat illegal types and illegal string values for {lnum} the same */
10939 if (lnum < 0)
10940 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010941 fold_count = foldedCount(curwin, lnum, &foldinfo);
10942 if (fold_count > 0)
10943 {
10944 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10945 &foldinfo, buf);
10946 if (text == buf)
10947 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010948 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010949 }
10950#endif
10951}
10952
10953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 * "foreground()" function
10955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010958 typval_T *argvars UNUSED;
10959 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961#ifdef FEAT_GUI
10962 if (gui.in_use)
10963 gui_mch_set_foreground();
10964#else
10965# ifdef WIN32
10966 win32_set_foreground();
10967# endif
10968#endif
10969}
10970
10971/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010972 * "function()" function
10973 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010976 typval_T *argvars;
10977 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010978{
10979 char_u *s;
10980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010981 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010982 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010983 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010984 /* Don't check an autoload name for existence here. */
10985 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010986 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010987 else
10988 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010989 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010990 {
10991 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010992 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010993
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010994 /* Expand s: and <SID> into <SNR>nr_, so that the function can
10995 * also be called from another script. Using trans_function_name()
10996 * would also work, but some plugins depend on the name being
10997 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010998 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020010999 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011000 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011001 if (rettv->vval.v_string != NULL)
11002 {
11003 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011004 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011005 }
11006 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011007 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011008 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011009 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011010 }
11011}
11012
11013/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011014 * "garbagecollect()" function
11015 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011016 static void
11017f_garbagecollect(argvars, rettv)
11018 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011019 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011020{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011021 /* This is postponed until we are back at the toplevel, because we may be
11022 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11023 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011024
11025 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11026 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011027}
11028
11029/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011030 * "get()" function
11031 */
11032 static void
11033f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011034 typval_T *argvars;
11035 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011036{
Bram Moolenaar33570922005-01-25 22:26:29 +000011037 listitem_T *li;
11038 list_T *l;
11039 dictitem_T *di;
11040 dict_T *d;
11041 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011042
Bram Moolenaare9a41262005-01-15 22:18:47 +000011043 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011044 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011045 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011046 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011047 int error = FALSE;
11048
11049 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11050 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011051 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011052 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011053 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011054 else if (argvars[0].v_type == VAR_DICT)
11055 {
11056 if ((d = argvars[0].vval.v_dict) != NULL)
11057 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011058 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011059 if (di != NULL)
11060 tv = &di->di_tv;
11061 }
11062 }
11063 else
11064 EMSG2(_(e_listdictarg), "get()");
11065
11066 if (tv == NULL)
11067 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011068 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011069 copy_tv(&argvars[2], rettv);
11070 }
11071 else
11072 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011073}
11074
Bram Moolenaar342337a2005-07-21 21:11:17 +000011075static 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 +000011076
11077/*
11078 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011079 * Return a range (from start to end) of lines in rettv from the specified
11080 * buffer.
11081 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011082 */
11083 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011084get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011085 buf_T *buf;
11086 linenr_T start;
11087 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011088 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011089 typval_T *rettv;
11090{
11091 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011092
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011093 if (retlist && rettv_list_alloc(rettv) == FAIL)
11094 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011095
11096 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11097 return;
11098
11099 if (!retlist)
11100 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011101 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11102 p = ml_get_buf(buf, start, FALSE);
11103 else
11104 p = (char_u *)"";
11105
11106 rettv->v_type = VAR_STRING;
11107 rettv->vval.v_string = vim_strsave(p);
11108 }
11109 else
11110 {
11111 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011112 return;
11113
11114 if (start < 1)
11115 start = 1;
11116 if (end > buf->b_ml.ml_line_count)
11117 end = buf->b_ml.ml_line_count;
11118 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011119 if (list_append_string(rettv->vval.v_list,
11120 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011121 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011122 }
11123}
11124
11125/*
11126 * "getbufline()" function
11127 */
11128 static void
11129f_getbufline(argvars, rettv)
11130 typval_T *argvars;
11131 typval_T *rettv;
11132{
11133 linenr_T lnum;
11134 linenr_T end;
11135 buf_T *buf;
11136
11137 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11138 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011139 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011140 --emsg_off;
11141
Bram Moolenaar661b1822005-07-28 22:36:45 +000011142 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011143 if (argvars[2].v_type == VAR_UNKNOWN)
11144 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011145 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011146 end = get_tv_lnum_buf(&argvars[2], buf);
11147
Bram Moolenaar342337a2005-07-21 21:11:17 +000011148 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011149}
11150
Bram Moolenaar0d660222005-01-07 21:51:51 +000011151/*
11152 * "getbufvar()" function
11153 */
11154 static void
11155f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011156 typval_T *argvars;
11157 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011158{
11159 buf_T *buf;
11160 buf_T *save_curbuf;
11161 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011162 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011163 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011165 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11166 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011167 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011168 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011169
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011170 rettv->v_type = VAR_STRING;
11171 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011172
11173 if (buf != NULL && varname != NULL)
11174 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011175 /* set curbuf to be our buf, temporarily */
11176 save_curbuf = curbuf;
11177 curbuf = buf;
11178
Bram Moolenaar0d660222005-01-07 21:51:51 +000011179 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011180 {
11181 if (get_option_tv(&varname, rettv, TRUE) == OK)
11182 done = TRUE;
11183 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011184 else if (STRCMP(varname, "changedtick") == 0)
11185 {
11186 rettv->v_type = VAR_NUMBER;
11187 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011188 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011189 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011190 else
11191 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011192 /* Look up the variable. */
11193 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11194 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11195 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011196 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011197 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011198 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011199 done = TRUE;
11200 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011201 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011202
11203 /* restore previous notion of curbuf */
11204 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011205 }
11206
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011207 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11208 /* use the default value */
11209 copy_tv(&argvars[2], rettv);
11210
Bram Moolenaar0d660222005-01-07 21:51:51 +000011211 --emsg_off;
11212}
11213
11214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 * "getchar()" function
11216 */
11217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011219 typval_T *argvars;
11220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221{
11222 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011223 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011225 /* Position the cursor. Needed after a message that ends in a space. */
11226 windgoto(msg_row, msg_col);
11227
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 ++no_mapping;
11229 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011230 for (;;)
11231 {
11232 if (argvars[0].v_type == VAR_UNKNOWN)
11233 /* getchar(): blocking wait. */
11234 n = safe_vgetc();
11235 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11236 /* getchar(1): only check if char avail */
11237 n = vpeekc();
11238 else if (error || vpeekc() == NUL)
11239 /* illegal argument or getchar(0) and no char avail: return zero */
11240 n = 0;
11241 else
11242 /* getchar(0) and char avail: return char */
11243 n = safe_vgetc();
11244 if (n == K_IGNORE)
11245 continue;
11246 break;
11247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 --no_mapping;
11249 --allow_keys;
11250
Bram Moolenaar219b8702006-11-01 14:32:36 +000011251 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11252 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11253 vimvars[VV_MOUSE_COL].vv_nr = 0;
11254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 if (IS_SPECIAL(n) || mod_mask != 0)
11257 {
11258 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11259 int i = 0;
11260
11261 /* Turn a special key into three bytes, plus modifier. */
11262 if (mod_mask != 0)
11263 {
11264 temp[i++] = K_SPECIAL;
11265 temp[i++] = KS_MODIFIER;
11266 temp[i++] = mod_mask;
11267 }
11268 if (IS_SPECIAL(n))
11269 {
11270 temp[i++] = K_SPECIAL;
11271 temp[i++] = K_SECOND(n);
11272 temp[i++] = K_THIRD(n);
11273 }
11274#ifdef FEAT_MBYTE
11275 else if (has_mbyte)
11276 i += (*mb_char2bytes)(n, temp + i);
11277#endif
11278 else
11279 temp[i++] = n;
11280 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 rettv->v_type = VAR_STRING;
11282 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011283
11284#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011285 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011286 {
11287 int row = mouse_row;
11288 int col = mouse_col;
11289 win_T *win;
11290 linenr_T lnum;
11291# ifdef FEAT_WINDOWS
11292 win_T *wp;
11293# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011294 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011295
11296 if (row >= 0 && col >= 0)
11297 {
11298 /* Find the window at the mouse coordinates and compute the
11299 * text position. */
11300 win = mouse_find_win(&row, &col);
11301 (void)mouse_comp_pos(win, &row, &col, &lnum);
11302# ifdef FEAT_WINDOWS
11303 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011304 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011305# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011306 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011307 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11308 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11309 }
11310 }
11311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 }
11313}
11314
11315/*
11316 * "getcharmod()" function
11317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011319f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011320 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324}
11325
11326/*
11327 * "getcmdline()" function
11328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011331 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334 rettv->v_type = VAR_STRING;
11335 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336}
11337
11338/*
11339 * "getcmdpos()" function
11340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011343 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347}
11348
11349/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011350 * "getcmdtype()" function
11351 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011352 static void
11353f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011354 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011355 typval_T *rettv;
11356{
11357 rettv->v_type = VAR_STRING;
11358 rettv->vval.v_string = alloc(2);
11359 if (rettv->vval.v_string != NULL)
11360 {
11361 rettv->vval.v_string[0] = get_cmdline_type();
11362 rettv->vval.v_string[1] = NUL;
11363 }
11364}
11365
11366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 * "getcwd()" function
11368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011370f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011371 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011374 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011377 rettv->vval.v_string = NULL;
11378 cwd = alloc(MAXPATHL);
11379 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011381 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11382 {
11383 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011385 if (rettv->vval.v_string != NULL)
11386 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011388 }
11389 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 }
11391}
11392
11393/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011394 * "getfontname()" function
11395 */
11396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011397f_getfontname(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 Moolenaar46c9c732004-12-12 11:37:09 +000011400{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 rettv->v_type = VAR_STRING;
11402 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011403#ifdef FEAT_GUI
11404 if (gui.in_use)
11405 {
11406 GuiFont font;
11407 char_u *name = NULL;
11408
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011409 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011410 {
11411 /* Get the "Normal" font. Either the name saved by
11412 * hl_set_font_name() or from the font ID. */
11413 font = gui.norm_font;
11414 name = hl_get_font_name();
11415 }
11416 else
11417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011419 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11420 return;
11421 font = gui_mch_get_font(name, FALSE);
11422 if (font == NOFONT)
11423 return; /* Invalid font name, return empty string. */
11424 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011426 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011427 gui_mch_free_font(font);
11428 }
11429#endif
11430}
11431
11432/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011433 * "getfperm({fname})" function
11434 */
11435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011437 typval_T *argvars;
11438 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011439{
11440 char_u *fname;
11441 struct stat st;
11442 char_u *perm = NULL;
11443 char_u flags[] = "rwx";
11444 int i;
11445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011449 if (mch_stat((char *)fname, &st) >= 0)
11450 {
11451 perm = vim_strsave((char_u *)"---------");
11452 if (perm != NULL)
11453 {
11454 for (i = 0; i < 9; i++)
11455 {
11456 if (st.st_mode & (1 << (8 - i)))
11457 perm[i] = flags[i % 3];
11458 }
11459 }
11460 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011462}
11463
11464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 * "getfsize({fname})" function
11466 */
11467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011469 typval_T *argvars;
11470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471{
11472 char_u *fname;
11473 struct stat st;
11474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478
11479 if (mch_stat((char *)fname, &st) >= 0)
11480 {
11481 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011486
11487 /* non-perfect check for overflow */
11488 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11489 rettv->vval.v_number = -2;
11490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491 }
11492 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011493 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494}
11495
11496/*
11497 * "getftime({fname})" function
11498 */
11499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011501 typval_T *argvars;
11502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503{
11504 char_u *fname;
11505 struct stat st;
11506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011507 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508
11509 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011510 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011512 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513}
11514
11515/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011516 * "getftype({fname})" function
11517 */
11518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011519f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011520 typval_T *argvars;
11521 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011522{
11523 char_u *fname;
11524 struct stat st;
11525 char_u *type = NULL;
11526 char *t;
11527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011528 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011530 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011531 if (mch_lstat((char *)fname, &st) >= 0)
11532 {
11533#ifdef S_ISREG
11534 if (S_ISREG(st.st_mode))
11535 t = "file";
11536 else if (S_ISDIR(st.st_mode))
11537 t = "dir";
11538# ifdef S_ISLNK
11539 else if (S_ISLNK(st.st_mode))
11540 t = "link";
11541# endif
11542# ifdef S_ISBLK
11543 else if (S_ISBLK(st.st_mode))
11544 t = "bdev";
11545# endif
11546# ifdef S_ISCHR
11547 else if (S_ISCHR(st.st_mode))
11548 t = "cdev";
11549# endif
11550# ifdef S_ISFIFO
11551 else if (S_ISFIFO(st.st_mode))
11552 t = "fifo";
11553# endif
11554# ifdef S_ISSOCK
11555 else if (S_ISSOCK(st.st_mode))
11556 t = "fifo";
11557# endif
11558 else
11559 t = "other";
11560#else
11561# ifdef S_IFMT
11562 switch (st.st_mode & S_IFMT)
11563 {
11564 case S_IFREG: t = "file"; break;
11565 case S_IFDIR: t = "dir"; break;
11566# ifdef S_IFLNK
11567 case S_IFLNK: t = "link"; break;
11568# endif
11569# ifdef S_IFBLK
11570 case S_IFBLK: t = "bdev"; break;
11571# endif
11572# ifdef S_IFCHR
11573 case S_IFCHR: t = "cdev"; break;
11574# endif
11575# ifdef S_IFIFO
11576 case S_IFIFO: t = "fifo"; break;
11577# endif
11578# ifdef S_IFSOCK
11579 case S_IFSOCK: t = "socket"; break;
11580# endif
11581 default: t = "other";
11582 }
11583# else
11584 if (mch_isdir(fname))
11585 t = "dir";
11586 else
11587 t = "file";
11588# endif
11589#endif
11590 type = vim_strsave((char_u *)t);
11591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011593}
11594
11595/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011596 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011597 */
11598 static void
11599f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011600 typval_T *argvars;
11601 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011602{
11603 linenr_T lnum;
11604 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011605 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011606
11607 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011608 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011609 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011610 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011611 retlist = FALSE;
11612 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011613 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011614 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011615 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011616 retlist = TRUE;
11617 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011618
Bram Moolenaar342337a2005-07-21 21:11:17 +000011619 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011620}
11621
11622/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011623 * "getmatches()" function
11624 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011625 static void
11626f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011627 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011628 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011629{
11630#ifdef FEAT_SEARCH_EXTRA
11631 dict_T *dict;
11632 matchitem_T *cur = curwin->w_match_head;
11633
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011634 if (rettv_list_alloc(rettv) == OK)
11635 {
11636 while (cur != NULL)
11637 {
11638 dict = dict_alloc();
11639 if (dict == NULL)
11640 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011641 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11642 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11643 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11644 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11645 list_append_dict(rettv->vval.v_list, dict);
11646 cur = cur->next;
11647 }
11648 }
11649#endif
11650}
11651
11652/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011653 * "getpid()" function
11654 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011655 static void
11656f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011657 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011658 typval_T *rettv;
11659{
11660 rettv->vval.v_number = mch_get_pid();
11661}
11662
11663/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011664 * "getpos(string)" function
11665 */
11666 static void
11667f_getpos(argvars, rettv)
11668 typval_T *argvars;
11669 typval_T *rettv;
11670{
11671 pos_T *fp;
11672 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011673 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011674
11675 if (rettv_list_alloc(rettv) == OK)
11676 {
11677 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011678 fp = var2fpos(&argvars[0], TRUE, &fnum);
11679 if (fnum != -1)
11680 list_append_number(l, (varnumber_T)fnum);
11681 else
11682 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011683 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11684 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011685 list_append_number(l, (fp != NULL)
11686 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011687 : (varnumber_T)0);
11688 list_append_number(l,
11689#ifdef FEAT_VIRTUALEDIT
11690 (fp != NULL) ? (varnumber_T)fp->coladd :
11691#endif
11692 (varnumber_T)0);
11693 }
11694 else
11695 rettv->vval.v_number = FALSE;
11696}
11697
11698/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011699 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011700 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011701 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011702f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011703 typval_T *argvars UNUSED;
11704 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011705{
11706#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011707 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011708#endif
11709
Bram Moolenaar2641f772005-03-25 21:58:17 +000011710#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011711 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011712 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011713 wp = NULL;
11714 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11715 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011716 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011717 if (wp == NULL)
11718 return;
11719 }
11720
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011721 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011722 }
11723#endif
11724}
11725
11726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 * "getreg()" function
11728 */
11729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011731 typval_T *argvars;
11732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733{
11734 char_u *strregname;
11735 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011736 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011737 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011739 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011741 strregname = get_tv_string_chk(&argvars[0]);
11742 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011743 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011744 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011747 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011748 regname = (strregname == NULL ? '"' : *strregname);
11749 if (regname == 0)
11750 regname = '"';
11751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011753 rettv->vval.v_string = error ? NULL :
11754 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755}
11756
11757/*
11758 * "getregtype()" function
11759 */
11760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011762 typval_T *argvars;
11763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764{
11765 char_u *strregname;
11766 int regname;
11767 char_u buf[NUMBUFLEN + 2];
11768 long reglen = 0;
11769
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011770 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011771 {
11772 strregname = get_tv_string_chk(&argvars[0]);
11773 if (strregname == NULL) /* type error; errmsg already given */
11774 {
11775 rettv->v_type = VAR_STRING;
11776 rettv->vval.v_string = NULL;
11777 return;
11778 }
11779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780 else
11781 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011782 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783
11784 regname = (strregname == NULL ? '"' : *strregname);
11785 if (regname == 0)
11786 regname = '"';
11787
11788 buf[0] = NUL;
11789 buf[1] = NUL;
11790 switch (get_reg_type(regname, &reglen))
11791 {
11792 case MLINE: buf[0] = 'V'; break;
11793 case MCHAR: buf[0] = 'v'; break;
11794#ifdef FEAT_VISUAL
11795 case MBLOCK:
11796 buf[0] = Ctrl_V;
11797 sprintf((char *)buf + 1, "%ld", reglen + 1);
11798 break;
11799#endif
11800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011801 rettv->v_type = VAR_STRING;
11802 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803}
11804
11805/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011806 * "gettabvar()" function
11807 */
11808 static void
11809f_gettabvar(argvars, rettv)
11810 typval_T *argvars;
11811 typval_T *rettv;
11812{
11813 tabpage_T *tp;
11814 dictitem_T *v;
11815 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011816 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011817
11818 rettv->v_type = VAR_STRING;
11819 rettv->vval.v_string = NULL;
11820
11821 varname = get_tv_string_chk(&argvars[1]);
11822 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11823 if (tp != NULL && varname != NULL)
11824 {
11825 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011826 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011827 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011828 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011829 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011830 done = TRUE;
11831 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011832 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011833
11834 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011835 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011836}
11837
11838/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011839 * "gettabwinvar()" function
11840 */
11841 static void
11842f_gettabwinvar(argvars, rettv)
11843 typval_T *argvars;
11844 typval_T *rettv;
11845{
11846 getwinvar(argvars, rettv, 1);
11847}
11848
11849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850 * "getwinposx()" function
11851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011854 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858#ifdef FEAT_GUI
11859 if (gui.in_use)
11860 {
11861 int x, y;
11862
11863 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 }
11866#endif
11867}
11868
11869/*
11870 * "getwinposy()" function
11871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011873f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011874 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011877 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878#ifdef FEAT_GUI
11879 if (gui.in_use)
11880 {
11881 int x, y;
11882
11883 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011884 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885 }
11886#endif
11887}
11888
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011889/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011890 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011891 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011892 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011893find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011894 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011895 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011896{
11897#ifdef FEAT_WINDOWS
11898 win_T *wp;
11899#endif
11900 int nr;
11901
11902 nr = get_tv_number_chk(vp, NULL);
11903
11904#ifdef FEAT_WINDOWS
11905 if (nr < 0)
11906 return NULL;
11907 if (nr == 0)
11908 return curwin;
11909
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011910 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11911 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011912 if (--nr <= 0)
11913 break;
11914 return wp;
11915#else
11916 if (nr == 0 || nr == 1)
11917 return curwin;
11918 return NULL;
11919#endif
11920}
11921
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922/*
11923 * "getwinvar()" function
11924 */
11925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011926f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011927 typval_T *argvars;
11928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011930 getwinvar(argvars, rettv, 0);
11931}
11932
11933/*
11934 * getwinvar() and gettabwinvar()
11935 */
11936 static void
11937getwinvar(argvars, rettv, off)
11938 typval_T *argvars;
11939 typval_T *rettv;
11940 int off; /* 1 for gettabwinvar() */
11941{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942 win_T *win, *oldcurwin;
11943 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011944 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011945 tabpage_T *tp = NULL;
11946 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011947 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011949#ifdef FEAT_WINDOWS
11950 if (off == 1)
11951 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11952 else
11953 tp = curtab;
11954#endif
11955 win = find_win_by_nr(&argvars[off], tp);
11956 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011957 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011959 rettv->v_type = VAR_STRING;
11960 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961
11962 if (win != NULL && varname != NULL)
11963 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011964 /* Set curwin to be our win, temporarily. Also set the tabpage,
11965 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020011966 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011967
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011969 {
11970 if (get_option_tv(&varname, rettv, 1) == OK)
11971 done = TRUE;
11972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 else
11974 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011975 /* Look up the variable. */
11976 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11977 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011979 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011980 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011981 done = TRUE;
11982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011984
11985 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020011986 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987 }
11988
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011989 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11990 /* use the default return value */
11991 copy_tv(&argvars[off + 2], rettv);
11992
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 --emsg_off;
11994}
11995
11996/*
11997 * "glob()" function
11998 */
11999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012000f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012001 typval_T *argvars;
12002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012004 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012006 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012008 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012009 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012010 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012011 if (argvars[1].v_type != VAR_UNKNOWN)
12012 {
12013 if (get_tv_number_chk(&argvars[1], &error))
12014 options |= WILD_KEEP_ALL;
12015 if (argvars[2].v_type != VAR_UNKNOWN
12016 && get_tv_number_chk(&argvars[2], &error))
12017 {
12018 rettv->v_type = VAR_LIST;
12019 rettv->vval.v_list = NULL;
12020 }
12021 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012022 if (!error)
12023 {
12024 ExpandInit(&xpc);
12025 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012026 if (p_wic)
12027 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012028 if (rettv->v_type == VAR_STRING)
12029 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012030 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012031 else if (rettv_list_alloc(rettv) != FAIL)
12032 {
12033 int i;
12034
12035 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12036 NULL, options, WILD_ALL_KEEP);
12037 for (i = 0; i < xpc.xp_numfiles; i++)
12038 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12039
12040 ExpandCleanup(&xpc);
12041 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012042 }
12043 else
12044 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045}
12046
12047/*
12048 * "globpath()" function
12049 */
12050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012051f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012052 typval_T *argvars;
12053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012055 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012057 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012058 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012060 /* When the optional second argument is non-zero, don't remove matches
12061 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12062 if (argvars[2].v_type != VAR_UNKNOWN
12063 && get_tv_number_chk(&argvars[2], &error))
12064 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012065 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012066 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012067 rettv->vval.v_string = NULL;
12068 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012069 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12070 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071}
12072
12073/*
12074 * "has()" function
12075 */
12076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012077f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012078 typval_T *argvars;
12079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080{
12081 int i;
12082 char_u *name;
12083 int n = FALSE;
12084 static char *(has_list[]) =
12085 {
12086#ifdef AMIGA
12087 "amiga",
12088# ifdef FEAT_ARP
12089 "arp",
12090# endif
12091#endif
12092#ifdef __BEOS__
12093 "beos",
12094#endif
12095#ifdef MSDOS
12096# ifdef DJGPP
12097 "dos32",
12098# else
12099 "dos16",
12100# endif
12101#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012102#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103 "mac",
12104#endif
12105#if defined(MACOS_X_UNIX)
12106 "macunix",
12107#endif
12108#ifdef OS2
12109 "os2",
12110#endif
12111#ifdef __QNX__
12112 "qnx",
12113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114#ifdef UNIX
12115 "unix",
12116#endif
12117#ifdef VMS
12118 "vms",
12119#endif
12120#ifdef WIN16
12121 "win16",
12122#endif
12123#ifdef WIN32
12124 "win32",
12125#endif
12126#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12127 "win32unix",
12128#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012129#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130 "win64",
12131#endif
12132#ifdef EBCDIC
12133 "ebcdic",
12134#endif
12135#ifndef CASE_INSENSITIVE_FILENAME
12136 "fname_case",
12137#endif
12138#ifdef FEAT_ARABIC
12139 "arabic",
12140#endif
12141#ifdef FEAT_AUTOCMD
12142 "autocmd",
12143#endif
12144#ifdef FEAT_BEVAL
12145 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012146# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12147 "balloon_multiline",
12148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012149#endif
12150#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12151 "builtin_terms",
12152# ifdef ALL_BUILTIN_TCAPS
12153 "all_builtin_terms",
12154# endif
12155#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012156#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12157 || defined(FEAT_GUI_W32) \
12158 || defined(FEAT_GUI_MOTIF))
12159 "browsefilter",
12160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012161#ifdef FEAT_BYTEOFF
12162 "byte_offset",
12163#endif
12164#ifdef FEAT_CINDENT
12165 "cindent",
12166#endif
12167#ifdef FEAT_CLIENTSERVER
12168 "clientserver",
12169#endif
12170#ifdef FEAT_CLIPBOARD
12171 "clipboard",
12172#endif
12173#ifdef FEAT_CMDL_COMPL
12174 "cmdline_compl",
12175#endif
12176#ifdef FEAT_CMDHIST
12177 "cmdline_hist",
12178#endif
12179#ifdef FEAT_COMMENTS
12180 "comments",
12181#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012182#ifdef FEAT_CONCEAL
12183 "conceal",
12184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012185#ifdef FEAT_CRYPT
12186 "cryptv",
12187#endif
12188#ifdef FEAT_CSCOPE
12189 "cscope",
12190#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012191#ifdef FEAT_CURSORBIND
12192 "cursorbind",
12193#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012194#ifdef CURSOR_SHAPE
12195 "cursorshape",
12196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012197#ifdef DEBUG
12198 "debug",
12199#endif
12200#ifdef FEAT_CON_DIALOG
12201 "dialog_con",
12202#endif
12203#ifdef FEAT_GUI_DIALOG
12204 "dialog_gui",
12205#endif
12206#ifdef FEAT_DIFF
12207 "diff",
12208#endif
12209#ifdef FEAT_DIGRAPHS
12210 "digraphs",
12211#endif
12212#ifdef FEAT_DND
12213 "dnd",
12214#endif
12215#ifdef FEAT_EMACS_TAGS
12216 "emacs_tags",
12217#endif
12218 "eval", /* always present, of course! */
12219#ifdef FEAT_EX_EXTRA
12220 "ex_extra",
12221#endif
12222#ifdef FEAT_SEARCH_EXTRA
12223 "extra_search",
12224#endif
12225#ifdef FEAT_FKMAP
12226 "farsi",
12227#endif
12228#ifdef FEAT_SEARCHPATH
12229 "file_in_path",
12230#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012231#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012232 "filterpipe",
12233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234#ifdef FEAT_FIND_ID
12235 "find_in_path",
12236#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012237#ifdef FEAT_FLOAT
12238 "float",
12239#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240#ifdef FEAT_FOLDING
12241 "folding",
12242#endif
12243#ifdef FEAT_FOOTER
12244 "footer",
12245#endif
12246#if !defined(USE_SYSTEM) && defined(UNIX)
12247 "fork",
12248#endif
12249#ifdef FEAT_GETTEXT
12250 "gettext",
12251#endif
12252#ifdef FEAT_GUI
12253 "gui",
12254#endif
12255#ifdef FEAT_GUI_ATHENA
12256# ifdef FEAT_GUI_NEXTAW
12257 "gui_neXtaw",
12258# else
12259 "gui_athena",
12260# endif
12261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012262#ifdef FEAT_GUI_GTK
12263 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012266#ifdef FEAT_GUI_GNOME
12267 "gui_gnome",
12268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269#ifdef FEAT_GUI_MAC
12270 "gui_mac",
12271#endif
12272#ifdef FEAT_GUI_MOTIF
12273 "gui_motif",
12274#endif
12275#ifdef FEAT_GUI_PHOTON
12276 "gui_photon",
12277#endif
12278#ifdef FEAT_GUI_W16
12279 "gui_win16",
12280#endif
12281#ifdef FEAT_GUI_W32
12282 "gui_win32",
12283#endif
12284#ifdef FEAT_HANGULIN
12285 "hangul_input",
12286#endif
12287#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12288 "iconv",
12289#endif
12290#ifdef FEAT_INS_EXPAND
12291 "insert_expand",
12292#endif
12293#ifdef FEAT_JUMPLIST
12294 "jumplist",
12295#endif
12296#ifdef FEAT_KEYMAP
12297 "keymap",
12298#endif
12299#ifdef FEAT_LANGMAP
12300 "langmap",
12301#endif
12302#ifdef FEAT_LIBCALL
12303 "libcall",
12304#endif
12305#ifdef FEAT_LINEBREAK
12306 "linebreak",
12307#endif
12308#ifdef FEAT_LISP
12309 "lispindent",
12310#endif
12311#ifdef FEAT_LISTCMDS
12312 "listcmds",
12313#endif
12314#ifdef FEAT_LOCALMAP
12315 "localmap",
12316#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012317#ifdef FEAT_LUA
12318# ifndef DYNAMIC_LUA
12319 "lua",
12320# endif
12321#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322#ifdef FEAT_MENU
12323 "menu",
12324#endif
12325#ifdef FEAT_SESSION
12326 "mksession",
12327#endif
12328#ifdef FEAT_MODIFY_FNAME
12329 "modify_fname",
12330#endif
12331#ifdef FEAT_MOUSE
12332 "mouse",
12333#endif
12334#ifdef FEAT_MOUSESHAPE
12335 "mouseshape",
12336#endif
12337#if defined(UNIX) || defined(VMS)
12338# ifdef FEAT_MOUSE_DEC
12339 "mouse_dec",
12340# endif
12341# ifdef FEAT_MOUSE_GPM
12342 "mouse_gpm",
12343# endif
12344# ifdef FEAT_MOUSE_JSB
12345 "mouse_jsbterm",
12346# endif
12347# ifdef FEAT_MOUSE_NET
12348 "mouse_netterm",
12349# endif
12350# ifdef FEAT_MOUSE_PTERM
12351 "mouse_pterm",
12352# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012353# ifdef FEAT_MOUSE_SGR
12354 "mouse_sgr",
12355# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012356# ifdef FEAT_SYSMOUSE
12357 "mouse_sysmouse",
12358# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012359# ifdef FEAT_MOUSE_URXVT
12360 "mouse_urxvt",
12361# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362# ifdef FEAT_MOUSE_XTERM
12363 "mouse_xterm",
12364# endif
12365#endif
12366#ifdef FEAT_MBYTE
12367 "multi_byte",
12368#endif
12369#ifdef FEAT_MBYTE_IME
12370 "multi_byte_ime",
12371#endif
12372#ifdef FEAT_MULTI_LANG
12373 "multi_lang",
12374#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012375#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012376#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012377 "mzscheme",
12378#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012379#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012380#ifdef FEAT_OLE
12381 "ole",
12382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383#ifdef FEAT_PATH_EXTRA
12384 "path_extra",
12385#endif
12386#ifdef FEAT_PERL
12387#ifndef DYNAMIC_PERL
12388 "perl",
12389#endif
12390#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012391#ifdef FEAT_PERSISTENT_UNDO
12392 "persistent_undo",
12393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012394#ifdef FEAT_PYTHON
12395#ifndef DYNAMIC_PYTHON
12396 "python",
12397#endif
12398#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012399#ifdef FEAT_PYTHON3
12400#ifndef DYNAMIC_PYTHON3
12401 "python3",
12402#endif
12403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404#ifdef FEAT_POSTSCRIPT
12405 "postscript",
12406#endif
12407#ifdef FEAT_PRINTER
12408 "printer",
12409#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012410#ifdef FEAT_PROFILE
12411 "profile",
12412#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012413#ifdef FEAT_RELTIME
12414 "reltime",
12415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416#ifdef FEAT_QUICKFIX
12417 "quickfix",
12418#endif
12419#ifdef FEAT_RIGHTLEFT
12420 "rightleft",
12421#endif
12422#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12423 "ruby",
12424#endif
12425#ifdef FEAT_SCROLLBIND
12426 "scrollbind",
12427#endif
12428#ifdef FEAT_CMDL_INFO
12429 "showcmd",
12430 "cmdline_info",
12431#endif
12432#ifdef FEAT_SIGNS
12433 "signs",
12434#endif
12435#ifdef FEAT_SMARTINDENT
12436 "smartindent",
12437#endif
12438#ifdef FEAT_SNIFF
12439 "sniff",
12440#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012441#ifdef STARTUPTIME
12442 "startuptime",
12443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012444#ifdef FEAT_STL_OPT
12445 "statusline",
12446#endif
12447#ifdef FEAT_SUN_WORKSHOP
12448 "sun_workshop",
12449#endif
12450#ifdef FEAT_NETBEANS_INTG
12451 "netbeans_intg",
12452#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012453#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012454 "spell",
12455#endif
12456#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012457 "syntax",
12458#endif
12459#if defined(USE_SYSTEM) || !defined(UNIX)
12460 "system",
12461#endif
12462#ifdef FEAT_TAG_BINS
12463 "tag_binary",
12464#endif
12465#ifdef FEAT_TAG_OLDSTATIC
12466 "tag_old_static",
12467#endif
12468#ifdef FEAT_TAG_ANYWHITE
12469 "tag_any_white",
12470#endif
12471#ifdef FEAT_TCL
12472# ifndef DYNAMIC_TCL
12473 "tcl",
12474# endif
12475#endif
12476#ifdef TERMINFO
12477 "terminfo",
12478#endif
12479#ifdef FEAT_TERMRESPONSE
12480 "termresponse",
12481#endif
12482#ifdef FEAT_TEXTOBJ
12483 "textobjects",
12484#endif
12485#ifdef HAVE_TGETENT
12486 "tgetent",
12487#endif
12488#ifdef FEAT_TITLE
12489 "title",
12490#endif
12491#ifdef FEAT_TOOLBAR
12492 "toolbar",
12493#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012494#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12495 "unnamedplus",
12496#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012497#ifdef FEAT_USR_CMDS
12498 "user-commands", /* was accidentally included in 5.4 */
12499 "user_commands",
12500#endif
12501#ifdef FEAT_VIMINFO
12502 "viminfo",
12503#endif
12504#ifdef FEAT_VERTSPLIT
12505 "vertsplit",
12506#endif
12507#ifdef FEAT_VIRTUALEDIT
12508 "virtualedit",
12509#endif
12510#ifdef FEAT_VISUAL
12511 "visual",
12512#endif
12513#ifdef FEAT_VISUALEXTRA
12514 "visualextra",
12515#endif
12516#ifdef FEAT_VREPLACE
12517 "vreplace",
12518#endif
12519#ifdef FEAT_WILDIGN
12520 "wildignore",
12521#endif
12522#ifdef FEAT_WILDMENU
12523 "wildmenu",
12524#endif
12525#ifdef FEAT_WINDOWS
12526 "windows",
12527#endif
12528#ifdef FEAT_WAK
12529 "winaltkeys",
12530#endif
12531#ifdef FEAT_WRITEBACKUP
12532 "writebackup",
12533#endif
12534#ifdef FEAT_XIM
12535 "xim",
12536#endif
12537#ifdef FEAT_XFONTSET
12538 "xfontset",
12539#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012540#ifdef FEAT_XPM_W32
12541 "xpm_w32",
12542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543#ifdef USE_XSMP
12544 "xsmp",
12545#endif
12546#ifdef USE_XSMP_INTERACT
12547 "xsmp_interact",
12548#endif
12549#ifdef FEAT_XCLIPBOARD
12550 "xterm_clipboard",
12551#endif
12552#ifdef FEAT_XTERM_SAVE
12553 "xterm_save",
12554#endif
12555#if defined(UNIX) && defined(FEAT_X11)
12556 "X11",
12557#endif
12558 NULL
12559 };
12560
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012561 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012562 for (i = 0; has_list[i] != NULL; ++i)
12563 if (STRICMP(name, has_list[i]) == 0)
12564 {
12565 n = TRUE;
12566 break;
12567 }
12568
12569 if (n == FALSE)
12570 {
12571 if (STRNICMP(name, "patch", 5) == 0)
12572 n = has_patch(atoi((char *)name + 5));
12573 else if (STRICMP(name, "vim_starting") == 0)
12574 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012575#ifdef FEAT_MBYTE
12576 else if (STRICMP(name, "multi_byte_encoding") == 0)
12577 n = has_mbyte;
12578#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012579#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12580 else if (STRICMP(name, "balloon_multiline") == 0)
12581 n = multiline_balloon_available();
12582#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583#ifdef DYNAMIC_TCL
12584 else if (STRICMP(name, "tcl") == 0)
12585 n = tcl_enabled(FALSE);
12586#endif
12587#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12588 else if (STRICMP(name, "iconv") == 0)
12589 n = iconv_enabled(FALSE);
12590#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012591#ifdef DYNAMIC_LUA
12592 else if (STRICMP(name, "lua") == 0)
12593 n = lua_enabled(FALSE);
12594#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012595#ifdef DYNAMIC_MZSCHEME
12596 else if (STRICMP(name, "mzscheme") == 0)
12597 n = mzscheme_enabled(FALSE);
12598#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599#ifdef DYNAMIC_RUBY
12600 else if (STRICMP(name, "ruby") == 0)
12601 n = ruby_enabled(FALSE);
12602#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012603#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012604#ifdef DYNAMIC_PYTHON
12605 else if (STRICMP(name, "python") == 0)
12606 n = python_enabled(FALSE);
12607#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012608#endif
12609#ifdef FEAT_PYTHON3
12610#ifdef DYNAMIC_PYTHON3
12611 else if (STRICMP(name, "python3") == 0)
12612 n = python3_enabled(FALSE);
12613#endif
12614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615#ifdef DYNAMIC_PERL
12616 else if (STRICMP(name, "perl") == 0)
12617 n = perl_enabled(FALSE);
12618#endif
12619#ifdef FEAT_GUI
12620 else if (STRICMP(name, "gui_running") == 0)
12621 n = (gui.in_use || gui.starting);
12622# ifdef FEAT_GUI_W32
12623 else if (STRICMP(name, "gui_win32s") == 0)
12624 n = gui_is_win32s();
12625# endif
12626# ifdef FEAT_BROWSE
12627 else if (STRICMP(name, "browse") == 0)
12628 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12629# endif
12630#endif
12631#ifdef FEAT_SYN_HL
12632 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012633 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634#endif
12635#if defined(WIN3264)
12636 else if (STRICMP(name, "win95") == 0)
12637 n = mch_windows95();
12638#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012639#ifdef FEAT_NETBEANS_INTG
12640 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012641 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012642#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643 }
12644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012645 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012646}
12647
12648/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012649 * "has_key()" function
12650 */
12651 static void
12652f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012653 typval_T *argvars;
12654 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012655{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012656 if (argvars[0].v_type != VAR_DICT)
12657 {
12658 EMSG(_(e_dictreq));
12659 return;
12660 }
12661 if (argvars[0].vval.v_dict == NULL)
12662 return;
12663
12664 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012665 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012666}
12667
12668/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012669 * "haslocaldir()" function
12670 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012671 static void
12672f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012673 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012674 typval_T *rettv;
12675{
12676 rettv->vval.v_number = (curwin->w_localdir != NULL);
12677}
12678
12679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012680 * "hasmapto()" function
12681 */
12682 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012683f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012684 typval_T *argvars;
12685 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012686{
12687 char_u *name;
12688 char_u *mode;
12689 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012690 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012691
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012693 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694 mode = (char_u *)"nvo";
12695 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012697 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012698 if (argvars[2].v_type != VAR_UNKNOWN)
12699 abbr = get_tv_number(&argvars[2]);
12700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012701
Bram Moolenaar2c932302006-03-18 21:42:09 +000012702 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012703 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012705 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706}
12707
12708/*
12709 * "histadd()" function
12710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012713 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012714 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715{
12716#ifdef FEAT_CMDHIST
12717 int histype;
12718 char_u *str;
12719 char_u buf[NUMBUFLEN];
12720#endif
12721
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012722 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723 if (check_restricted() || check_secure())
12724 return;
12725#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012726 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12727 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012728 if (histype >= 0)
12729 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731 if (*str != NUL)
12732 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012733 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012735 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 return;
12737 }
12738 }
12739#endif
12740}
12741
12742/*
12743 * "histdel()" function
12744 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012746f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012747 typval_T *argvars UNUSED;
12748 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012749{
12750#ifdef FEAT_CMDHIST
12751 int n;
12752 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012753 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012755 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12756 if (str == NULL)
12757 n = 0;
12758 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012759 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012760 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012761 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012763 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012764 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012765 else
12766 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012767 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012768 get_tv_string_buf(&argvars[1], buf));
12769 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770#endif
12771}
12772
12773/*
12774 * "histget()" function
12775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012776 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012777f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012778 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012779 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012780{
12781#ifdef FEAT_CMDHIST
12782 int type;
12783 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012784 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012786 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12787 if (str == NULL)
12788 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012790 {
12791 type = get_histtype(str);
12792 if (argvars[1].v_type == VAR_UNKNOWN)
12793 idx = get_history_idx(type);
12794 else
12795 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12796 /* -1 on type error */
12797 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012800 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012801#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012802 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012803}
12804
12805/*
12806 * "histnr()" function
12807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012810 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812{
12813 int i;
12814
12815#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012816 char_u *history = get_tv_string_chk(&argvars[0]);
12817
12818 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012819 if (i >= HIST_CMD && i < HIST_COUNT)
12820 i = get_history_idx(i);
12821 else
12822#endif
12823 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012824 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012825}
12826
12827/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 * "highlightID(name)" function
12829 */
12830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012831f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012832 typval_T *argvars;
12833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012835 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836}
12837
12838/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012839 * "highlight_exists()" function
12840 */
12841 static void
12842f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012843 typval_T *argvars;
12844 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012845{
12846 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12847}
12848
12849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012850 * "hostname()" function
12851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012853f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012854 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012856{
12857 char_u hostname[256];
12858
12859 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012860 rettv->v_type = VAR_STRING;
12861 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012862}
12863
12864/*
12865 * iconv() function
12866 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012869 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012870 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871{
12872#ifdef FEAT_MBYTE
12873 char_u buf1[NUMBUFLEN];
12874 char_u buf2[NUMBUFLEN];
12875 char_u *from, *to, *str;
12876 vimconv_T vimconv;
12877#endif
12878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012879 rettv->v_type = VAR_STRING;
12880 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012881
12882#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012883 str = get_tv_string(&argvars[0]);
12884 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12885 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012886 vimconv.vc_type = CONV_NONE;
12887 convert_setup(&vimconv, from, to);
12888
12889 /* If the encodings are equal, no conversion needed. */
12890 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012891 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012893 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894
12895 convert_setup(&vimconv, NULL, NULL);
12896 vim_free(from);
12897 vim_free(to);
12898#endif
12899}
12900
12901/*
12902 * "indent()" function
12903 */
12904 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012905f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012906 typval_T *argvars;
12907 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012908{
12909 linenr_T lnum;
12910
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012911 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012912 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012913 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916}
12917
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012918/*
12919 * "index()" function
12920 */
12921 static void
12922f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012923 typval_T *argvars;
12924 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012925{
Bram Moolenaar33570922005-01-25 22:26:29 +000012926 list_T *l;
12927 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012928 long idx = 0;
12929 int ic = FALSE;
12930
12931 rettv->vval.v_number = -1;
12932 if (argvars[0].v_type != VAR_LIST)
12933 {
12934 EMSG(_(e_listreq));
12935 return;
12936 }
12937 l = argvars[0].vval.v_list;
12938 if (l != NULL)
12939 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012940 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012941 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012942 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012943 int error = FALSE;
12944
Bram Moolenaar758711c2005-02-02 23:11:38 +000012945 /* Start at specified item. Use the cached index that list_find()
12946 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012947 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012948 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012949 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012950 ic = get_tv_number_chk(&argvars[3], &error);
12951 if (error)
12952 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012953 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012954
Bram Moolenaar758711c2005-02-02 23:11:38 +000012955 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012956 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012957 {
12958 rettv->vval.v_number = idx;
12959 break;
12960 }
12961 }
12962}
12963
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964static int inputsecret_flag = 0;
12965
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012966static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12967
Bram Moolenaar071d4272004-06-13 20:20:40 +000012968/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012969 * This function is used by f_input() and f_inputdialog() functions. The third
12970 * argument to f_input() specifies the type of completion to use at the
12971 * prompt. The third argument to f_inputdialog() specifies the value to return
12972 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973 */
12974 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012975get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012976 typval_T *argvars;
12977 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012978 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012980 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 char_u *p = NULL;
12982 int c;
12983 char_u buf[NUMBUFLEN];
12984 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012985 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012986 int xp_type = EXPAND_NOTHING;
12987 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012989 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012990 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012991
12992#ifdef NO_CONSOLE_INPUT
12993 /* While starting up, there is no place to enter text. */
12994 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000012995 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996#endif
12997
12998 cmd_silent = FALSE; /* Want to see the prompt. */
12999 if (prompt != NULL)
13000 {
13001 /* Only the part of the message after the last NL is considered as
13002 * prompt for the command line */
13003 p = vim_strrchr(prompt, '\n');
13004 if (p == NULL)
13005 p = prompt;
13006 else
13007 {
13008 ++p;
13009 c = *p;
13010 *p = NUL;
13011 msg_start();
13012 msg_clr_eos();
13013 msg_puts_attr(prompt, echo_attr);
13014 msg_didout = FALSE;
13015 msg_starthere();
13016 *p = c;
13017 }
13018 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013020 if (argvars[1].v_type != VAR_UNKNOWN)
13021 {
13022 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13023 if (defstr != NULL)
13024 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013025
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013026 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013027 {
13028 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013029 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013030 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013031
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013032 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013033 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013034
Bram Moolenaar4463f292005-09-25 22:20:24 +000013035 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13036 if (xp_name == NULL)
13037 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013038
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013039 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013040
Bram Moolenaar4463f292005-09-25 22:20:24 +000013041 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13042 &xp_arg) == FAIL)
13043 return;
13044 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013045 }
13046
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013047 if (defstr != NULL)
13048 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013049 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13050 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013051 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013052 && argvars[1].v_type != VAR_UNKNOWN
13053 && argvars[2].v_type != VAR_UNKNOWN)
13054 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13055 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013056
13057 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013058
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013059 /* since the user typed this, no need to wait for return */
13060 need_wait_return = FALSE;
13061 msg_didout = FALSE;
13062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013063 cmd_silent = cmd_silent_save;
13064}
13065
13066/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013067 * "input()" function
13068 * Also handles inputsecret() when inputsecret is set.
13069 */
13070 static void
13071f_input(argvars, rettv)
13072 typval_T *argvars;
13073 typval_T *rettv;
13074{
13075 get_user_input(argvars, rettv, FALSE);
13076}
13077
13078/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013079 * "inputdialog()" function
13080 */
13081 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013082f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013083 typval_T *argvars;
13084 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085{
13086#if defined(FEAT_GUI_TEXTDIALOG)
13087 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13088 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13089 {
13090 char_u *message;
13091 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013092 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013094 message = get_tv_string_chk(&argvars[0]);
13095 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013096 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013097 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013098 else
13099 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013100 if (message != NULL && defstr != NULL
13101 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013102 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013103 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104 else
13105 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013106 if (message != NULL && defstr != NULL
13107 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013108 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013109 rettv->vval.v_string = vim_strsave(
13110 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013111 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013112 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013114 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 }
13116 else
13117#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013118 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119}
13120
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013121/*
13122 * "inputlist()" function
13123 */
13124 static void
13125f_inputlist(argvars, rettv)
13126 typval_T *argvars;
13127 typval_T *rettv;
13128{
13129 listitem_T *li;
13130 int selected;
13131 int mouse_used;
13132
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013133#ifdef NO_CONSOLE_INPUT
13134 /* While starting up, there is no place to enter text. */
13135 if (no_console_input())
13136 return;
13137#endif
13138 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13139 {
13140 EMSG2(_(e_listarg), "inputlist()");
13141 return;
13142 }
13143
13144 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013145 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013146 lines_left = Rows; /* avoid more prompt */
13147 msg_scroll = TRUE;
13148 msg_clr_eos();
13149
13150 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13151 {
13152 msg_puts(get_tv_string(&li->li_tv));
13153 msg_putchar('\n');
13154 }
13155
13156 /* Ask for choice. */
13157 selected = prompt_for_number(&mouse_used);
13158 if (mouse_used)
13159 selected -= lines_left;
13160
13161 rettv->vval.v_number = selected;
13162}
13163
13164
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13166
13167/*
13168 * "inputrestore()" function
13169 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013170 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013171f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013172 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013173 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174{
13175 if (ga_userinput.ga_len > 0)
13176 {
13177 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13179 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013180 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013181 }
13182 else if (p_verbose > 1)
13183 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013184 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013185 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 }
13187}
13188
13189/*
13190 * "inputsave()" function
13191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013193f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013194 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013195 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013197 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198 if (ga_grow(&ga_userinput, 1) == OK)
13199 {
13200 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13201 + ga_userinput.ga_len);
13202 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013203 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 }
13205 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013206 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207}
13208
13209/*
13210 * "inputsecret()" function
13211 */
13212 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013213f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013214 typval_T *argvars;
13215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216{
13217 ++cmdline_star;
13218 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013219 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220 --cmdline_star;
13221 --inputsecret_flag;
13222}
13223
13224/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013225 * "insert()" function
13226 */
13227 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013229 typval_T *argvars;
13230 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013231{
13232 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013233 listitem_T *item;
13234 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013235 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013236
13237 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013238 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013239 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013240 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013241 {
13242 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013243 before = get_tv_number_chk(&argvars[2], &error);
13244 if (error)
13245 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013246
Bram Moolenaar758711c2005-02-02 23:11:38 +000013247 if (before == l->lv_len)
13248 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013249 else
13250 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013251 item = list_find(l, before);
13252 if (item == NULL)
13253 {
13254 EMSGN(_(e_listidx), before);
13255 l = NULL;
13256 }
13257 }
13258 if (l != NULL)
13259 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013260 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013261 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013262 }
13263 }
13264}
13265
13266/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013267 * "invert(expr)" function
13268 */
13269 static void
13270f_invert(argvars, rettv)
13271 typval_T *argvars;
13272 typval_T *rettv;
13273{
13274 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13275}
13276
13277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 * "isdirectory()" function
13279 */
13280 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013281f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013282 typval_T *argvars;
13283 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013285 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286}
13287
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013288/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013289 * "islocked()" function
13290 */
13291 static void
13292f_islocked(argvars, rettv)
13293 typval_T *argvars;
13294 typval_T *rettv;
13295{
13296 lval_T lv;
13297 char_u *end;
13298 dictitem_T *di;
13299
13300 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013301 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13302 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013303 if (end != NULL && lv.ll_name != NULL)
13304 {
13305 if (*end != NUL)
13306 EMSG(_(e_trailing));
13307 else
13308 {
13309 if (lv.ll_tv == NULL)
13310 {
13311 if (check_changedtick(lv.ll_name))
13312 rettv->vval.v_number = 1; /* always locked */
13313 else
13314 {
13315 di = find_var(lv.ll_name, NULL);
13316 if (di != NULL)
13317 {
13318 /* Consider a variable locked when:
13319 * 1. the variable itself is locked
13320 * 2. the value of the variable is locked.
13321 * 3. the List or Dict value is locked.
13322 */
13323 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13324 || tv_islocked(&di->di_tv));
13325 }
13326 }
13327 }
13328 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013329 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013330 else if (lv.ll_newkey != NULL)
13331 EMSG2(_(e_dictkey), lv.ll_newkey);
13332 else if (lv.ll_list != NULL)
13333 /* List item. */
13334 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13335 else
13336 /* Dictionary item. */
13337 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13338 }
13339 }
13340
13341 clear_lval(&lv);
13342}
13343
Bram Moolenaar33570922005-01-25 22:26:29 +000013344static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013345
13346/*
13347 * Turn a dict into a list:
13348 * "what" == 0: list of keys
13349 * "what" == 1: list of values
13350 * "what" == 2: list of items
13351 */
13352 static void
13353dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013354 typval_T *argvars;
13355 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013356 int what;
13357{
Bram Moolenaar33570922005-01-25 22:26:29 +000013358 list_T *l2;
13359 dictitem_T *di;
13360 hashitem_T *hi;
13361 listitem_T *li;
13362 listitem_T *li2;
13363 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013364 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013365
Bram Moolenaar8c711452005-01-14 21:53:12 +000013366 if (argvars[0].v_type != VAR_DICT)
13367 {
13368 EMSG(_(e_dictreq));
13369 return;
13370 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013371 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013372 return;
13373
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013374 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013375 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013376
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013377 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013378 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013379 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013380 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013381 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013382 --todo;
13383 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013384
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013385 li = listitem_alloc();
13386 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013387 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013388 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013389
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013390 if (what == 0)
13391 {
13392 /* keys() */
13393 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013394 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013395 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13396 }
13397 else if (what == 1)
13398 {
13399 /* values() */
13400 copy_tv(&di->di_tv, &li->li_tv);
13401 }
13402 else
13403 {
13404 /* items() */
13405 l2 = list_alloc();
13406 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013407 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013408 li->li_tv.vval.v_list = l2;
13409 if (l2 == NULL)
13410 break;
13411 ++l2->lv_refcount;
13412
13413 li2 = listitem_alloc();
13414 if (li2 == NULL)
13415 break;
13416 list_append(l2, li2);
13417 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013418 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013419 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13420
13421 li2 = listitem_alloc();
13422 if (li2 == NULL)
13423 break;
13424 list_append(l2, li2);
13425 copy_tv(&di->di_tv, &li2->li_tv);
13426 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013427 }
13428 }
13429}
13430
13431/*
13432 * "items(dict)" function
13433 */
13434 static void
13435f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013436 typval_T *argvars;
13437 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013438{
13439 dict_list(argvars, rettv, 2);
13440}
13441
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013443 * "join()" function
13444 */
13445 static void
13446f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013447 typval_T *argvars;
13448 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013449{
13450 garray_T ga;
13451 char_u *sep;
13452
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013453 if (argvars[0].v_type != VAR_LIST)
13454 {
13455 EMSG(_(e_listreq));
13456 return;
13457 }
13458 if (argvars[0].vval.v_list == NULL)
13459 return;
13460 if (argvars[1].v_type == VAR_UNKNOWN)
13461 sep = (char_u *)" ";
13462 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013463 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013464
13465 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013466
13467 if (sep != NULL)
13468 {
13469 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013470 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013471 ga_append(&ga, NUL);
13472 rettv->vval.v_string = (char_u *)ga.ga_data;
13473 }
13474 else
13475 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013476}
13477
13478/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013479 * "keys()" function
13480 */
13481 static void
13482f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013483 typval_T *argvars;
13484 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013485{
13486 dict_list(argvars, rettv, 0);
13487}
13488
13489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013490 * "last_buffer_nr()" function.
13491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013492 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013493f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013494 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013495 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496{
13497 int n = 0;
13498 buf_T *buf;
13499
13500 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13501 if (n < buf->b_fnum)
13502 n = buf->b_fnum;
13503
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013504 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013505}
13506
13507/*
13508 * "len()" function
13509 */
13510 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013511f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013512 typval_T *argvars;
13513 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013514{
13515 switch (argvars[0].v_type)
13516 {
13517 case VAR_STRING:
13518 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519 rettv->vval.v_number = (varnumber_T)STRLEN(
13520 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013521 break;
13522 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013523 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013524 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013525 case VAR_DICT:
13526 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13527 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013528 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013529 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013530 break;
13531 }
13532}
13533
Bram Moolenaar33570922005-01-25 22:26:29 +000013534static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013535
13536 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013537libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013538 typval_T *argvars;
13539 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013540 int type;
13541{
13542#ifdef FEAT_LIBCALL
13543 char_u *string_in;
13544 char_u **string_result;
13545 int nr_result;
13546#endif
13547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013548 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013549 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013550 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013551
13552 if (check_restricted() || check_secure())
13553 return;
13554
13555#ifdef FEAT_LIBCALL
13556 /* The first two args must be strings, otherwise its meaningless */
13557 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13558 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013559 string_in = NULL;
13560 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013561 string_in = argvars[2].vval.v_string;
13562 if (type == VAR_NUMBER)
13563 string_result = NULL;
13564 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013565 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013566 if (mch_libcall(argvars[0].vval.v_string,
13567 argvars[1].vval.v_string,
13568 string_in,
13569 argvars[2].vval.v_number,
13570 string_result,
13571 &nr_result) == OK
13572 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013574 }
13575#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576}
13577
13578/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013579 * "libcall()" function
13580 */
13581 static void
13582f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013583 typval_T *argvars;
13584 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013585{
13586 libcall_common(argvars, rettv, VAR_STRING);
13587}
13588
13589/*
13590 * "libcallnr()" function
13591 */
13592 static void
13593f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013594 typval_T *argvars;
13595 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013596{
13597 libcall_common(argvars, rettv, VAR_NUMBER);
13598}
13599
13600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013601 * "line(string)" function
13602 */
13603 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013604f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013605 typval_T *argvars;
13606 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607{
13608 linenr_T lnum = 0;
13609 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013610 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013612 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013613 if (fp != NULL)
13614 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013615 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616}
13617
13618/*
13619 * "line2byte(lnum)" function
13620 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013622f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013623 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013624 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625{
13626#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013627 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628#else
13629 linenr_T lnum;
13630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013632 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013633 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013635 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13636 if (rettv->vval.v_number >= 0)
13637 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013638#endif
13639}
13640
13641/*
13642 * "lispindent(lnum)" function
13643 */
13644 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013645f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013646 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013647 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013648{
13649#ifdef FEAT_LISP
13650 pos_T pos;
13651 linenr_T lnum;
13652
13653 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013654 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013655 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13656 {
13657 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013658 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013659 curwin->w_cursor = pos;
13660 }
13661 else
13662#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013663 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664}
13665
13666/*
13667 * "localtime()" function
13668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013670f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013671 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013672 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013674 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675}
13676
Bram Moolenaar33570922005-01-25 22:26:29 +000013677static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678
13679 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013680get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013681 typval_T *argvars;
13682 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683 int exact;
13684{
13685 char_u *keys;
13686 char_u *which;
13687 char_u buf[NUMBUFLEN];
13688 char_u *keys_buf = NULL;
13689 char_u *rhs;
13690 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013691 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013692 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013693 mapblock_T *mp;
13694 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695
13696 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013697 rettv->v_type = VAR_STRING;
13698 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013700 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701 if (*keys == NUL)
13702 return;
13703
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013704 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013705 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013706 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013707 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013708 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013709 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013710 if (argvars[3].v_type != VAR_UNKNOWN)
13711 get_dict = get_tv_number(&argvars[3]);
13712 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013713 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013714 else
13715 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013716 if (which == NULL)
13717 return;
13718
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719 mode = get_map_mode(&which, 0);
13720
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013721 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013722 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013724
13725 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013726 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013727 /* Return a string. */
13728 if (rhs != NULL)
13729 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730
Bram Moolenaarbd743252010-10-20 21:23:33 +020013731 }
13732 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13733 {
13734 /* Return a dictionary. */
13735 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13736 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13737 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738
Bram Moolenaarbd743252010-10-20 21:23:33 +020013739 dict_add_nr_str(dict, "lhs", 0L, lhs);
13740 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13741 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13742 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13743 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13744 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13745 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013746 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013747 dict_add_nr_str(dict, "mode", 0L, mapmode);
13748
13749 vim_free(lhs);
13750 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013751 }
13752}
13753
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013754#ifdef FEAT_FLOAT
13755/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013756 * "log()" function
13757 */
13758 static void
13759f_log(argvars, rettv)
13760 typval_T *argvars;
13761 typval_T *rettv;
13762{
13763 float_T f;
13764
13765 rettv->v_type = VAR_FLOAT;
13766 if (get_float_arg(argvars, &f) == OK)
13767 rettv->vval.v_float = log(f);
13768 else
13769 rettv->vval.v_float = 0.0;
13770}
13771
13772/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013773 * "log10()" function
13774 */
13775 static void
13776f_log10(argvars, rettv)
13777 typval_T *argvars;
13778 typval_T *rettv;
13779{
13780 float_T f;
13781
13782 rettv->v_type = VAR_FLOAT;
13783 if (get_float_arg(argvars, &f) == OK)
13784 rettv->vval.v_float = log10(f);
13785 else
13786 rettv->vval.v_float = 0.0;
13787}
13788#endif
13789
Bram Moolenaar1dced572012-04-05 16:54:08 +020013790#ifdef FEAT_LUA
13791/*
13792 * "luaeval()" function
13793 */
13794 static void
13795f_luaeval(argvars, rettv)
13796 typval_T *argvars;
13797 typval_T *rettv;
13798{
13799 char_u *str;
13800 char_u buf[NUMBUFLEN];
13801
13802 str = get_tv_string_buf(&argvars[0], buf);
13803 do_luaeval(str, argvars + 1, rettv);
13804}
13805#endif
13806
Bram Moolenaar071d4272004-06-13 20:20:40 +000013807/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013808 * "map()" function
13809 */
13810 static void
13811f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013812 typval_T *argvars;
13813 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013814{
13815 filter_map(argvars, rettv, TRUE);
13816}
13817
13818/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013819 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013820 */
13821 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013822f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013823 typval_T *argvars;
13824 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013826 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827}
13828
13829/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013830 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831 */
13832 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013833f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013834 typval_T *argvars;
13835 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013837 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013838}
13839
Bram Moolenaar33570922005-01-25 22:26:29 +000013840static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841
13842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013843find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013844 typval_T *argvars;
13845 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846 int type;
13847{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013848 char_u *str = NULL;
13849 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850 char_u *pat;
13851 regmatch_T regmatch;
13852 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013853 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854 char_u *save_cpo;
13855 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013856 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013857 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013858 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013859 list_T *l = NULL;
13860 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013861 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013862 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863
13864 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13865 save_cpo = p_cpo;
13866 p_cpo = (char_u *)"";
13867
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013868 rettv->vval.v_number = -1;
13869 if (type == 3)
13870 {
13871 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013872 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013873 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013874 }
13875 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013876 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013877 rettv->v_type = VAR_STRING;
13878 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013880
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013881 if (argvars[0].v_type == VAR_LIST)
13882 {
13883 if ((l = argvars[0].vval.v_list) == NULL)
13884 goto theend;
13885 li = l->lv_first;
13886 }
13887 else
13888 expr = str = get_tv_string(&argvars[0]);
13889
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013890 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13891 if (pat == NULL)
13892 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013893
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013894 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013895 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013896 int error = FALSE;
13897
13898 start = get_tv_number_chk(&argvars[2], &error);
13899 if (error)
13900 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013901 if (l != NULL)
13902 {
13903 li = list_find(l, start);
13904 if (li == NULL)
13905 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013906 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013907 }
13908 else
13909 {
13910 if (start < 0)
13911 start = 0;
13912 if (start > (long)STRLEN(str))
13913 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013914 /* When "count" argument is there ignore matches before "start",
13915 * otherwise skip part of the string. Differs when pattern is "^"
13916 * or "\<". */
13917 if (argvars[3].v_type != VAR_UNKNOWN)
13918 startcol = start;
13919 else
13920 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013921 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013922
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013923 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013924 nth = get_tv_number_chk(&argvars[3], &error);
13925 if (error)
13926 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013927 }
13928
13929 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13930 if (regmatch.regprog != NULL)
13931 {
13932 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013933
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013934 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013935 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013936 if (l != NULL)
13937 {
13938 if (li == NULL)
13939 {
13940 match = FALSE;
13941 break;
13942 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013943 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013944 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013945 if (str == NULL)
13946 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013947 }
13948
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013949 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013950
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013951 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013952 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013953 if (l == NULL && !match)
13954 break;
13955
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013956 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013957 if (l != NULL)
13958 {
13959 li = li->li_next;
13960 ++idx;
13961 }
13962 else
13963 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013964#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013965 startcol = (colnr_T)(regmatch.startp[0]
13966 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013967#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013968 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013969#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013970 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013971 }
13972
13973 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013975 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013976 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013977 int i;
13978
13979 /* return list with matched string and submatches */
13980 for (i = 0; i < NSUBEXP; ++i)
13981 {
13982 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013983 {
13984 if (list_append_string(rettv->vval.v_list,
13985 (char_u *)"", 0) == FAIL)
13986 break;
13987 }
13988 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013989 regmatch.startp[i],
13990 (int)(regmatch.endp[i] - regmatch.startp[i]))
13991 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013992 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013993 }
13994 }
13995 else if (type == 2)
13996 {
13997 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013998 if (l != NULL)
13999 copy_tv(&li->li_tv, rettv);
14000 else
14001 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014003 }
14004 else if (l != NULL)
14005 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006 else
14007 {
14008 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014009 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010 (varnumber_T)(regmatch.startp[0] - str);
14011 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014012 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014013 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014014 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 }
14016 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014017 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 }
14019
14020theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014021 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 p_cpo = save_cpo;
14023}
14024
14025/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014026 * "match()" function
14027 */
14028 static void
14029f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014030 typval_T *argvars;
14031 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014032{
14033 find_some_match(argvars, rettv, 1);
14034}
14035
14036/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014037 * "matchadd()" function
14038 */
14039 static void
14040f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014041 typval_T *argvars UNUSED;
14042 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014043{
14044#ifdef FEAT_SEARCH_EXTRA
14045 char_u buf[NUMBUFLEN];
14046 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14047 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14048 int prio = 10; /* default priority */
14049 int id = -1;
14050 int error = FALSE;
14051
14052 rettv->vval.v_number = -1;
14053
14054 if (grp == NULL || pat == NULL)
14055 return;
14056 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014057 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014058 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014059 if (argvars[3].v_type != VAR_UNKNOWN)
14060 id = get_tv_number_chk(&argvars[3], &error);
14061 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014062 if (error == TRUE)
14063 return;
14064 if (id >= 1 && id <= 3)
14065 {
14066 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14067 return;
14068 }
14069
14070 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14071#endif
14072}
14073
14074/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014075 * "matcharg()" function
14076 */
14077 static void
14078f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014079 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014080 typval_T *rettv;
14081{
14082 if (rettv_list_alloc(rettv) == OK)
14083 {
14084#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014085 int id = get_tv_number(&argvars[0]);
14086 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014087
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014088 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014089 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014090 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14091 {
14092 list_append_string(rettv->vval.v_list,
14093 syn_id2name(m->hlg_id), -1);
14094 list_append_string(rettv->vval.v_list, m->pattern, -1);
14095 }
14096 else
14097 {
14098 list_append_string(rettv->vval.v_list, NUL, -1);
14099 list_append_string(rettv->vval.v_list, NUL, -1);
14100 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014101 }
14102#endif
14103 }
14104}
14105
14106/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014107 * "matchdelete()" function
14108 */
14109 static void
14110f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014111 typval_T *argvars UNUSED;
14112 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014113{
14114#ifdef FEAT_SEARCH_EXTRA
14115 rettv->vval.v_number = match_delete(curwin,
14116 (int)get_tv_number(&argvars[0]), TRUE);
14117#endif
14118}
14119
14120/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014121 * "matchend()" function
14122 */
14123 static void
14124f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014125 typval_T *argvars;
14126 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014127{
14128 find_some_match(argvars, rettv, 0);
14129}
14130
14131/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014132 * "matchlist()" function
14133 */
14134 static void
14135f_matchlist(argvars, rettv)
14136 typval_T *argvars;
14137 typval_T *rettv;
14138{
14139 find_some_match(argvars, rettv, 3);
14140}
14141
14142/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014143 * "matchstr()" function
14144 */
14145 static void
14146f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014147 typval_T *argvars;
14148 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014149{
14150 find_some_match(argvars, rettv, 2);
14151}
14152
Bram Moolenaar33570922005-01-25 22:26:29 +000014153static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014154
14155 static void
14156max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014157 typval_T *argvars;
14158 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014159 int domax;
14160{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014161 long n = 0;
14162 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014163 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014164
14165 if (argvars[0].v_type == VAR_LIST)
14166 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014167 list_T *l;
14168 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014169
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014170 l = argvars[0].vval.v_list;
14171 if (l != NULL)
14172 {
14173 li = l->lv_first;
14174 if (li != NULL)
14175 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014176 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014177 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014178 {
14179 li = li->li_next;
14180 if (li == NULL)
14181 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014182 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014183 if (domax ? i > n : i < n)
14184 n = i;
14185 }
14186 }
14187 }
14188 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014189 else if (argvars[0].v_type == VAR_DICT)
14190 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014191 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014192 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014193 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014194 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014195
14196 d = argvars[0].vval.v_dict;
14197 if (d != NULL)
14198 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014199 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014200 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014201 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014202 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014203 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014204 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014205 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014206 if (first)
14207 {
14208 n = i;
14209 first = FALSE;
14210 }
14211 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014212 n = i;
14213 }
14214 }
14215 }
14216 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014217 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014218 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014219 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014220}
14221
14222/*
14223 * "max()" function
14224 */
14225 static void
14226f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014227 typval_T *argvars;
14228 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014229{
14230 max_min(argvars, rettv, TRUE);
14231}
14232
14233/*
14234 * "min()" function
14235 */
14236 static void
14237f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014238 typval_T *argvars;
14239 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014240{
14241 max_min(argvars, rettv, FALSE);
14242}
14243
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014244static int mkdir_recurse __ARGS((char_u *dir, int prot));
14245
14246/*
14247 * Create the directory in which "dir" is located, and higher levels when
14248 * needed.
14249 */
14250 static int
14251mkdir_recurse(dir, prot)
14252 char_u *dir;
14253 int prot;
14254{
14255 char_u *p;
14256 char_u *updir;
14257 int r = FAIL;
14258
14259 /* Get end of directory name in "dir".
14260 * We're done when it's "/" or "c:/". */
14261 p = gettail_sep(dir);
14262 if (p <= get_past_head(dir))
14263 return OK;
14264
14265 /* If the directory exists we're done. Otherwise: create it.*/
14266 updir = vim_strnsave(dir, (int)(p - dir));
14267 if (updir == NULL)
14268 return FAIL;
14269 if (mch_isdir(updir))
14270 r = OK;
14271 else if (mkdir_recurse(updir, prot) == OK)
14272 r = vim_mkdir_emsg(updir, prot);
14273 vim_free(updir);
14274 return r;
14275}
14276
14277#ifdef vim_mkdir
14278/*
14279 * "mkdir()" function
14280 */
14281 static void
14282f_mkdir(argvars, rettv)
14283 typval_T *argvars;
14284 typval_T *rettv;
14285{
14286 char_u *dir;
14287 char_u buf[NUMBUFLEN];
14288 int prot = 0755;
14289
14290 rettv->vval.v_number = FAIL;
14291 if (check_restricted() || check_secure())
14292 return;
14293
14294 dir = get_tv_string_buf(&argvars[0], buf);
14295 if (argvars[1].v_type != VAR_UNKNOWN)
14296 {
14297 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014298 prot = get_tv_number_chk(&argvars[2], NULL);
14299 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014300 mkdir_recurse(dir, prot);
14301 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014302 rettv->vval.v_number = prot != -1 ? vim_mkdir_emsg(dir, prot) : 0;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014303}
14304#endif
14305
Bram Moolenaar0d660222005-01-07 21:51:51 +000014306/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307 * "mode()" function
14308 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014310f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014311 typval_T *argvars;
14312 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014314 char_u buf[3];
14315
14316 buf[1] = NUL;
14317 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318
14319#ifdef FEAT_VISUAL
14320 if (VIsual_active)
14321 {
14322 if (VIsual_select)
14323 buf[0] = VIsual_mode + 's' - 'v';
14324 else
14325 buf[0] = VIsual_mode;
14326 }
14327 else
14328#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014329 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14330 || State == CONFIRM)
14331 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014333 if (State == ASKMORE)
14334 buf[1] = 'm';
14335 else if (State == CONFIRM)
14336 buf[1] = '?';
14337 }
14338 else if (State == EXTERNCMD)
14339 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340 else if (State & INSERT)
14341 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014342#ifdef FEAT_VREPLACE
14343 if (State & VREPLACE_FLAG)
14344 {
14345 buf[0] = 'R';
14346 buf[1] = 'v';
14347 }
14348 else
14349#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350 if (State & REPLACE_FLAG)
14351 buf[0] = 'R';
14352 else
14353 buf[0] = 'i';
14354 }
14355 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014356 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014358 if (exmode_active)
14359 buf[1] = 'v';
14360 }
14361 else if (exmode_active)
14362 {
14363 buf[0] = 'c';
14364 buf[1] = 'e';
14365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014367 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014369 if (finish_op)
14370 buf[1] = 'o';
14371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014373 /* Clear out the minor mode when the argument is not a non-zero number or
14374 * non-empty string. */
14375 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014376 buf[1] = NUL;
14377
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014378 rettv->vval.v_string = vim_strsave(buf);
14379 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380}
14381
Bram Moolenaar429fa852013-04-15 12:27:36 +020014382#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014383/*
14384 * "mzeval()" function
14385 */
14386 static void
14387f_mzeval(argvars, rettv)
14388 typval_T *argvars;
14389 typval_T *rettv;
14390{
14391 char_u *str;
14392 char_u buf[NUMBUFLEN];
14393
14394 str = get_tv_string_buf(&argvars[0], buf);
14395 do_mzeval(str, rettv);
14396}
Bram Moolenaar75676462013-01-30 14:55:42 +010014397
14398 void
14399mzscheme_call_vim(name, args, rettv)
14400 char_u *name;
14401 typval_T *args;
14402 typval_T *rettv;
14403{
14404 typval_T argvars[3];
14405
14406 argvars[0].v_type = VAR_STRING;
14407 argvars[0].vval.v_string = name;
14408 copy_tv(args, &argvars[1]);
14409 argvars[2].v_type = VAR_UNKNOWN;
14410 f_call(argvars, rettv);
14411 clear_tv(&argvars[1]);
14412}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014413#endif
14414
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014416 * "nextnonblank()" function
14417 */
14418 static void
14419f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014420 typval_T *argvars;
14421 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014422{
14423 linenr_T lnum;
14424
14425 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14426 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014427 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014428 {
14429 lnum = 0;
14430 break;
14431 }
14432 if (*skipwhite(ml_get(lnum)) != NUL)
14433 break;
14434 }
14435 rettv->vval.v_number = lnum;
14436}
14437
14438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014439 * "nr2char()" function
14440 */
14441 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014442f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014443 typval_T *argvars;
14444 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445{
14446 char_u buf[NUMBUFLEN];
14447
14448#ifdef FEAT_MBYTE
14449 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014450 {
14451 int utf8 = 0;
14452
14453 if (argvars[1].v_type != VAR_UNKNOWN)
14454 utf8 = get_tv_number_chk(&argvars[1], NULL);
14455 if (utf8)
14456 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14457 else
14458 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014460 else
14461#endif
14462 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014463 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464 buf[1] = NUL;
14465 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014466 rettv->v_type = VAR_STRING;
14467 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014468}
14469
14470/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014471 * "or(expr, expr)" function
14472 */
14473 static void
14474f_or(argvars, rettv)
14475 typval_T *argvars;
14476 typval_T *rettv;
14477{
14478 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14479 | get_tv_number_chk(&argvars[1], NULL);
14480}
14481
14482/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014483 * "pathshorten()" function
14484 */
14485 static void
14486f_pathshorten(argvars, rettv)
14487 typval_T *argvars;
14488 typval_T *rettv;
14489{
14490 char_u *p;
14491
14492 rettv->v_type = VAR_STRING;
14493 p = get_tv_string_chk(&argvars[0]);
14494 if (p == NULL)
14495 rettv->vval.v_string = NULL;
14496 else
14497 {
14498 p = vim_strsave(p);
14499 rettv->vval.v_string = p;
14500 if (p != NULL)
14501 shorten_dir(p);
14502 }
14503}
14504
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014505#ifdef FEAT_FLOAT
14506/*
14507 * "pow()" function
14508 */
14509 static void
14510f_pow(argvars, rettv)
14511 typval_T *argvars;
14512 typval_T *rettv;
14513{
14514 float_T fx, fy;
14515
14516 rettv->v_type = VAR_FLOAT;
14517 if (get_float_arg(argvars, &fx) == OK
14518 && get_float_arg(&argvars[1], &fy) == OK)
14519 rettv->vval.v_float = pow(fx, fy);
14520 else
14521 rettv->vval.v_float = 0.0;
14522}
14523#endif
14524
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014525/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014526 * "prevnonblank()" function
14527 */
14528 static void
14529f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014530 typval_T *argvars;
14531 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014532{
14533 linenr_T lnum;
14534
14535 lnum = get_tv_lnum(argvars);
14536 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14537 lnum = 0;
14538 else
14539 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14540 --lnum;
14541 rettv->vval.v_number = lnum;
14542}
14543
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014544#ifdef HAVE_STDARG_H
14545/* This dummy va_list is here because:
14546 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14547 * - locally in the function results in a "used before set" warning
14548 * - using va_start() to initialize it gives "function with fixed args" error */
14549static va_list ap;
14550#endif
14551
Bram Moolenaar8c711452005-01-14 21:53:12 +000014552/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014553 * "printf()" function
14554 */
14555 static void
14556f_printf(argvars, rettv)
14557 typval_T *argvars;
14558 typval_T *rettv;
14559{
14560 rettv->v_type = VAR_STRING;
14561 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014562#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014563 {
14564 char_u buf[NUMBUFLEN];
14565 int len;
14566 char_u *s;
14567 int saved_did_emsg = did_emsg;
14568 char *fmt;
14569
14570 /* Get the required length, allocate the buffer and do it for real. */
14571 did_emsg = FALSE;
14572 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014573 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014574 if (!did_emsg)
14575 {
14576 s = alloc(len + 1);
14577 if (s != NULL)
14578 {
14579 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014580 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014581 }
14582 }
14583 did_emsg |= saved_did_emsg;
14584 }
14585#endif
14586}
14587
14588/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014589 * "pumvisible()" function
14590 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014591 static void
14592f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014593 typval_T *argvars UNUSED;
14594 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014595{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014596#ifdef FEAT_INS_EXPAND
14597 if (pum_visible())
14598 rettv->vval.v_number = 1;
14599#endif
14600}
14601
Bram Moolenaardb913952012-06-29 12:54:53 +020014602#ifdef FEAT_PYTHON3
14603/*
14604 * "py3eval()" function
14605 */
14606 static void
14607f_py3eval(argvars, rettv)
14608 typval_T *argvars;
14609 typval_T *rettv;
14610{
14611 char_u *str;
14612 char_u buf[NUMBUFLEN];
14613
14614 str = get_tv_string_buf(&argvars[0], buf);
14615 do_py3eval(str, rettv);
14616}
14617#endif
14618
14619#ifdef FEAT_PYTHON
14620/*
14621 * "pyeval()" function
14622 */
14623 static void
14624f_pyeval(argvars, rettv)
14625 typval_T *argvars;
14626 typval_T *rettv;
14627{
14628 char_u *str;
14629 char_u buf[NUMBUFLEN];
14630
14631 str = get_tv_string_buf(&argvars[0], buf);
14632 do_pyeval(str, rettv);
14633}
14634#endif
14635
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014636/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014637 * "range()" function
14638 */
14639 static void
14640f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014641 typval_T *argvars;
14642 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014643{
14644 long start;
14645 long end;
14646 long stride = 1;
14647 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014648 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014650 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014651 if (argvars[1].v_type == VAR_UNKNOWN)
14652 {
14653 end = start - 1;
14654 start = 0;
14655 }
14656 else
14657 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014658 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014659 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014660 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014661 }
14662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014663 if (error)
14664 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014665 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014666 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014667 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014668 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014669 else
14670 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014671 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014672 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014673 if (list_append_number(rettv->vval.v_list,
14674 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014675 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014676 }
14677}
14678
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014679/*
14680 * "readfile()" function
14681 */
14682 static void
14683f_readfile(argvars, rettv)
14684 typval_T *argvars;
14685 typval_T *rettv;
14686{
14687 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014688 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014689 char_u *fname;
14690 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014691 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14692 int io_size = sizeof(buf);
14693 int readlen; /* size of last fread() */
14694 char_u *prev = NULL; /* previously read bytes, if any */
14695 long prevlen = 0; /* length of data in prev */
14696 long prevsize = 0; /* size of prev buffer */
14697 long maxline = MAXLNUM;
14698 long cnt = 0;
14699 char_u *p; /* position in buf */
14700 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014701
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014702 if (argvars[1].v_type != VAR_UNKNOWN)
14703 {
14704 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14705 binary = TRUE;
14706 if (argvars[2].v_type != VAR_UNKNOWN)
14707 maxline = get_tv_number(&argvars[2]);
14708 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014709
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014710 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014711 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014712
14713 /* Always open the file in binary mode, library functions have a mind of
14714 * their own about CR-LF conversion. */
14715 fname = get_tv_string(&argvars[0]);
14716 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14717 {
14718 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14719 return;
14720 }
14721
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014722 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014723 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014724 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014725
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014726 /* This for loop processes what was read, but is also entered at end
14727 * of file so that either:
14728 * - an incomplete line gets written
14729 * - a "binary" file gets an empty line at the end if it ends in a
14730 * newline. */
14731 for (p = buf, start = buf;
14732 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14733 ++p)
14734 {
14735 if (*p == '\n' || readlen <= 0)
14736 {
14737 listitem_T *li;
14738 char_u *s = NULL;
14739 long_u len = p - start;
14740
14741 /* Finished a line. Remove CRs before NL. */
14742 if (readlen > 0 && !binary)
14743 {
14744 while (len > 0 && start[len - 1] == '\r')
14745 --len;
14746 /* removal may cross back to the "prev" string */
14747 if (len == 0)
14748 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14749 --prevlen;
14750 }
14751 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014752 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014753 else
14754 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014755 /* Change "prev" buffer to be the right size. This way
14756 * the bytes are only copied once, and very long lines are
14757 * allocated only once. */
14758 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014759 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014760 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014761 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014762 prev = NULL; /* the list will own the string */
14763 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014764 }
14765 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014766 if (s == NULL)
14767 {
14768 do_outofmem_msg((long_u) prevlen + len + 1);
14769 failed = TRUE;
14770 break;
14771 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014772
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014773 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014774 {
14775 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014776 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014777 break;
14778 }
14779 li->li_tv.v_type = VAR_STRING;
14780 li->li_tv.v_lock = 0;
14781 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014782 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014783
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014784 start = p + 1; /* step over newline */
14785 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014786 break;
14787 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014788 else if (*p == NUL)
14789 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014790#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014791 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14792 * when finding the BF and check the previous two bytes. */
14793 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014794 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014795 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14796 * + 1, these may be in the "prev" string. */
14797 char_u back1 = p >= buf + 1 ? p[-1]
14798 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14799 char_u back2 = p >= buf + 2 ? p[-2]
14800 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14801 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014802
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014803 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014804 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014805 char_u *dest = p - 2;
14806
14807 /* Usually a BOM is at the beginning of a file, and so at
14808 * the beginning of a line; then we can just step over it.
14809 */
14810 if (start == dest)
14811 start = p + 1;
14812 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014813 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014814 /* have to shuffle buf to close gap */
14815 int adjust_prevlen = 0;
14816
14817 if (dest < buf)
14818 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014819 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014820 dest = buf;
14821 }
14822 if (readlen > p - buf + 1)
14823 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14824 readlen -= 3 - adjust_prevlen;
14825 prevlen -= adjust_prevlen;
14826 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014827 }
14828 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014829 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014830#endif
14831 } /* for */
14832
14833 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14834 break;
14835 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014836 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014837 /* There's part of a line in buf, store it in "prev". */
14838 if (p - start + prevlen >= prevsize)
14839 {
14840 /* need bigger "prev" buffer */
14841 char_u *newprev;
14842
14843 /* A common use case is ordinary text files and "prev" gets a
14844 * fragment of a line, so the first allocation is made
14845 * small, to avoid repeatedly 'allocing' large and
14846 * 'reallocing' small. */
14847 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014848 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014849 else
14850 {
14851 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014852 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014853 prevsize = grow50pc > growmin ? grow50pc : growmin;
14854 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014855 newprev = prev == NULL ? alloc(prevsize)
14856 : vim_realloc(prev, prevsize);
14857 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014858 {
14859 do_outofmem_msg((long_u)prevsize);
14860 failed = TRUE;
14861 break;
14862 }
14863 prev = newprev;
14864 }
14865 /* Add the line part to end of "prev". */
14866 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014867 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014868 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014869 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014870
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014871 /*
14872 * For a negative line count use only the lines at the end of the file,
14873 * free the rest.
14874 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014875 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014876 while (cnt > -maxline)
14877 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014878 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014879 --cnt;
14880 }
14881
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014882 if (failed)
14883 {
14884 list_free(rettv->vval.v_list, TRUE);
14885 /* readfile doc says an empty list is returned on error */
14886 rettv->vval.v_list = list_alloc();
14887 }
14888
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014889 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014890 fclose(fd);
14891}
14892
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014893#if defined(FEAT_RELTIME)
14894static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14895
14896/*
14897 * Convert a List to proftime_T.
14898 * Return FAIL when there is something wrong.
14899 */
14900 static int
14901list2proftime(arg, tm)
14902 typval_T *arg;
14903 proftime_T *tm;
14904{
14905 long n1, n2;
14906 int error = FALSE;
14907
14908 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14909 || arg->vval.v_list->lv_len != 2)
14910 return FAIL;
14911 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14912 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14913# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014914 tm->HighPart = n1;
14915 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014916# else
14917 tm->tv_sec = n1;
14918 tm->tv_usec = n2;
14919# endif
14920 return error ? FAIL : OK;
14921}
14922#endif /* FEAT_RELTIME */
14923
14924/*
14925 * "reltime()" function
14926 */
14927 static void
14928f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014929 typval_T *argvars UNUSED;
14930 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014931{
14932#ifdef FEAT_RELTIME
14933 proftime_T res;
14934 proftime_T start;
14935
14936 if (argvars[0].v_type == VAR_UNKNOWN)
14937 {
14938 /* No arguments: get current time. */
14939 profile_start(&res);
14940 }
14941 else if (argvars[1].v_type == VAR_UNKNOWN)
14942 {
14943 if (list2proftime(&argvars[0], &res) == FAIL)
14944 return;
14945 profile_end(&res);
14946 }
14947 else
14948 {
14949 /* Two arguments: compute the difference. */
14950 if (list2proftime(&argvars[0], &start) == FAIL
14951 || list2proftime(&argvars[1], &res) == FAIL)
14952 return;
14953 profile_sub(&res, &start);
14954 }
14955
14956 if (rettv_list_alloc(rettv) == OK)
14957 {
14958 long n1, n2;
14959
14960# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014961 n1 = res.HighPart;
14962 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014963# else
14964 n1 = res.tv_sec;
14965 n2 = res.tv_usec;
14966# endif
14967 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14968 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14969 }
14970#endif
14971}
14972
14973/*
14974 * "reltimestr()" function
14975 */
14976 static void
14977f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014978 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014979 typval_T *rettv;
14980{
14981#ifdef FEAT_RELTIME
14982 proftime_T tm;
14983#endif
14984
14985 rettv->v_type = VAR_STRING;
14986 rettv->vval.v_string = NULL;
14987#ifdef FEAT_RELTIME
14988 if (list2proftime(&argvars[0], &tm) == OK)
14989 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
14990#endif
14991}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014992
Bram Moolenaar0d660222005-01-07 21:51:51 +000014993#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
14994static void make_connection __ARGS((void));
14995static int check_connection __ARGS((void));
14996
14997 static void
14998make_connection()
14999{
15000 if (X_DISPLAY == NULL
15001# ifdef FEAT_GUI
15002 && !gui.in_use
15003# endif
15004 )
15005 {
15006 x_force_connect = TRUE;
15007 setup_term_clip();
15008 x_force_connect = FALSE;
15009 }
15010}
15011
15012 static int
15013check_connection()
15014{
15015 make_connection();
15016 if (X_DISPLAY == NULL)
15017 {
15018 EMSG(_("E240: No connection to Vim server"));
15019 return FAIL;
15020 }
15021 return OK;
15022}
15023#endif
15024
15025#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015026static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015027
15028 static void
15029remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015030 typval_T *argvars;
15031 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015032 int expr;
15033{
15034 char_u *server_name;
15035 char_u *keys;
15036 char_u *r = NULL;
15037 char_u buf[NUMBUFLEN];
15038# ifdef WIN32
15039 HWND w;
15040# else
15041 Window w;
15042# endif
15043
15044 if (check_restricted() || check_secure())
15045 return;
15046
15047# ifdef FEAT_X11
15048 if (check_connection() == FAIL)
15049 return;
15050# endif
15051
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015052 server_name = get_tv_string_chk(&argvars[0]);
15053 if (server_name == NULL)
15054 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015055 keys = get_tv_string_buf(&argvars[1], buf);
15056# ifdef WIN32
15057 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15058# else
15059 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15060 < 0)
15061# endif
15062 {
15063 if (r != NULL)
15064 EMSG(r); /* sending worked but evaluation failed */
15065 else
15066 EMSG2(_("E241: Unable to send to %s"), server_name);
15067 return;
15068 }
15069
15070 rettv->vval.v_string = r;
15071
15072 if (argvars[2].v_type != VAR_UNKNOWN)
15073 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015074 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015075 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015076 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015077
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015078 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015079 v.di_tv.v_type = VAR_STRING;
15080 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015081 idvar = get_tv_string_chk(&argvars[2]);
15082 if (idvar != NULL)
15083 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015084 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015085 }
15086}
15087#endif
15088
15089/*
15090 * "remote_expr()" function
15091 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015092 static void
15093f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015094 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015095 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015096{
15097 rettv->v_type = VAR_STRING;
15098 rettv->vval.v_string = NULL;
15099#ifdef FEAT_CLIENTSERVER
15100 remote_common(argvars, rettv, TRUE);
15101#endif
15102}
15103
15104/*
15105 * "remote_foreground()" function
15106 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015107 static void
15108f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015109 typval_T *argvars UNUSED;
15110 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015111{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015112#ifdef FEAT_CLIENTSERVER
15113# ifdef WIN32
15114 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015115 {
15116 char_u *server_name = get_tv_string_chk(&argvars[0]);
15117
15118 if (server_name != NULL)
15119 serverForeground(server_name);
15120 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015121# else
15122 /* Send a foreground() expression to the server. */
15123 argvars[1].v_type = VAR_STRING;
15124 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15125 argvars[2].v_type = VAR_UNKNOWN;
15126 remote_common(argvars, rettv, TRUE);
15127 vim_free(argvars[1].vval.v_string);
15128# endif
15129#endif
15130}
15131
Bram Moolenaar0d660222005-01-07 21:51:51 +000015132 static void
15133f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015134 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015135 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015136{
15137#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015138 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015139 char_u *s = NULL;
15140# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015141 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015142# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015143 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015144
15145 if (check_restricted() || check_secure())
15146 {
15147 rettv->vval.v_number = -1;
15148 return;
15149 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015150 serverid = get_tv_string_chk(&argvars[0]);
15151 if (serverid == NULL)
15152 {
15153 rettv->vval.v_number = -1;
15154 return; /* type error; errmsg already given */
15155 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015156# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015157 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015158 if (n == 0)
15159 rettv->vval.v_number = -1;
15160 else
15161 {
15162 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15163 rettv->vval.v_number = (s != NULL);
15164 }
15165# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015166 if (check_connection() == FAIL)
15167 return;
15168
15169 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015170 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015171# endif
15172
15173 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15174 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015175 char_u *retvar;
15176
Bram Moolenaar33570922005-01-25 22:26:29 +000015177 v.di_tv.v_type = VAR_STRING;
15178 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015179 retvar = get_tv_string_chk(&argvars[1]);
15180 if (retvar != NULL)
15181 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015182 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015183 }
15184#else
15185 rettv->vval.v_number = -1;
15186#endif
15187}
15188
Bram Moolenaar0d660222005-01-07 21:51:51 +000015189 static void
15190f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015191 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015192 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015193{
15194 char_u *r = NULL;
15195
15196#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015197 char_u *serverid = get_tv_string_chk(&argvars[0]);
15198
15199 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015200 {
15201# ifdef WIN32
15202 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015203 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015204
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015205 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015206 if (n != 0)
15207 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15208 if (r == NULL)
15209# else
15210 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015211 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015212# endif
15213 EMSG(_("E277: Unable to read a server reply"));
15214 }
15215#endif
15216 rettv->v_type = VAR_STRING;
15217 rettv->vval.v_string = r;
15218}
15219
15220/*
15221 * "remote_send()" function
15222 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015223 static void
15224f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015225 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015226 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015227{
15228 rettv->v_type = VAR_STRING;
15229 rettv->vval.v_string = NULL;
15230#ifdef FEAT_CLIENTSERVER
15231 remote_common(argvars, rettv, FALSE);
15232#endif
15233}
15234
15235/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015236 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015237 */
15238 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015239f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015240 typval_T *argvars;
15241 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015242{
Bram Moolenaar33570922005-01-25 22:26:29 +000015243 list_T *l;
15244 listitem_T *item, *item2;
15245 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015246 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015247 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015248 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015249 dict_T *d;
15250 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015251 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015252
Bram Moolenaar8c711452005-01-14 21:53:12 +000015253 if (argvars[0].v_type == VAR_DICT)
15254 {
15255 if (argvars[2].v_type != VAR_UNKNOWN)
15256 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015257 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015258 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015260 key = get_tv_string_chk(&argvars[1]);
15261 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015262 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015263 di = dict_find(d, key, -1);
15264 if (di == NULL)
15265 EMSG2(_(e_dictkey), key);
15266 else
15267 {
15268 *rettv = di->di_tv;
15269 init_tv(&di->di_tv);
15270 dictitem_remove(d, di);
15271 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015272 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015273 }
15274 }
15275 else if (argvars[0].v_type != VAR_LIST)
15276 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015277 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015278 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015280 int error = FALSE;
15281
15282 idx = get_tv_number_chk(&argvars[1], &error);
15283 if (error)
15284 ; /* type error: do nothing, errmsg already given */
15285 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015286 EMSGN(_(e_listidx), idx);
15287 else
15288 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015289 if (argvars[2].v_type == VAR_UNKNOWN)
15290 {
15291 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015292 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015293 *rettv = item->li_tv;
15294 vim_free(item);
15295 }
15296 else
15297 {
15298 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015299 end = get_tv_number_chk(&argvars[2], &error);
15300 if (error)
15301 ; /* type error: do nothing */
15302 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015303 EMSGN(_(e_listidx), end);
15304 else
15305 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015306 int cnt = 0;
15307
15308 for (li = item; li != NULL; li = li->li_next)
15309 {
15310 ++cnt;
15311 if (li == item2)
15312 break;
15313 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015314 if (li == NULL) /* didn't find "item2" after "item" */
15315 EMSG(_(e_invrange));
15316 else
15317 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015318 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015319 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015320 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015321 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015322 l->lv_first = item;
15323 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015324 item->li_prev = NULL;
15325 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015326 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015327 }
15328 }
15329 }
15330 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015331 }
15332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333}
15334
15335/*
15336 * "rename({from}, {to})" function
15337 */
15338 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015339f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015340 typval_T *argvars;
15341 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342{
15343 char_u buf[NUMBUFLEN];
15344
15345 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015346 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015347 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015348 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15349 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350}
15351
15352/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015353 * "repeat()" function
15354 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015355 static void
15356f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015357 typval_T *argvars;
15358 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015359{
15360 char_u *p;
15361 int n;
15362 int slen;
15363 int len;
15364 char_u *r;
15365 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015366
15367 n = get_tv_number(&argvars[1]);
15368 if (argvars[0].v_type == VAR_LIST)
15369 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015370 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015371 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015372 if (list_extend(rettv->vval.v_list,
15373 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015374 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015375 }
15376 else
15377 {
15378 p = get_tv_string(&argvars[0]);
15379 rettv->v_type = VAR_STRING;
15380 rettv->vval.v_string = NULL;
15381
15382 slen = (int)STRLEN(p);
15383 len = slen * n;
15384 if (len <= 0)
15385 return;
15386
15387 r = alloc(len + 1);
15388 if (r != NULL)
15389 {
15390 for (i = 0; i < n; i++)
15391 mch_memmove(r + i * slen, p, (size_t)slen);
15392 r[len] = NUL;
15393 }
15394
15395 rettv->vval.v_string = r;
15396 }
15397}
15398
15399/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015400 * "resolve()" function
15401 */
15402 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015403f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015404 typval_T *argvars;
15405 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015406{
15407 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015408#ifdef HAVE_READLINK
15409 char_u *buf = NULL;
15410#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015411
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015412 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413#ifdef FEAT_SHORTCUT
15414 {
15415 char_u *v = NULL;
15416
15417 v = mch_resolve_shortcut(p);
15418 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015419 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015421 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422 }
15423#else
15424# ifdef HAVE_READLINK
15425 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015426 char_u *cpy;
15427 int len;
15428 char_u *remain = NULL;
15429 char_u *q;
15430 int is_relative_to_current = FALSE;
15431 int has_trailing_pathsep = FALSE;
15432 int limit = 100;
15433
15434 p = vim_strsave(p);
15435
15436 if (p[0] == '.' && (vim_ispathsep(p[1])
15437 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15438 is_relative_to_current = TRUE;
15439
15440 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015441 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015444 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015446
15447 q = getnextcomp(p);
15448 if (*q != NUL)
15449 {
15450 /* Separate the first path component in "p", and keep the
15451 * remainder (beginning with the path separator). */
15452 remain = vim_strsave(q - 1);
15453 q[-1] = NUL;
15454 }
15455
Bram Moolenaard9462e32011-04-11 21:35:11 +020015456 buf = alloc(MAXPATHL + 1);
15457 if (buf == NULL)
15458 goto fail;
15459
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 for (;;)
15461 {
15462 for (;;)
15463 {
15464 len = readlink((char *)p, (char *)buf, MAXPATHL);
15465 if (len <= 0)
15466 break;
15467 buf[len] = NUL;
15468
15469 if (limit-- == 0)
15470 {
15471 vim_free(p);
15472 vim_free(remain);
15473 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015474 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015475 goto fail;
15476 }
15477
15478 /* Ensure that the result will have a trailing path separator
15479 * if the argument has one. */
15480 if (remain == NULL && has_trailing_pathsep)
15481 add_pathsep(buf);
15482
15483 /* Separate the first path component in the link value and
15484 * concatenate the remainders. */
15485 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15486 if (*q != NUL)
15487 {
15488 if (remain == NULL)
15489 remain = vim_strsave(q - 1);
15490 else
15491 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015492 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015493 if (cpy != NULL)
15494 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495 vim_free(remain);
15496 remain = cpy;
15497 }
15498 }
15499 q[-1] = NUL;
15500 }
15501
15502 q = gettail(p);
15503 if (q > p && *q == NUL)
15504 {
15505 /* Ignore trailing path separator. */
15506 q[-1] = NUL;
15507 q = gettail(p);
15508 }
15509 if (q > p && !mch_isFullName(buf))
15510 {
15511 /* symlink is relative to directory of argument */
15512 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15513 if (cpy != NULL)
15514 {
15515 STRCPY(cpy, p);
15516 STRCPY(gettail(cpy), buf);
15517 vim_free(p);
15518 p = cpy;
15519 }
15520 }
15521 else
15522 {
15523 vim_free(p);
15524 p = vim_strsave(buf);
15525 }
15526 }
15527
15528 if (remain == NULL)
15529 break;
15530
15531 /* Append the first path component of "remain" to "p". */
15532 q = getnextcomp(remain + 1);
15533 len = q - remain - (*q != NUL);
15534 cpy = vim_strnsave(p, STRLEN(p) + len);
15535 if (cpy != NULL)
15536 {
15537 STRNCAT(cpy, remain, len);
15538 vim_free(p);
15539 p = cpy;
15540 }
15541 /* Shorten "remain". */
15542 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015543 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 else
15545 {
15546 vim_free(remain);
15547 remain = NULL;
15548 }
15549 }
15550
15551 /* If the result is a relative path name, make it explicitly relative to
15552 * the current directory if and only if the argument had this form. */
15553 if (!vim_ispathsep(*p))
15554 {
15555 if (is_relative_to_current
15556 && *p != NUL
15557 && !(p[0] == '.'
15558 && (p[1] == NUL
15559 || vim_ispathsep(p[1])
15560 || (p[1] == '.'
15561 && (p[2] == NUL
15562 || vim_ispathsep(p[2]))))))
15563 {
15564 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015565 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566 if (cpy != NULL)
15567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568 vim_free(p);
15569 p = cpy;
15570 }
15571 }
15572 else if (!is_relative_to_current)
15573 {
15574 /* Strip leading "./". */
15575 q = p;
15576 while (q[0] == '.' && vim_ispathsep(q[1]))
15577 q += 2;
15578 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015579 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015580 }
15581 }
15582
15583 /* Ensure that the result will have no trailing path separator
15584 * if the argument had none. But keep "/" or "//". */
15585 if (!has_trailing_pathsep)
15586 {
15587 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015588 if (after_pathsep(p, q))
15589 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015590 }
15591
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015592 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015593 }
15594# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015595 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015596# endif
15597#endif
15598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015599 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015600
15601#ifdef HAVE_READLINK
15602fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015603 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015604#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015605 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015606}
15607
15608/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015609 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610 */
15611 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015612f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015613 typval_T *argvars;
15614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015615{
Bram Moolenaar33570922005-01-25 22:26:29 +000015616 list_T *l;
15617 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618
Bram Moolenaar0d660222005-01-07 21:51:51 +000015619 if (argvars[0].v_type != VAR_LIST)
15620 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015621 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015622 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015623 {
15624 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015625 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015626 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015627 while (li != NULL)
15628 {
15629 ni = li->li_prev;
15630 list_append(l, li);
15631 li = ni;
15632 }
15633 rettv->vval.v_list = l;
15634 rettv->v_type = VAR_LIST;
15635 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015636 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015638}
15639
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015640#define SP_NOMOVE 0x01 /* don't move cursor */
15641#define SP_REPEAT 0x02 /* repeat to find outer pair */
15642#define SP_RETCOUNT 0x04 /* return matchcount */
15643#define SP_SETPCMARK 0x08 /* set previous context mark */
15644#define SP_START 0x10 /* accept match at start position */
15645#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15646#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015647
Bram Moolenaar33570922005-01-25 22:26:29 +000015648static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015649
15650/*
15651 * Get flags for a search function.
15652 * Possibly sets "p_ws".
15653 * Returns BACKWARD, FORWARD or zero (for an error).
15654 */
15655 static int
15656get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015657 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015658 int *flagsp;
15659{
15660 int dir = FORWARD;
15661 char_u *flags;
15662 char_u nbuf[NUMBUFLEN];
15663 int mask;
15664
15665 if (varp->v_type != VAR_UNKNOWN)
15666 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015667 flags = get_tv_string_buf_chk(varp, nbuf);
15668 if (flags == NULL)
15669 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015670 while (*flags != NUL)
15671 {
15672 switch (*flags)
15673 {
15674 case 'b': dir = BACKWARD; break;
15675 case 'w': p_ws = TRUE; break;
15676 case 'W': p_ws = FALSE; break;
15677 default: mask = 0;
15678 if (flagsp != NULL)
15679 switch (*flags)
15680 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015681 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015682 case 'e': mask = SP_END; break;
15683 case 'm': mask = SP_RETCOUNT; break;
15684 case 'n': mask = SP_NOMOVE; break;
15685 case 'p': mask = SP_SUBPAT; break;
15686 case 'r': mask = SP_REPEAT; break;
15687 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015688 }
15689 if (mask == 0)
15690 {
15691 EMSG2(_(e_invarg2), flags);
15692 dir = 0;
15693 }
15694 else
15695 *flagsp |= mask;
15696 }
15697 if (dir == 0)
15698 break;
15699 ++flags;
15700 }
15701 }
15702 return dir;
15703}
15704
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015706 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015708 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015709search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015710 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015711 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015712 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015714 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 char_u *pat;
15716 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015717 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015718 int save_p_ws = p_ws;
15719 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015720 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015721 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015722 proftime_T tm;
15723#ifdef FEAT_RELTIME
15724 long time_limit = 0;
15725#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015726 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015727 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015729 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015730 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015731 if (dir == 0)
15732 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015733 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015734 if (flags & SP_START)
15735 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015736 if (flags & SP_END)
15737 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015738
Bram Moolenaar76929292008-01-06 19:07:36 +000015739 /* Optional arguments: line number to stop searching and timeout. */
15740 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015741 {
15742 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15743 if (lnum_stop < 0)
15744 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015745#ifdef FEAT_RELTIME
15746 if (argvars[3].v_type != VAR_UNKNOWN)
15747 {
15748 time_limit = get_tv_number_chk(&argvars[3], NULL);
15749 if (time_limit < 0)
15750 goto theend;
15751 }
15752#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015753 }
15754
Bram Moolenaar76929292008-01-06 19:07:36 +000015755#ifdef FEAT_RELTIME
15756 /* Set the time limit, if there is one. */
15757 profile_setlimit(time_limit, &tm);
15758#endif
15759
Bram Moolenaar231334e2005-07-25 20:46:57 +000015760 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015761 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015762 * Check to make sure only those flags are set.
15763 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15764 * flags cannot be set. Check for that condition also.
15765 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015766 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015767 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015769 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015770 goto theend;
15771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015772
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015773 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015774 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015775 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015776 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015777 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015778 if (flags & SP_SUBPAT)
15779 retval = subpatnum;
15780 else
15781 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015782 if (flags & SP_SETPCMARK)
15783 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015785 if (match_pos != NULL)
15786 {
15787 /* Store the match cursor position */
15788 match_pos->lnum = pos.lnum;
15789 match_pos->col = pos.col + 1;
15790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015791 /* "/$" will put the cursor after the end of the line, may need to
15792 * correct that here */
15793 check_cursor();
15794 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015795
15796 /* If 'n' flag is used: restore cursor position. */
15797 if (flags & SP_NOMOVE)
15798 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015799 else
15800 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015801theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015803
15804 return retval;
15805}
15806
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015807#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015808
15809/*
15810 * round() is not in C90, use ceil() or floor() instead.
15811 */
15812 float_T
15813vim_round(f)
15814 float_T f;
15815{
15816 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15817}
15818
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015819/*
15820 * "round({float})" function
15821 */
15822 static void
15823f_round(argvars, rettv)
15824 typval_T *argvars;
15825 typval_T *rettv;
15826{
15827 float_T f;
15828
15829 rettv->v_type = VAR_FLOAT;
15830 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015831 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015832 else
15833 rettv->vval.v_float = 0.0;
15834}
15835#endif
15836
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015837/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015838 * "screenattr()" function
15839 */
15840 static void
15841f_screenattr(argvars, rettv)
15842 typval_T *argvars UNUSED;
15843 typval_T *rettv;
15844{
15845 int row;
15846 int col;
15847 int c;
15848
15849 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15850 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15851 if (row < 0 || row >= screen_Rows
15852 || col < 0 || col >= screen_Columns)
15853 c = -1;
15854 else
15855 c = ScreenAttrs[LineOffset[row] + col];
15856 rettv->vval.v_number = c;
15857}
15858
15859/*
15860 * "screenchar()" function
15861 */
15862 static void
15863f_screenchar(argvars, rettv)
15864 typval_T *argvars UNUSED;
15865 typval_T *rettv;
15866{
15867 int row;
15868 int col;
15869 int off;
15870 int c;
15871
15872 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15873 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15874 if (row < 0 || row >= screen_Rows
15875 || col < 0 || col >= screen_Columns)
15876 c = -1;
15877 else
15878 {
15879 off = LineOffset[row] + col;
15880#ifdef FEAT_MBYTE
15881 if (enc_utf8 && ScreenLinesUC[off] != 0)
15882 c = ScreenLinesUC[off];
15883 else
15884#endif
15885 c = ScreenLines[off];
15886 }
15887 rettv->vval.v_number = c;
15888}
15889
15890/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015891 * "screencol()" function
15892 *
15893 * First column is 1 to be consistent with virtcol().
15894 */
15895 static void
15896f_screencol(argvars, rettv)
15897 typval_T *argvars UNUSED;
15898 typval_T *rettv;
15899{
15900 rettv->vval.v_number = screen_screencol() + 1;
15901}
15902
15903/*
15904 * "screenrow()" function
15905 */
15906 static void
15907f_screenrow(argvars, rettv)
15908 typval_T *argvars UNUSED;
15909 typval_T *rettv;
15910{
15911 rettv->vval.v_number = screen_screenrow() + 1;
15912}
15913
15914/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015915 * "search()" function
15916 */
15917 static void
15918f_search(argvars, rettv)
15919 typval_T *argvars;
15920 typval_T *rettv;
15921{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015922 int flags = 0;
15923
15924 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015925}
15926
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015928 * "searchdecl()" function
15929 */
15930 static void
15931f_searchdecl(argvars, rettv)
15932 typval_T *argvars;
15933 typval_T *rettv;
15934{
15935 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015936 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015937 int error = FALSE;
15938 char_u *name;
15939
15940 rettv->vval.v_number = 1; /* default: FAIL */
15941
15942 name = get_tv_string_chk(&argvars[0]);
15943 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015944 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015945 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015946 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15947 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15948 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015949 if (!error && name != NULL)
15950 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015951 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015952}
15953
15954/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015955 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015956 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015957 static int
15958searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015959 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015960 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015961{
15962 char_u *spat, *mpat, *epat;
15963 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015964 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965 int dir;
15966 int flags = 0;
15967 char_u nbuf1[NUMBUFLEN];
15968 char_u nbuf2[NUMBUFLEN];
15969 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015970 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015971 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015972 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015975 spat = get_tv_string_chk(&argvars[0]);
15976 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15977 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15978 if (spat == NULL || mpat == NULL || epat == NULL)
15979 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015980
Bram Moolenaar071d4272004-06-13 20:20:40 +000015981 /* Handle the optional fourth argument: flags */
15982 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015983 if (dir == 0)
15984 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015985
15986 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015987 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
15988 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015989 if ((flags & (SP_END | SP_SUBPAT)) != 0
15990 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000015991 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015992 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000015993 goto theend;
15994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015995
Bram Moolenaar92de73d2008-01-22 10:59:38 +000015996 /* Using 'r' implies 'W', otherwise it doesn't work. */
15997 if (flags & SP_REPEAT)
15998 p_ws = FALSE;
15999
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016000 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016001 if (argvars[3].v_type == VAR_UNKNOWN
16002 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016003 skip = (char_u *)"";
16004 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016005 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016006 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016007 if (argvars[5].v_type != VAR_UNKNOWN)
16008 {
16009 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16010 if (lnum_stop < 0)
16011 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016012#ifdef FEAT_RELTIME
16013 if (argvars[6].v_type != VAR_UNKNOWN)
16014 {
16015 time_limit = get_tv_number_chk(&argvars[6], NULL);
16016 if (time_limit < 0)
16017 goto theend;
16018 }
16019#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016020 }
16021 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016022 if (skip == NULL)
16023 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016024
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016025 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016026 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016027
16028theend:
16029 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016030
16031 return retval;
16032}
16033
16034/*
16035 * "searchpair()" function
16036 */
16037 static void
16038f_searchpair(argvars, rettv)
16039 typval_T *argvars;
16040 typval_T *rettv;
16041{
16042 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16043}
16044
16045/*
16046 * "searchpairpos()" function
16047 */
16048 static void
16049f_searchpairpos(argvars, rettv)
16050 typval_T *argvars;
16051 typval_T *rettv;
16052{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016053 pos_T match_pos;
16054 int lnum = 0;
16055 int col = 0;
16056
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016057 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016058 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016059
16060 if (searchpair_cmn(argvars, &match_pos) > 0)
16061 {
16062 lnum = match_pos.lnum;
16063 col = match_pos.col;
16064 }
16065
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016066 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16067 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016068}
16069
16070/*
16071 * Search for a start/middle/end thing.
16072 * Used by searchpair(), see its documentation for the details.
16073 * Returns 0 or -1 for no match,
16074 */
16075 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016076do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16077 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016078 char_u *spat; /* start pattern */
16079 char_u *mpat; /* middle pattern */
16080 char_u *epat; /* end pattern */
16081 int dir; /* BACKWARD or FORWARD */
16082 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016083 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016084 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016085 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016086 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016087{
16088 char_u *save_cpo;
16089 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16090 long retval = 0;
16091 pos_T pos;
16092 pos_T firstpos;
16093 pos_T foundpos;
16094 pos_T save_cursor;
16095 pos_T save_pos;
16096 int n;
16097 int r;
16098 int nest = 1;
16099 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016100 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016101 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016102
16103 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16104 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016105 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016106
Bram Moolenaar76929292008-01-06 19:07:36 +000016107#ifdef FEAT_RELTIME
16108 /* Set the time limit, if there is one. */
16109 profile_setlimit(time_limit, &tm);
16110#endif
16111
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016112 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16113 * start/middle/end (pat3, for the top pair). */
16114 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16115 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16116 if (pat2 == NULL || pat3 == NULL)
16117 goto theend;
16118 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16119 if (*mpat == NUL)
16120 STRCPY(pat3, pat2);
16121 else
16122 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16123 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016124 if (flags & SP_START)
16125 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016126
Bram Moolenaar071d4272004-06-13 20:20:40 +000016127 save_cursor = curwin->w_cursor;
16128 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016129 clearpos(&firstpos);
16130 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016131 pat = pat3;
16132 for (;;)
16133 {
16134 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016135 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016136 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16137 /* didn't find it or found the first match again: FAIL */
16138 break;
16139
16140 if (firstpos.lnum == 0)
16141 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016142 if (equalpos(pos, foundpos))
16143 {
16144 /* Found the same position again. Can happen with a pattern that
16145 * has "\zs" at the end and searching backwards. Advance one
16146 * character and try again. */
16147 if (dir == BACKWARD)
16148 decl(&pos);
16149 else
16150 incl(&pos);
16151 }
16152 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016154 /* clear the start flag to avoid getting stuck here */
16155 options &= ~SEARCH_START;
16156
Bram Moolenaar071d4272004-06-13 20:20:40 +000016157 /* If the skip pattern matches, ignore this match. */
16158 if (*skip != NUL)
16159 {
16160 save_pos = curwin->w_cursor;
16161 curwin->w_cursor = pos;
16162 r = eval_to_bool(skip, &err, NULL, FALSE);
16163 curwin->w_cursor = save_pos;
16164 if (err)
16165 {
16166 /* Evaluating {skip} caused an error, break here. */
16167 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016168 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016169 break;
16170 }
16171 if (r)
16172 continue;
16173 }
16174
16175 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16176 {
16177 /* Found end when searching backwards or start when searching
16178 * forward: nested pair. */
16179 ++nest;
16180 pat = pat2; /* nested, don't search for middle */
16181 }
16182 else
16183 {
16184 /* Found end when searching forward or start when searching
16185 * backward: end of (nested) pair; or found middle in outer pair. */
16186 if (--nest == 1)
16187 pat = pat3; /* outer level, search for middle */
16188 }
16189
16190 if (nest == 0)
16191 {
16192 /* Found the match: return matchcount or line number. */
16193 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016194 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016195 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016196 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016197 if (flags & SP_SETPCMARK)
16198 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016199 curwin->w_cursor = pos;
16200 if (!(flags & SP_REPEAT))
16201 break;
16202 nest = 1; /* search for next unmatched */
16203 }
16204 }
16205
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016206 if (match_pos != NULL)
16207 {
16208 /* Store the match cursor position */
16209 match_pos->lnum = curwin->w_cursor.lnum;
16210 match_pos->col = curwin->w_cursor.col + 1;
16211 }
16212
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016214 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016215 curwin->w_cursor = save_cursor;
16216
16217theend:
16218 vim_free(pat2);
16219 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016220 if (p_cpo == empty_option)
16221 p_cpo = save_cpo;
16222 else
16223 /* Darn, evaluating the {skip} expression changed the value. */
16224 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016225
16226 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016227}
16228
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016229/*
16230 * "searchpos()" function
16231 */
16232 static void
16233f_searchpos(argvars, rettv)
16234 typval_T *argvars;
16235 typval_T *rettv;
16236{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016237 pos_T match_pos;
16238 int lnum = 0;
16239 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016240 int n;
16241 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016242
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016243 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016244 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016245
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016246 n = search_cmn(argvars, &match_pos, &flags);
16247 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016248 {
16249 lnum = match_pos.lnum;
16250 col = match_pos.col;
16251 }
16252
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016253 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16254 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016255 if (flags & SP_SUBPAT)
16256 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016257}
16258
16259
Bram Moolenaar0d660222005-01-07 21:51:51 +000016260 static void
16261f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016262 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016263 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016265#ifdef FEAT_CLIENTSERVER
16266 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016267 char_u *server = get_tv_string_chk(&argvars[0]);
16268 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269
Bram Moolenaar0d660222005-01-07 21:51:51 +000016270 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016271 if (server == NULL || reply == NULL)
16272 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016273 if (check_restricted() || check_secure())
16274 return;
16275# ifdef FEAT_X11
16276 if (check_connection() == FAIL)
16277 return;
16278# endif
16279
16280 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016282 EMSG(_("E258: Unable to send to client"));
16283 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016284 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016285 rettv->vval.v_number = 0;
16286#else
16287 rettv->vval.v_number = -1;
16288#endif
16289}
16290
Bram Moolenaar0d660222005-01-07 21:51:51 +000016291 static void
16292f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016293 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016294 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016295{
16296 char_u *r = NULL;
16297
16298#ifdef FEAT_CLIENTSERVER
16299# ifdef WIN32
16300 r = serverGetVimNames();
16301# else
16302 make_connection();
16303 if (X_DISPLAY != NULL)
16304 r = serverGetVimNames(X_DISPLAY);
16305# endif
16306#endif
16307 rettv->v_type = VAR_STRING;
16308 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016309}
16310
16311/*
16312 * "setbufvar()" function
16313 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016315f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016316 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016317 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318{
16319 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016320 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016322 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016323 char_u nbuf[NUMBUFLEN];
16324
16325 if (check_restricted() || check_secure())
16326 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016327 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16328 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016329 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016330 varp = &argvars[2];
16331
16332 if (buf != NULL && varname != NULL && varp != NULL)
16333 {
16334 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336
16337 if (*varname == '&')
16338 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016339 long numval;
16340 char_u *strval;
16341 int error = FALSE;
16342
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016344 numval = get_tv_number_chk(varp, &error);
16345 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016346 if (!error && strval != NULL)
16347 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 }
16349 else
16350 {
16351 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16352 if (bufvarname != NULL)
16353 {
16354 STRCPY(bufvarname, "b:");
16355 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016356 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016357 vim_free(bufvarname);
16358 }
16359 }
16360
16361 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016362 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016364}
16365
16366/*
16367 * "setcmdpos()" function
16368 */
16369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016370f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016371 typval_T *argvars;
16372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016373{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016374 int pos = (int)get_tv_number(&argvars[0]) - 1;
16375
16376 if (pos >= 0)
16377 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378}
16379
16380/*
16381 * "setline()" function
16382 */
16383 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016384f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016385 typval_T *argvars;
16386 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016387{
16388 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016389 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016390 list_T *l = NULL;
16391 listitem_T *li = NULL;
16392 long added = 0;
16393 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016394
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016395 lnum = get_tv_lnum(&argvars[0]);
16396 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016398 l = argvars[1].vval.v_list;
16399 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016400 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016401 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016402 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016403
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016404 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016405 for (;;)
16406 {
16407 if (l != NULL)
16408 {
16409 /* list argument, get next string */
16410 if (li == NULL)
16411 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016412 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016413 li = li->li_next;
16414 }
16415
16416 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016417 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016418 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016419
16420 /* When coming here from Insert mode, sync undo, so that this can be
16421 * undone separately from what was previously inserted. */
16422 if (u_sync_once == 2)
16423 {
16424 u_sync_once = 1; /* notify that u_sync() was called */
16425 u_sync(TRUE);
16426 }
16427
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016428 if (lnum <= curbuf->b_ml.ml_line_count)
16429 {
16430 /* existing line, replace it */
16431 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16432 {
16433 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016434 if (lnum == curwin->w_cursor.lnum)
16435 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016436 rettv->vval.v_number = 0; /* OK */
16437 }
16438 }
16439 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16440 {
16441 /* lnum is one past the last line, append the line */
16442 ++added;
16443 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16444 rettv->vval.v_number = 0; /* OK */
16445 }
16446
16447 if (l == NULL) /* only one string argument */
16448 break;
16449 ++lnum;
16450 }
16451
16452 if (added > 0)
16453 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016454}
16455
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016456static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16457
Bram Moolenaar071d4272004-06-13 20:20:40 +000016458/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016459 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016460 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016461 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016462set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016463 win_T *wp UNUSED;
16464 typval_T *list_arg UNUSED;
16465 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016466 typval_T *rettv;
16467{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016468#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016469 char_u *act;
16470 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016471#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016472
Bram Moolenaar2641f772005-03-25 21:58:17 +000016473 rettv->vval.v_number = -1;
16474
16475#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016476 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016477 EMSG(_(e_listreq));
16478 else
16479 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016480 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016481
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016482 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016483 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016484 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016485 if (act == NULL)
16486 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016487 if (*act == 'a' || *act == 'r')
16488 action = *act;
16489 }
16490
Bram Moolenaar81484f42012-12-05 15:16:47 +010016491 if (l != NULL && set_errorlist(wp, l, action,
16492 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016493 rettv->vval.v_number = 0;
16494 }
16495#endif
16496}
16497
16498/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016499 * "setloclist()" function
16500 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016501 static void
16502f_setloclist(argvars, rettv)
16503 typval_T *argvars;
16504 typval_T *rettv;
16505{
16506 win_T *win;
16507
16508 rettv->vval.v_number = -1;
16509
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016510 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016511 if (win != NULL)
16512 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16513}
16514
16515/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016516 * "setmatches()" function
16517 */
16518 static void
16519f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016520 typval_T *argvars UNUSED;
16521 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016522{
16523#ifdef FEAT_SEARCH_EXTRA
16524 list_T *l;
16525 listitem_T *li;
16526 dict_T *d;
16527
16528 rettv->vval.v_number = -1;
16529 if (argvars[0].v_type != VAR_LIST)
16530 {
16531 EMSG(_(e_listreq));
16532 return;
16533 }
16534 if ((l = argvars[0].vval.v_list) != NULL)
16535 {
16536
16537 /* To some extent make sure that we are dealing with a list from
16538 * "getmatches()". */
16539 li = l->lv_first;
16540 while (li != NULL)
16541 {
16542 if (li->li_tv.v_type != VAR_DICT
16543 || (d = li->li_tv.vval.v_dict) == NULL)
16544 {
16545 EMSG(_(e_invarg));
16546 return;
16547 }
16548 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16549 && dict_find(d, (char_u *)"pattern", -1) != NULL
16550 && dict_find(d, (char_u *)"priority", -1) != NULL
16551 && dict_find(d, (char_u *)"id", -1) != NULL))
16552 {
16553 EMSG(_(e_invarg));
16554 return;
16555 }
16556 li = li->li_next;
16557 }
16558
16559 clear_matches(curwin);
16560 li = l->lv_first;
16561 while (li != NULL)
16562 {
16563 d = li->li_tv.vval.v_dict;
16564 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16565 get_dict_string(d, (char_u *)"pattern", FALSE),
16566 (int)get_dict_number(d, (char_u *)"priority"),
16567 (int)get_dict_number(d, (char_u *)"id"));
16568 li = li->li_next;
16569 }
16570 rettv->vval.v_number = 0;
16571 }
16572#endif
16573}
16574
16575/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016576 * "setpos()" function
16577 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016578 static void
16579f_setpos(argvars, rettv)
16580 typval_T *argvars;
16581 typval_T *rettv;
16582{
16583 pos_T pos;
16584 int fnum;
16585 char_u *name;
16586
Bram Moolenaar08250432008-02-13 11:42:46 +000016587 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016588 name = get_tv_string_chk(argvars);
16589 if (name != NULL)
16590 {
16591 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16592 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016593 if (--pos.col < 0)
16594 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016595 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016596 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016597 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016598 if (fnum == curbuf->b_fnum)
16599 {
16600 curwin->w_cursor = pos;
16601 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016602 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016603 }
16604 else
16605 EMSG(_(e_invarg));
16606 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016607 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16608 {
16609 /* set mark */
16610 if (setmark_pos(name[1], &pos, fnum) == OK)
16611 rettv->vval.v_number = 0;
16612 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016613 else
16614 EMSG(_(e_invarg));
16615 }
16616 }
16617}
16618
16619/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016620 * "setqflist()" function
16621 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016622 static void
16623f_setqflist(argvars, rettv)
16624 typval_T *argvars;
16625 typval_T *rettv;
16626{
16627 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16628}
16629
16630/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016631 * "setreg()" function
16632 */
16633 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016634f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016635 typval_T *argvars;
16636 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016637{
16638 int regname;
16639 char_u *strregname;
16640 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016641 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016642 int append;
16643 char_u yank_type;
16644 long block_len;
16645
16646 block_len = -1;
16647 yank_type = MAUTO;
16648 append = FALSE;
16649
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016650 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016651 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016652
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016653 if (strregname == NULL)
16654 return; /* type error; errmsg already given */
16655 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016656 if (regname == 0 || regname == '@')
16657 regname = '"';
16658 else if (regname == '=')
16659 return;
16660
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016661 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016663 stropt = get_tv_string_chk(&argvars[2]);
16664 if (stropt == NULL)
16665 return; /* type error */
16666 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016667 switch (*stropt)
16668 {
16669 case 'a': case 'A': /* append */
16670 append = TRUE;
16671 break;
16672 case 'v': case 'c': /* character-wise selection */
16673 yank_type = MCHAR;
16674 break;
16675 case 'V': case 'l': /* line-wise selection */
16676 yank_type = MLINE;
16677 break;
16678#ifdef FEAT_VISUAL
16679 case 'b': case Ctrl_V: /* block-wise selection */
16680 yank_type = MBLOCK;
16681 if (VIM_ISDIGIT(stropt[1]))
16682 {
16683 ++stropt;
16684 block_len = getdigits(&stropt) - 1;
16685 --stropt;
16686 }
16687 break;
16688#endif
16689 }
16690 }
16691
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016692 strval = get_tv_string_chk(&argvars[1]);
16693 if (strval != NULL)
16694 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016695 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016696 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016697}
16698
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016699/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016700 * "settabvar()" function
16701 */
16702 static void
16703f_settabvar(argvars, rettv)
16704 typval_T *argvars;
16705 typval_T *rettv;
16706{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016707#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016708 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016709 tabpage_T *tp;
16710#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016711 char_u *varname, *tabvarname;
16712 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016713
16714 rettv->vval.v_number = 0;
16715
16716 if (check_restricted() || check_secure())
16717 return;
16718
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016719#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016720 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016721#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016722 varname = get_tv_string_chk(&argvars[1]);
16723 varp = &argvars[2];
16724
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016725 if (varname != NULL && varp != NULL
16726#ifdef FEAT_WINDOWS
16727 && tp != NULL
16728#endif
16729 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016730 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016731#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016732 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016733 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016734#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016735
16736 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16737 if (tabvarname != NULL)
16738 {
16739 STRCPY(tabvarname, "t:");
16740 STRCPY(tabvarname + 2, varname);
16741 set_var(tabvarname, varp, TRUE);
16742 vim_free(tabvarname);
16743 }
16744
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016745#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016746 /* Restore current tabpage */
16747 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016748 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016749#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016750 }
16751}
16752
16753/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016754 * "settabwinvar()" function
16755 */
16756 static void
16757f_settabwinvar(argvars, rettv)
16758 typval_T *argvars;
16759 typval_T *rettv;
16760{
16761 setwinvar(argvars, rettv, 1);
16762}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016763
16764/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016765 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016767 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016768f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016769 typval_T *argvars;
16770 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016771{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016772 setwinvar(argvars, rettv, 0);
16773}
16774
16775/*
16776 * "setwinvar()" and "settabwinvar()" functions
16777 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016778
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016779 static void
16780setwinvar(argvars, rettv, off)
16781 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016782 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016783 int off;
16784{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785 win_T *win;
16786#ifdef FEAT_WINDOWS
16787 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016788 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789#endif
16790 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016791 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016793 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016794
16795 if (check_restricted() || check_secure())
16796 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016797
16798#ifdef FEAT_WINDOWS
16799 if (off == 1)
16800 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16801 else
16802 tp = curtab;
16803#endif
16804 win = find_win_by_nr(&argvars[off], tp);
16805 varname = get_tv_string_chk(&argvars[off + 1]);
16806 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807
16808 if (win != NULL && varname != NULL && varp != NULL)
16809 {
16810#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016811 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016812 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813#endif
16814
16815 if (*varname == '&')
16816 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016817 long numval;
16818 char_u *strval;
16819 int error = FALSE;
16820
Bram Moolenaar071d4272004-06-13 20:20:40 +000016821 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016822 numval = get_tv_number_chk(varp, &error);
16823 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016824 if (!error && strval != NULL)
16825 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016826 }
16827 else
16828 {
16829 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16830 if (winvarname != NULL)
16831 {
16832 STRCPY(winvarname, "w:");
16833 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016834 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016835 vim_free(winvarname);
16836 }
16837 }
16838
16839#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016840 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016841#endif
16842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843}
16844
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016845#ifdef FEAT_CRYPT
16846/*
16847 * "sha256({string})" function
16848 */
16849 static void
16850f_sha256(argvars, rettv)
16851 typval_T *argvars;
16852 typval_T *rettv;
16853{
16854 char_u *p;
16855
16856 p = get_tv_string(&argvars[0]);
16857 rettv->vval.v_string = vim_strsave(
16858 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16859 rettv->v_type = VAR_STRING;
16860}
16861#endif /* FEAT_CRYPT */
16862
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016864 * "shellescape({string})" function
16865 */
16866 static void
16867f_shellescape(argvars, rettv)
16868 typval_T *argvars;
16869 typval_T *rettv;
16870{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016871 rettv->vval.v_string = vim_strsave_shellescape(
16872 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016873 rettv->v_type = VAR_STRING;
16874}
16875
16876/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016877 * shiftwidth() function
16878 */
16879 static void
16880f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016881 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016882 typval_T *rettv;
16883{
16884 rettv->vval.v_number = get_sw_value();
16885}
16886
16887/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016888 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889 */
16890 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016891f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016892 typval_T *argvars;
16893 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016895 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896
Bram Moolenaar0d660222005-01-07 21:51:51 +000016897 p = get_tv_string(&argvars[0]);
16898 rettv->vval.v_string = vim_strsave(p);
16899 simplify_filename(rettv->vval.v_string); /* simplify in place */
16900 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016901}
16902
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016903#ifdef FEAT_FLOAT
16904/*
16905 * "sin()" function
16906 */
16907 static void
16908f_sin(argvars, rettv)
16909 typval_T *argvars;
16910 typval_T *rettv;
16911{
16912 float_T f;
16913
16914 rettv->v_type = VAR_FLOAT;
16915 if (get_float_arg(argvars, &f) == OK)
16916 rettv->vval.v_float = sin(f);
16917 else
16918 rettv->vval.v_float = 0.0;
16919}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016920
16921/*
16922 * "sinh()" function
16923 */
16924 static void
16925f_sinh(argvars, rettv)
16926 typval_T *argvars;
16927 typval_T *rettv;
16928{
16929 float_T f;
16930
16931 rettv->v_type = VAR_FLOAT;
16932 if (get_float_arg(argvars, &f) == OK)
16933 rettv->vval.v_float = sinh(f);
16934 else
16935 rettv->vval.v_float = 0.0;
16936}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016937#endif
16938
Bram Moolenaar0d660222005-01-07 21:51:51 +000016939static int
16940#ifdef __BORLANDC__
16941 _RTLENTRYF
16942#endif
16943 item_compare __ARGS((const void *s1, const void *s2));
16944static int
16945#ifdef __BORLANDC__
16946 _RTLENTRYF
16947#endif
16948 item_compare2 __ARGS((const void *s1, const void *s2));
16949
16950static int item_compare_ic;
16951static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016952static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016953static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016954#define ITEM_COMPARE_FAIL 999
16955
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016957 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016958 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016959 static int
16960#ifdef __BORLANDC__
16961_RTLENTRYF
16962#endif
16963item_compare(s1, s2)
16964 const void *s1;
16965 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016967 char_u *p1, *p2;
16968 char_u *tofree1, *tofree2;
16969 int res;
16970 char_u numbuf1[NUMBUFLEN];
16971 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016972
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016973 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16974 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016975 if (p1 == NULL)
16976 p1 = (char_u *)"";
16977 if (p2 == NULL)
16978 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979 if (item_compare_ic)
16980 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016981 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016982 res = STRCMP(p1, p2);
16983 vim_free(tofree1);
16984 vim_free(tofree2);
16985 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986}
16987
16988 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000016989#ifdef __BORLANDC__
16990_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000016991#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992item_compare2(s1, s2)
16993 const void *s1;
16994 const void *s2;
16995{
16996 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000016997 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016998 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000016999 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017000
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017001 /* shortcut after failure in previous call; compare all items equal */
17002 if (item_compare_func_err)
17003 return 0;
17004
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017005 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17006 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017007 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17008 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017009
17010 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017011 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017012 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17013 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017014 clear_tv(&argv[0]);
17015 clear_tv(&argv[1]);
17016
17017 if (res == FAIL)
17018 res = ITEM_COMPARE_FAIL;
17019 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017020 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17021 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017022 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017023 clear_tv(&rettv);
17024 return res;
17025}
17026
17027/*
17028 * "sort({list})" function
17029 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017030 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017031f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017032 typval_T *argvars;
17033 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017034{
Bram Moolenaar33570922005-01-25 22:26:29 +000017035 list_T *l;
17036 listitem_T *li;
17037 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017038 long len;
17039 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040
Bram Moolenaar0d660222005-01-07 21:51:51 +000017041 if (argvars[0].v_type != VAR_LIST)
17042 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043 else
17044 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017045 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017046 if (l == NULL || tv_check_lock(l->lv_lock,
17047 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017048 return;
17049 rettv->vval.v_list = l;
17050 rettv->v_type = VAR_LIST;
17051 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052
Bram Moolenaar0d660222005-01-07 21:51:51 +000017053 len = list_len(l);
17054 if (len <= 1)
17055 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057 item_compare_ic = FALSE;
17058 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017059 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017060 if (argvars[1].v_type != VAR_UNKNOWN)
17061 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017062 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017063 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017064 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017065 else
17066 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017067 int error = FALSE;
17068
17069 i = get_tv_number_chk(&argvars[1], &error);
17070 if (error)
17071 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017072 if (i == 1)
17073 item_compare_ic = TRUE;
17074 else
17075 item_compare_func = get_tv_string(&argvars[1]);
17076 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017077
17078 if (argvars[2].v_type != VAR_UNKNOWN)
17079 {
17080 /* optional third argument: {dict} */
17081 if (argvars[2].v_type != VAR_DICT)
17082 {
17083 EMSG(_(e_dictreq));
17084 return;
17085 }
17086 item_compare_selfdict = argvars[2].vval.v_dict;
17087 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017088 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089
Bram Moolenaar0d660222005-01-07 21:51:51 +000017090 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017091 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017092 if (ptrs == NULL)
17093 return;
17094 i = 0;
17095 for (li = l->lv_first; li != NULL; li = li->li_next)
17096 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017098 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017099 /* test the compare function */
17100 if (item_compare_func != NULL
17101 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17102 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017103 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017104 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017105 {
17106 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017107 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017108 item_compare_func == NULL ? item_compare : item_compare2);
17109
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017110 if (!item_compare_func_err)
17111 {
17112 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017113 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017114 l->lv_len = 0;
17115 for (i = 0; i < len; ++i)
17116 list_append(l, ptrs[i]);
17117 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017118 }
17119
17120 vim_free(ptrs);
17121 }
17122}
17123
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017124/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017125 * "soundfold({word})" function
17126 */
17127 static void
17128f_soundfold(argvars, rettv)
17129 typval_T *argvars;
17130 typval_T *rettv;
17131{
17132 char_u *s;
17133
17134 rettv->v_type = VAR_STRING;
17135 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017136#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017137 rettv->vval.v_string = eval_soundfold(s);
17138#else
17139 rettv->vval.v_string = vim_strsave(s);
17140#endif
17141}
17142
17143/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017144 * "spellbadword()" function
17145 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017146 static void
17147f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017148 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017149 typval_T *rettv;
17150{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017151 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017152 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017153 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017154
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017155 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017156 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017157
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017158#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017159 if (argvars[0].v_type == VAR_UNKNOWN)
17160 {
17161 /* Find the start and length of the badly spelled word. */
17162 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17163 if (len != 0)
17164 word = ml_get_cursor();
17165 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017166 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017167 {
17168 char_u *str = get_tv_string_chk(&argvars[0]);
17169 int capcol = -1;
17170
17171 if (str != NULL)
17172 {
17173 /* Check the argument for spelling. */
17174 while (*str != NUL)
17175 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017176 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017177 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017178 {
17179 word = str;
17180 break;
17181 }
17182 str += len;
17183 }
17184 }
17185 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017186#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017187
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017188 list_append_string(rettv->vval.v_list, word, len);
17189 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017190 attr == HLF_SPB ? "bad" :
17191 attr == HLF_SPR ? "rare" :
17192 attr == HLF_SPL ? "local" :
17193 attr == HLF_SPC ? "caps" :
17194 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017195}
17196
17197/*
17198 * "spellsuggest()" function
17199 */
17200 static void
17201f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017202 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017203 typval_T *rettv;
17204{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017205#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017206 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017207 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017208 int maxcount;
17209 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017210 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017211 listitem_T *li;
17212 int need_capital = FALSE;
17213#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017214
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017215 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017216 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017217
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017218#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017219 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017220 {
17221 str = get_tv_string(&argvars[0]);
17222 if (argvars[1].v_type != VAR_UNKNOWN)
17223 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017224 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017225 if (maxcount <= 0)
17226 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017227 if (argvars[2].v_type != VAR_UNKNOWN)
17228 {
17229 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17230 if (typeerr)
17231 return;
17232 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017233 }
17234 else
17235 maxcount = 25;
17236
Bram Moolenaar4770d092006-01-12 23:22:24 +000017237 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017238
17239 for (i = 0; i < ga.ga_len; ++i)
17240 {
17241 str = ((char_u **)ga.ga_data)[i];
17242
17243 li = listitem_alloc();
17244 if (li == NULL)
17245 vim_free(str);
17246 else
17247 {
17248 li->li_tv.v_type = VAR_STRING;
17249 li->li_tv.v_lock = 0;
17250 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017251 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017252 }
17253 }
17254 ga_clear(&ga);
17255 }
17256#endif
17257}
17258
Bram Moolenaar0d660222005-01-07 21:51:51 +000017259 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017260f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017261 typval_T *argvars;
17262 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017263{
17264 char_u *str;
17265 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017266 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017267 regmatch_T regmatch;
17268 char_u patbuf[NUMBUFLEN];
17269 char_u *save_cpo;
17270 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017271 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017272 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017273 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017274
17275 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17276 save_cpo = p_cpo;
17277 p_cpo = (char_u *)"";
17278
17279 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017280 if (argvars[1].v_type != VAR_UNKNOWN)
17281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017282 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17283 if (pat == NULL)
17284 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017285 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017286 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017287 }
17288 if (pat == NULL || *pat == NUL)
17289 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017290
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017291 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017293 if (typeerr)
17294 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017295
Bram Moolenaar0d660222005-01-07 21:51:51 +000017296 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17297 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017298 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017299 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017300 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017301 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017302 if (*str == NUL)
17303 match = FALSE; /* empty item at the end */
17304 else
17305 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017306 if (match)
17307 end = regmatch.startp[0];
17308 else
17309 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017310 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17311 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017312 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017313 if (list_append_string(rettv->vval.v_list, str,
17314 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017315 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017316 }
17317 if (!match)
17318 break;
17319 /* Advance to just after the match. */
17320 if (regmatch.endp[0] > str)
17321 col = 0;
17322 else
17323 {
17324 /* Don't get stuck at the same match. */
17325#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017326 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017327#else
17328 col = 1;
17329#endif
17330 }
17331 str = regmatch.endp[0];
17332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017333
Bram Moolenaar473de612013-06-08 18:19:48 +020017334 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336
Bram Moolenaar0d660222005-01-07 21:51:51 +000017337 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017338}
17339
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017340#ifdef FEAT_FLOAT
17341/*
17342 * "sqrt()" function
17343 */
17344 static void
17345f_sqrt(argvars, rettv)
17346 typval_T *argvars;
17347 typval_T *rettv;
17348{
17349 float_T f;
17350
17351 rettv->v_type = VAR_FLOAT;
17352 if (get_float_arg(argvars, &f) == OK)
17353 rettv->vval.v_float = sqrt(f);
17354 else
17355 rettv->vval.v_float = 0.0;
17356}
17357
17358/*
17359 * "str2float()" function
17360 */
17361 static void
17362f_str2float(argvars, rettv)
17363 typval_T *argvars;
17364 typval_T *rettv;
17365{
17366 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17367
17368 if (*p == '+')
17369 p = skipwhite(p + 1);
17370 (void)string2float(p, &rettv->vval.v_float);
17371 rettv->v_type = VAR_FLOAT;
17372}
17373#endif
17374
Bram Moolenaar2c932302006-03-18 21:42:09 +000017375/*
17376 * "str2nr()" function
17377 */
17378 static void
17379f_str2nr(argvars, rettv)
17380 typval_T *argvars;
17381 typval_T *rettv;
17382{
17383 int base = 10;
17384 char_u *p;
17385 long n;
17386
17387 if (argvars[1].v_type != VAR_UNKNOWN)
17388 {
17389 base = get_tv_number(&argvars[1]);
17390 if (base != 8 && base != 10 && base != 16)
17391 {
17392 EMSG(_(e_invarg));
17393 return;
17394 }
17395 }
17396
17397 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017398 if (*p == '+')
17399 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017400 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17401 rettv->vval.v_number = n;
17402}
17403
Bram Moolenaar071d4272004-06-13 20:20:40 +000017404#ifdef HAVE_STRFTIME
17405/*
17406 * "strftime({format}[, {time}])" function
17407 */
17408 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017409f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017410 typval_T *argvars;
17411 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412{
17413 char_u result_buf[256];
17414 struct tm *curtime;
17415 time_t seconds;
17416 char_u *p;
17417
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017418 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017420 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017421 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 seconds = time(NULL);
17423 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017424 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017425 curtime = localtime(&seconds);
17426 /* MSVC returns NULL for an invalid value of seconds. */
17427 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017428 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429 else
17430 {
17431# ifdef FEAT_MBYTE
17432 vimconv_T conv;
17433 char_u *enc;
17434
17435 conv.vc_type = CONV_NONE;
17436 enc = enc_locale();
17437 convert_setup(&conv, p_enc, enc);
17438 if (conv.vc_type != CONV_NONE)
17439 p = string_convert(&conv, p, NULL);
17440# endif
17441 if (p != NULL)
17442 (void)strftime((char *)result_buf, sizeof(result_buf),
17443 (char *)p, curtime);
17444 else
17445 result_buf[0] = NUL;
17446
17447# ifdef FEAT_MBYTE
17448 if (conv.vc_type != CONV_NONE)
17449 vim_free(p);
17450 convert_setup(&conv, enc, p_enc);
17451 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017452 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017453 else
17454# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017455 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017456
17457# ifdef FEAT_MBYTE
17458 /* Release conversion descriptors */
17459 convert_setup(&conv, NULL, NULL);
17460 vim_free(enc);
17461# endif
17462 }
17463}
17464#endif
17465
17466/*
17467 * "stridx()" function
17468 */
17469 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017470f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017471 typval_T *argvars;
17472 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473{
17474 char_u buf[NUMBUFLEN];
17475 char_u *needle;
17476 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017477 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017478 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017479 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017481 needle = get_tv_string_chk(&argvars[1]);
17482 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017483 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017484 if (needle == NULL || haystack == NULL)
17485 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017486
Bram Moolenaar33570922005-01-25 22:26:29 +000017487 if (argvars[2].v_type != VAR_UNKNOWN)
17488 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017489 int error = FALSE;
17490
17491 start_idx = get_tv_number_chk(&argvars[2], &error);
17492 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017493 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017494 if (start_idx >= 0)
17495 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017496 }
17497
17498 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17499 if (pos != NULL)
17500 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501}
17502
17503/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017504 * "string()" function
17505 */
17506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017507f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017508 typval_T *argvars;
17509 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017510{
17511 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017512 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017513
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017514 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017515 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017516 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017517 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017518 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519}
17520
17521/*
17522 * "strlen()" function
17523 */
17524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017525f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017526 typval_T *argvars;
17527 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017528{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017529 rettv->vval.v_number = (varnumber_T)(STRLEN(
17530 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017531}
17532
17533/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017534 * "strchars()" function
17535 */
17536 static void
17537f_strchars(argvars, rettv)
17538 typval_T *argvars;
17539 typval_T *rettv;
17540{
17541 char_u *s = get_tv_string(&argvars[0]);
17542#ifdef FEAT_MBYTE
17543 varnumber_T len = 0;
17544
17545 while (*s != NUL)
17546 {
17547 mb_cptr2char_adv(&s);
17548 ++len;
17549 }
17550 rettv->vval.v_number = len;
17551#else
17552 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17553#endif
17554}
17555
17556/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017557 * "strdisplaywidth()" function
17558 */
17559 static void
17560f_strdisplaywidth(argvars, rettv)
17561 typval_T *argvars;
17562 typval_T *rettv;
17563{
17564 char_u *s = get_tv_string(&argvars[0]);
17565 int col = 0;
17566
17567 if (argvars[1].v_type != VAR_UNKNOWN)
17568 col = get_tv_number(&argvars[1]);
17569
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017570 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017571}
17572
17573/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017574 * "strwidth()" function
17575 */
17576 static void
17577f_strwidth(argvars, rettv)
17578 typval_T *argvars;
17579 typval_T *rettv;
17580{
17581 char_u *s = get_tv_string(&argvars[0]);
17582
17583 rettv->vval.v_number = (varnumber_T)(
17584#ifdef FEAT_MBYTE
17585 mb_string2cells(s, -1)
17586#else
17587 STRLEN(s)
17588#endif
17589 );
17590}
17591
17592/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017593 * "strpart()" function
17594 */
17595 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017596f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017597 typval_T *argvars;
17598 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017599{
17600 char_u *p;
17601 int n;
17602 int len;
17603 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017604 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017606 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607 slen = (int)STRLEN(p);
17608
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017609 n = get_tv_number_chk(&argvars[1], &error);
17610 if (error)
17611 len = 0;
17612 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017613 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017614 else
17615 len = slen - n; /* default len: all bytes that are available. */
17616
17617 /*
17618 * Only return the overlap between the specified part and the actual
17619 * string.
17620 */
17621 if (n < 0)
17622 {
17623 len += n;
17624 n = 0;
17625 }
17626 else if (n > slen)
17627 n = slen;
17628 if (len < 0)
17629 len = 0;
17630 else if (n + len > slen)
17631 len = slen - n;
17632
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017633 rettv->v_type = VAR_STRING;
17634 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017635}
17636
17637/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017638 * "strridx()" function
17639 */
17640 static void
17641f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017642 typval_T *argvars;
17643 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017644{
17645 char_u buf[NUMBUFLEN];
17646 char_u *needle;
17647 char_u *haystack;
17648 char_u *rest;
17649 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017650 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017651
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017652 needle = get_tv_string_chk(&argvars[1]);
17653 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017654
17655 rettv->vval.v_number = -1;
17656 if (needle == NULL || haystack == NULL)
17657 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017658
17659 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017660 if (argvars[2].v_type != VAR_UNKNOWN)
17661 {
17662 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017663 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017664 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017665 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017666 }
17667 else
17668 end_idx = haystack_len;
17669
Bram Moolenaar0d660222005-01-07 21:51:51 +000017670 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017671 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017672 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017673 lastmatch = haystack + end_idx;
17674 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017675 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017676 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017677 for (rest = haystack; *rest != '\0'; ++rest)
17678 {
17679 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017680 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017681 break;
17682 lastmatch = rest;
17683 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017684 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017685
17686 if (lastmatch == NULL)
17687 rettv->vval.v_number = -1;
17688 else
17689 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17690}
17691
17692/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017693 * "strtrans()" function
17694 */
17695 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017696f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017697 typval_T *argvars;
17698 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017700 rettv->v_type = VAR_STRING;
17701 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017702}
17703
17704/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017705 * "submatch()" function
17706 */
17707 static void
17708f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017709 typval_T *argvars;
17710 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017711{
17712 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017713 rettv->vval.v_string =
17714 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017715}
17716
17717/*
17718 * "substitute()" function
17719 */
17720 static void
17721f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017722 typval_T *argvars;
17723 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017724{
17725 char_u patbuf[NUMBUFLEN];
17726 char_u subbuf[NUMBUFLEN];
17727 char_u flagsbuf[NUMBUFLEN];
17728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017729 char_u *str = get_tv_string_chk(&argvars[0]);
17730 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17731 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17732 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17733
Bram Moolenaar0d660222005-01-07 21:51:51 +000017734 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017735 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17736 rettv->vval.v_string = NULL;
17737 else
17738 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017739}
17740
17741/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017742 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017743 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017744 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017745f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017746 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017747 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017748{
17749 int id = 0;
17750#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017751 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752 long col;
17753 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017754 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017755
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017756 lnum = get_tv_lnum(argvars); /* -1 on type error */
17757 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17758 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017760 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017761 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017762 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763#endif
17764
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017765 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766}
17767
17768/*
17769 * "synIDattr(id, what [, mode])" function
17770 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017772f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017773 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017774 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017775{
17776 char_u *p = NULL;
17777#ifdef FEAT_SYN_HL
17778 int id;
17779 char_u *what;
17780 char_u *mode;
17781 char_u modebuf[NUMBUFLEN];
17782 int modec;
17783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017784 id = get_tv_number(&argvars[0]);
17785 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017786 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017787 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017788 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017790 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 modec = 0; /* replace invalid with current */
17792 }
17793 else
17794 {
17795#ifdef FEAT_GUI
17796 if (gui.in_use)
17797 modec = 'g';
17798 else
17799#endif
17800 if (t_colors > 1)
17801 modec = 'c';
17802 else
17803 modec = 't';
17804 }
17805
17806
17807 switch (TOLOWER_ASC(what[0]))
17808 {
17809 case 'b':
17810 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17811 p = highlight_color(id, what, modec);
17812 else /* bold */
17813 p = highlight_has_attr(id, HL_BOLD, modec);
17814 break;
17815
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017816 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817 p = highlight_color(id, what, modec);
17818 break;
17819
17820 case 'i':
17821 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17822 p = highlight_has_attr(id, HL_INVERSE, modec);
17823 else /* italic */
17824 p = highlight_has_attr(id, HL_ITALIC, modec);
17825 break;
17826
17827 case 'n': /* name */
17828 p = get_highlight_name(NULL, id - 1);
17829 break;
17830
17831 case 'r': /* reverse */
17832 p = highlight_has_attr(id, HL_INVERSE, modec);
17833 break;
17834
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017835 case 's':
17836 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17837 p = highlight_color(id, what, modec);
17838 else /* standout */
17839 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840 break;
17841
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017842 case 'u':
17843 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17844 /* underline */
17845 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17846 else
17847 /* undercurl */
17848 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017849 break;
17850 }
17851
17852 if (p != NULL)
17853 p = vim_strsave(p);
17854#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017855 rettv->v_type = VAR_STRING;
17856 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857}
17858
17859/*
17860 * "synIDtrans(id)" function
17861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017863f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017864 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866{
17867 int id;
17868
17869#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017870 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871
17872 if (id > 0)
17873 id = syn_get_final_id(id);
17874 else
17875#endif
17876 id = 0;
17877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017878 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879}
17880
17881/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017882 * "synconcealed(lnum, col)" function
17883 */
17884 static void
17885f_synconcealed(argvars, rettv)
17886 typval_T *argvars UNUSED;
17887 typval_T *rettv;
17888{
17889#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17890 long lnum;
17891 long col;
17892 int syntax_flags = 0;
17893 int cchar;
17894 int matchid = 0;
17895 char_u str[NUMBUFLEN];
17896#endif
17897
17898 rettv->v_type = VAR_LIST;
17899 rettv->vval.v_list = NULL;
17900
17901#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17902 lnum = get_tv_lnum(argvars); /* -1 on type error */
17903 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17904
17905 vim_memset(str, NUL, sizeof(str));
17906
17907 if (rettv_list_alloc(rettv) != FAIL)
17908 {
17909 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17910 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17911 && curwin->w_p_cole > 0)
17912 {
17913 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17914 syntax_flags = get_syntax_info(&matchid);
17915
17916 /* get the conceal character */
17917 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17918 {
17919 cchar = syn_get_sub_char();
17920 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17921 cchar = lcs_conceal;
17922 if (cchar != NUL)
17923 {
17924# ifdef FEAT_MBYTE
17925 if (has_mbyte)
17926 (*mb_char2bytes)(cchar, str);
17927 else
17928# endif
17929 str[0] = cchar;
17930 }
17931 }
17932 }
17933
17934 list_append_number(rettv->vval.v_list,
17935 (syntax_flags & HL_CONCEAL) != 0);
17936 /* -1 to auto-determine strlen */
17937 list_append_string(rettv->vval.v_list, str, -1);
17938 list_append_number(rettv->vval.v_list, matchid);
17939 }
17940#endif
17941}
17942
17943/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017944 * "synstack(lnum, col)" function
17945 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017946 static void
17947f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017948 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017949 typval_T *rettv;
17950{
17951#ifdef FEAT_SYN_HL
17952 long lnum;
17953 long col;
17954 int i;
17955 int id;
17956#endif
17957
17958 rettv->v_type = VAR_LIST;
17959 rettv->vval.v_list = NULL;
17960
17961#ifdef FEAT_SYN_HL
17962 lnum = get_tv_lnum(argvars); /* -1 on type error */
17963 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17964
17965 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017966 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017967 && rettv_list_alloc(rettv) != FAIL)
17968 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017969 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017970 for (i = 0; ; ++i)
17971 {
17972 id = syn_get_stack_item(i);
17973 if (id < 0)
17974 break;
17975 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17976 break;
17977 }
17978 }
17979#endif
17980}
17981
17982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017983 * "system()" function
17984 */
17985 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017986f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017987 typval_T *argvars;
17988 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017989{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017990 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017991 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000017992 char_u *infile = NULL;
17993 char_u buf[NUMBUFLEN];
17994 int err = FALSE;
17995 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017996
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017997 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000017998 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000017999
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018000 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018001 {
18002 /*
18003 * Write the string to a temp file, to be used for input of the shell
18004 * command.
18005 */
18006 if ((infile = vim_tempname('i')) == NULL)
18007 {
18008 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018009 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018010 }
18011
18012 fd = mch_fopen((char *)infile, WRITEBIN);
18013 if (fd == NULL)
18014 {
18015 EMSG2(_(e_notopen), infile);
18016 goto done;
18017 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018018 p = get_tv_string_buf_chk(&argvars[1], buf);
18019 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018020 {
18021 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018022 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018023 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018024 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18025 err = TRUE;
18026 if (fclose(fd) != 0)
18027 err = TRUE;
18028 if (err)
18029 {
18030 EMSG(_("E677: Error writing temp file"));
18031 goto done;
18032 }
18033 }
18034
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018035 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18036 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018037
Bram Moolenaar071d4272004-06-13 20:20:40 +000018038#ifdef USE_CR
18039 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018040 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018041 {
18042 char_u *s;
18043
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018044 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018045 {
18046 if (*s == CAR)
18047 *s = NL;
18048 }
18049 }
18050#else
18051# ifdef USE_CRNL
18052 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018053 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018054 {
18055 char_u *s, *d;
18056
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018057 d = res;
18058 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059 {
18060 if (s[0] == CAR && s[1] == NL)
18061 ++s;
18062 *d++ = *s;
18063 }
18064 *d = NUL;
18065 }
18066# endif
18067#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018068
18069done:
18070 if (infile != NULL)
18071 {
18072 mch_remove(infile);
18073 vim_free(infile);
18074 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018075 rettv->v_type = VAR_STRING;
18076 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077}
18078
18079/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018080 * "tabpagebuflist()" function
18081 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018082 static void
18083f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018084 typval_T *argvars UNUSED;
18085 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018086{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018087#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018088 tabpage_T *tp;
18089 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018090
18091 if (argvars[0].v_type == VAR_UNKNOWN)
18092 wp = firstwin;
18093 else
18094 {
18095 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18096 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018097 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018098 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018099 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018100 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018101 for (; wp != NULL; wp = wp->w_next)
18102 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018103 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018104 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018105 }
18106#endif
18107}
18108
18109
18110/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018111 * "tabpagenr()" function
18112 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018113 static void
18114f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018115 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018116 typval_T *rettv;
18117{
18118 int nr = 1;
18119#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018120 char_u *arg;
18121
18122 if (argvars[0].v_type != VAR_UNKNOWN)
18123 {
18124 arg = get_tv_string_chk(&argvars[0]);
18125 nr = 0;
18126 if (arg != NULL)
18127 {
18128 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018129 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018130 else
18131 EMSG2(_(e_invexpr2), arg);
18132 }
18133 }
18134 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018135 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018136#endif
18137 rettv->vval.v_number = nr;
18138}
18139
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018140
18141#ifdef FEAT_WINDOWS
18142static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18143
18144/*
18145 * Common code for tabpagewinnr() and winnr().
18146 */
18147 static int
18148get_winnr(tp, argvar)
18149 tabpage_T *tp;
18150 typval_T *argvar;
18151{
18152 win_T *twin;
18153 int nr = 1;
18154 win_T *wp;
18155 char_u *arg;
18156
18157 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18158 if (argvar->v_type != VAR_UNKNOWN)
18159 {
18160 arg = get_tv_string_chk(argvar);
18161 if (arg == NULL)
18162 nr = 0; /* type error; errmsg already given */
18163 else if (STRCMP(arg, "$") == 0)
18164 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18165 else if (STRCMP(arg, "#") == 0)
18166 {
18167 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18168 if (twin == NULL)
18169 nr = 0;
18170 }
18171 else
18172 {
18173 EMSG2(_(e_invexpr2), arg);
18174 nr = 0;
18175 }
18176 }
18177
18178 if (nr > 0)
18179 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18180 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018181 {
18182 if (wp == NULL)
18183 {
18184 /* didn't find it in this tabpage */
18185 nr = 0;
18186 break;
18187 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018188 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018189 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018190 return nr;
18191}
18192#endif
18193
18194/*
18195 * "tabpagewinnr()" function
18196 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018197 static void
18198f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018199 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018200 typval_T *rettv;
18201{
18202 int nr = 1;
18203#ifdef FEAT_WINDOWS
18204 tabpage_T *tp;
18205
18206 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18207 if (tp == NULL)
18208 nr = 0;
18209 else
18210 nr = get_winnr(tp, &argvars[1]);
18211#endif
18212 rettv->vval.v_number = nr;
18213}
18214
18215
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018216/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018217 * "tagfiles()" function
18218 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018219 static void
18220f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018221 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018222 typval_T *rettv;
18223{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018224 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018225 tagname_T tn;
18226 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018227
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018228 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018229 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018230 fname = alloc(MAXPATHL);
18231 if (fname == NULL)
18232 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018233
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018234 for (first = TRUE; ; first = FALSE)
18235 if (get_tagfname(&tn, first, fname) == FAIL
18236 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018237 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018238 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018239 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018240}
18241
18242/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018243 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018244 */
18245 static void
18246f_taglist(argvars, rettv)
18247 typval_T *argvars;
18248 typval_T *rettv;
18249{
18250 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018251
18252 tag_pattern = get_tv_string(&argvars[0]);
18253
18254 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018255 if (*tag_pattern == NUL)
18256 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018257
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018258 if (rettv_list_alloc(rettv) == OK)
18259 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018260}
18261
18262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263 * "tempname()" function
18264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018265 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018266f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018267 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018268 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269{
18270 static int x = 'A';
18271
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018272 rettv->v_type = VAR_STRING;
18273 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018274
18275 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18276 * names. Skip 'I' and 'O', they are used for shell redirection. */
18277 do
18278 {
18279 if (x == 'Z')
18280 x = '0';
18281 else if (x == '9')
18282 x = 'A';
18283 else
18284 {
18285#ifdef EBCDIC
18286 if (x == 'I')
18287 x = 'J';
18288 else if (x == 'R')
18289 x = 'S';
18290 else
18291#endif
18292 ++x;
18293 }
18294 } while (x == 'I' || x == 'O');
18295}
18296
18297/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018298 * "test(list)" function: Just checking the walls...
18299 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018300 static void
18301f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018302 typval_T *argvars UNUSED;
18303 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018304{
18305 /* Used for unit testing. Change the code below to your liking. */
18306#if 0
18307 listitem_T *li;
18308 list_T *l;
18309 char_u *bad, *good;
18310
18311 if (argvars[0].v_type != VAR_LIST)
18312 return;
18313 l = argvars[0].vval.v_list;
18314 if (l == NULL)
18315 return;
18316 li = l->lv_first;
18317 if (li == NULL)
18318 return;
18319 bad = get_tv_string(&li->li_tv);
18320 li = li->li_next;
18321 if (li == NULL)
18322 return;
18323 good = get_tv_string(&li->li_tv);
18324 rettv->vval.v_number = test_edit_score(bad, good);
18325#endif
18326}
18327
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018328#ifdef FEAT_FLOAT
18329/*
18330 * "tan()" function
18331 */
18332 static void
18333f_tan(argvars, rettv)
18334 typval_T *argvars;
18335 typval_T *rettv;
18336{
18337 float_T f;
18338
18339 rettv->v_type = VAR_FLOAT;
18340 if (get_float_arg(argvars, &f) == OK)
18341 rettv->vval.v_float = tan(f);
18342 else
18343 rettv->vval.v_float = 0.0;
18344}
18345
18346/*
18347 * "tanh()" function
18348 */
18349 static void
18350f_tanh(argvars, rettv)
18351 typval_T *argvars;
18352 typval_T *rettv;
18353{
18354 float_T f;
18355
18356 rettv->v_type = VAR_FLOAT;
18357 if (get_float_arg(argvars, &f) == OK)
18358 rettv->vval.v_float = tanh(f);
18359 else
18360 rettv->vval.v_float = 0.0;
18361}
18362#endif
18363
Bram Moolenaard52d9742005-08-21 22:20:28 +000018364/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018365 * "tolower(string)" function
18366 */
18367 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018368f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018369 typval_T *argvars;
18370 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018371{
18372 char_u *p;
18373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018374 p = vim_strsave(get_tv_string(&argvars[0]));
18375 rettv->v_type = VAR_STRING;
18376 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377
18378 if (p != NULL)
18379 while (*p != NUL)
18380 {
18381#ifdef FEAT_MBYTE
18382 int l;
18383
18384 if (enc_utf8)
18385 {
18386 int c, lc;
18387
18388 c = utf_ptr2char(p);
18389 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018390 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 /* TODO: reallocate string when byte count changes. */
18392 if (utf_char2len(lc) == l)
18393 utf_char2bytes(lc, p);
18394 p += l;
18395 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018396 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018397 p += l; /* skip multi-byte character */
18398 else
18399#endif
18400 {
18401 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18402 ++p;
18403 }
18404 }
18405}
18406
18407/*
18408 * "toupper(string)" function
18409 */
18410 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018411f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018412 typval_T *argvars;
18413 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018415 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018416 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417}
18418
18419/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018420 * "tr(string, fromstr, tostr)" function
18421 */
18422 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018423f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018424 typval_T *argvars;
18425 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018426{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018427 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018428 char_u *fromstr;
18429 char_u *tostr;
18430 char_u *p;
18431#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018432 int inlen;
18433 int fromlen;
18434 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018435 int idx;
18436 char_u *cpstr;
18437 int cplen;
18438 int first = TRUE;
18439#endif
18440 char_u buf[NUMBUFLEN];
18441 char_u buf2[NUMBUFLEN];
18442 garray_T ga;
18443
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018444 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018445 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18446 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018447
18448 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018449 rettv->v_type = VAR_STRING;
18450 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018451 if (fromstr == NULL || tostr == NULL)
18452 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018453 ga_init2(&ga, (int)sizeof(char), 80);
18454
18455#ifdef FEAT_MBYTE
18456 if (!has_mbyte)
18457#endif
18458 /* not multi-byte: fromstr and tostr must be the same length */
18459 if (STRLEN(fromstr) != STRLEN(tostr))
18460 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018461#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018462error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018463#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018464 EMSG2(_(e_invarg2), fromstr);
18465 ga_clear(&ga);
18466 return;
18467 }
18468
18469 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018470 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018471 {
18472#ifdef FEAT_MBYTE
18473 if (has_mbyte)
18474 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018475 inlen = (*mb_ptr2len)(in_str);
18476 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018477 cplen = inlen;
18478 idx = 0;
18479 for (p = fromstr; *p != NUL; p += fromlen)
18480 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018481 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018482 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018483 {
18484 for (p = tostr; *p != NUL; p += tolen)
18485 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018486 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018487 if (idx-- == 0)
18488 {
18489 cplen = tolen;
18490 cpstr = p;
18491 break;
18492 }
18493 }
18494 if (*p == NUL) /* tostr is shorter than fromstr */
18495 goto error;
18496 break;
18497 }
18498 ++idx;
18499 }
18500
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018501 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018502 {
18503 /* Check that fromstr and tostr have the same number of
18504 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018505 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018506 first = FALSE;
18507 for (p = tostr; *p != NUL; p += tolen)
18508 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018509 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018510 --idx;
18511 }
18512 if (idx != 0)
18513 goto error;
18514 }
18515
18516 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018517 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018518 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018519
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018520 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018521 }
18522 else
18523#endif
18524 {
18525 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018526 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018527 if (p != NULL)
18528 ga_append(&ga, tostr[p - fromstr]);
18529 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018530 ga_append(&ga, *in_str);
18531 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018532 }
18533 }
18534
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018535 /* add a terminating NUL */
18536 ga_grow(&ga, 1);
18537 ga_append(&ga, NUL);
18538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018539 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018540}
18541
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018542#ifdef FEAT_FLOAT
18543/*
18544 * "trunc({float})" function
18545 */
18546 static void
18547f_trunc(argvars, rettv)
18548 typval_T *argvars;
18549 typval_T *rettv;
18550{
18551 float_T f;
18552
18553 rettv->v_type = VAR_FLOAT;
18554 if (get_float_arg(argvars, &f) == OK)
18555 /* trunc() is not in C90, use floor() or ceil() instead. */
18556 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18557 else
18558 rettv->vval.v_float = 0.0;
18559}
18560#endif
18561
Bram Moolenaar8299df92004-07-10 09:47:34 +000018562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018563 * "type(expr)" function
18564 */
18565 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018566f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018567 typval_T *argvars;
18568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018570 int n;
18571
18572 switch (argvars[0].v_type)
18573 {
18574 case VAR_NUMBER: n = 0; break;
18575 case VAR_STRING: n = 1; break;
18576 case VAR_FUNC: n = 2; break;
18577 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018578 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018579#ifdef FEAT_FLOAT
18580 case VAR_FLOAT: n = 5; break;
18581#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018582 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18583 }
18584 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018585}
18586
18587/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018588 * "undofile(name)" function
18589 */
18590 static void
18591f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018592 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018593 typval_T *rettv;
18594{
18595 rettv->v_type = VAR_STRING;
18596#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018597 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018598 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018599
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018600 if (*fname == NUL)
18601 {
18602 /* If there is no file name there will be no undo file. */
18603 rettv->vval.v_string = NULL;
18604 }
18605 else
18606 {
18607 char_u *ffname = FullName_save(fname, FALSE);
18608
18609 if (ffname != NULL)
18610 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18611 vim_free(ffname);
18612 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018613 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018614#else
18615 rettv->vval.v_string = NULL;
18616#endif
18617}
18618
18619/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018620 * "undotree()" function
18621 */
18622 static void
18623f_undotree(argvars, rettv)
18624 typval_T *argvars UNUSED;
18625 typval_T *rettv;
18626{
18627 if (rettv_dict_alloc(rettv) == OK)
18628 {
18629 dict_T *dict = rettv->vval.v_dict;
18630 list_T *list;
18631
Bram Moolenaar730cde92010-06-27 05:18:54 +020018632 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018633 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018634 dict_add_nr_str(dict, "save_last",
18635 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018636 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18637 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018638 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018639
18640 list = list_alloc();
18641 if (list != NULL)
18642 {
18643 u_eval_tree(curbuf->b_u_oldhead, list);
18644 dict_add_list(dict, "entries", list);
18645 }
18646 }
18647}
18648
18649/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018650 * "values(dict)" function
18651 */
18652 static void
18653f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018654 typval_T *argvars;
18655 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018656{
18657 dict_list(argvars, rettv, 1);
18658}
18659
18660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661 * "virtcol(string)" function
18662 */
18663 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018664f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018665 typval_T *argvars;
18666 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018667{
18668 colnr_T vcol = 0;
18669 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018670 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018672 fp = var2fpos(&argvars[0], FALSE, &fnum);
18673 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18674 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 {
18676 getvvcol(curwin, fp, NULL, NULL, &vcol);
18677 ++vcol;
18678 }
18679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018680 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018681}
18682
18683/*
18684 * "visualmode()" function
18685 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018686 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018687f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018688 typval_T *argvars UNUSED;
18689 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018690{
18691#ifdef FEAT_VISUAL
18692 char_u str[2];
18693
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018694 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018695 str[0] = curbuf->b_visual_mode_eval;
18696 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018697 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698
18699 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018700 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018701 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018702#endif
18703}
18704
18705/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018706 * "wildmenumode()" function
18707 */
18708 static void
18709f_wildmenumode(argvars, rettv)
18710 typval_T *argvars UNUSED;
18711 typval_T *rettv UNUSED;
18712{
18713#ifdef FEAT_WILDMENU
18714 if (wild_menu_showing)
18715 rettv->vval.v_number = 1;
18716#endif
18717}
18718
18719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720 * "winbufnr(nr)" function
18721 */
18722 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018723f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018724 typval_T *argvars;
18725 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018726{
18727 win_T *wp;
18728
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018729 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018730 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018731 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018732 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018733 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018734}
18735
18736/*
18737 * "wincol()" function
18738 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018740f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018741 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743{
18744 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018745 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746}
18747
18748/*
18749 * "winheight(nr)" function
18750 */
18751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018752f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018753 typval_T *argvars;
18754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018755{
18756 win_T *wp;
18757
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018758 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018759 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018760 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018762 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763}
18764
18765/*
18766 * "winline()" function
18767 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018769f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018770 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772{
18773 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018774 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018775}
18776
18777/*
18778 * "winnr()" function
18779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018781f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018782 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018783 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018784{
18785 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018786
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018788 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018790 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791}
18792
18793/*
18794 * "winrestcmd()" function
18795 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018797f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018798 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018799 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018800{
18801#ifdef FEAT_WINDOWS
18802 win_T *wp;
18803 int winnr = 1;
18804 garray_T ga;
18805 char_u buf[50];
18806
18807 ga_init2(&ga, (int)sizeof(char), 70);
18808 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18809 {
18810 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18811 ga_concat(&ga, buf);
18812# ifdef FEAT_VERTSPLIT
18813 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18814 ga_concat(&ga, buf);
18815# endif
18816 ++winnr;
18817 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018818 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018819
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018820 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018822 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018824 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825}
18826
18827/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018828 * "winrestview()" function
18829 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018830 static void
18831f_winrestview(argvars, rettv)
18832 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018833 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018834{
18835 dict_T *dict;
18836
18837 if (argvars[0].v_type != VAR_DICT
18838 || (dict = argvars[0].vval.v_dict) == NULL)
18839 EMSG(_(e_invarg));
18840 else
18841 {
18842 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18843 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18844#ifdef FEAT_VIRTUALEDIT
18845 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18846#endif
18847 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018848 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018849
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018850 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018851#ifdef FEAT_DIFF
18852 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18853#endif
18854 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18855 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18856
18857 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018858 win_new_height(curwin, curwin->w_height);
18859# ifdef FEAT_VERTSPLIT
18860 win_new_width(curwin, W_WIDTH(curwin));
18861# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018862 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018863
18864 if (curwin->w_topline == 0)
18865 curwin->w_topline = 1;
18866 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18867 curwin->w_topline = curbuf->b_ml.ml_line_count;
18868#ifdef FEAT_DIFF
18869 check_topfill(curwin, TRUE);
18870#endif
18871 }
18872}
18873
18874/*
18875 * "winsaveview()" function
18876 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018877 static void
18878f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018879 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018880 typval_T *rettv;
18881{
18882 dict_T *dict;
18883
Bram Moolenaara800b422010-06-27 01:15:55 +020018884 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018885 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018886 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018887
18888 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18889 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18890#ifdef FEAT_VIRTUALEDIT
18891 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18892#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018893 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018894 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18895
18896 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18897#ifdef FEAT_DIFF
18898 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18899#endif
18900 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18901 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18902}
18903
18904/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018905 * "winwidth(nr)" function
18906 */
18907 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018908f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018909 typval_T *argvars;
18910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018911{
18912 win_T *wp;
18913
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018914 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018915 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018916 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018917 else
18918#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018919 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018921 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922#endif
18923}
18924
Bram Moolenaar071d4272004-06-13 20:20:40 +000018925/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018926 * "writefile()" function
18927 */
18928 static void
18929f_writefile(argvars, rettv)
18930 typval_T *argvars;
18931 typval_T *rettv;
18932{
18933 int binary = FALSE;
18934 char_u *fname;
18935 FILE *fd;
18936 listitem_T *li;
18937 char_u *s;
18938 int ret = 0;
18939 int c;
18940
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018941 if (check_restricted() || check_secure())
18942 return;
18943
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018944 if (argvars[0].v_type != VAR_LIST)
18945 {
18946 EMSG2(_(e_listarg), "writefile()");
18947 return;
18948 }
18949 if (argvars[0].vval.v_list == NULL)
18950 return;
18951
18952 if (argvars[2].v_type != VAR_UNKNOWN
18953 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18954 binary = TRUE;
18955
18956 /* Always open the file in binary mode, library functions have a mind of
18957 * their own about CR-LF conversion. */
18958 fname = get_tv_string(&argvars[1]);
18959 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18960 {
18961 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18962 ret = -1;
18963 }
18964 else
18965 {
18966 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18967 li = li->li_next)
18968 {
18969 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18970 {
18971 if (*s == '\n')
18972 c = putc(NUL, fd);
18973 else
18974 c = putc(*s, fd);
18975 if (c == EOF)
18976 {
18977 ret = -1;
18978 break;
18979 }
18980 }
18981 if (!binary || li->li_next != NULL)
18982 if (putc('\n', fd) == EOF)
18983 {
18984 ret = -1;
18985 break;
18986 }
18987 if (ret < 0)
18988 {
18989 EMSG(_(e_write));
18990 break;
18991 }
18992 }
18993 fclose(fd);
18994 }
18995
18996 rettv->vval.v_number = ret;
18997}
18998
18999/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019000 * "xor(expr, expr)" function
19001 */
19002 static void
19003f_xor(argvars, rettv)
19004 typval_T *argvars;
19005 typval_T *rettv;
19006{
19007 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19008 ^ get_tv_number_chk(&argvars[1], NULL);
19009}
19010
19011
19012/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019013 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019014 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019015 */
19016 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019017var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019018 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019019 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019020 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019021{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019022 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019023 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019024 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019025
Bram Moolenaara5525202006-03-02 22:52:09 +000019026 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019027 if (varp->v_type == VAR_LIST)
19028 {
19029 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019030 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019031 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019032 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019033
19034 l = varp->vval.v_list;
19035 if (l == NULL)
19036 return NULL;
19037
19038 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019039 pos.lnum = list_find_nr(l, 0L, &error);
19040 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019041 return NULL; /* invalid line number */
19042
19043 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019044 pos.col = list_find_nr(l, 1L, &error);
19045 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019046 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019047 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019048
19049 /* We accept "$" for the column number: last column. */
19050 li = list_find(l, 1L);
19051 if (li != NULL && li->li_tv.v_type == VAR_STRING
19052 && li->li_tv.vval.v_string != NULL
19053 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19054 pos.col = len + 1;
19055
Bram Moolenaara5525202006-03-02 22:52:09 +000019056 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019057 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019058 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019059 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019060
Bram Moolenaara5525202006-03-02 22:52:09 +000019061#ifdef FEAT_VIRTUALEDIT
19062 /* Get the virtual offset. Defaults to zero. */
19063 pos.coladd = list_find_nr(l, 2L, &error);
19064 if (error)
19065 pos.coladd = 0;
19066#endif
19067
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019068 return &pos;
19069 }
19070
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019071 name = get_tv_string_chk(varp);
19072 if (name == NULL)
19073 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019074 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019076#ifdef FEAT_VISUAL
19077 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19078 {
19079 if (VIsual_active)
19080 return &VIsual;
19081 return &curwin->w_cursor;
19082 }
19083#endif
19084 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019086 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019087 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19088 return NULL;
19089 return pp;
19090 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019091
19092#ifdef FEAT_VIRTUALEDIT
19093 pos.coladd = 0;
19094#endif
19095
Bram Moolenaar477933c2007-07-17 14:32:23 +000019096 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019097 {
19098 pos.col = 0;
19099 if (name[1] == '0') /* "w0": first visible line */
19100 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019101 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019102 pos.lnum = curwin->w_topline;
19103 return &pos;
19104 }
19105 else if (name[1] == '$') /* "w$": last visible line */
19106 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019107 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019108 pos.lnum = curwin->w_botline - 1;
19109 return &pos;
19110 }
19111 }
19112 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019114 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 {
19116 pos.lnum = curbuf->b_ml.ml_line_count;
19117 pos.col = 0;
19118 }
19119 else
19120 {
19121 pos.lnum = curwin->w_cursor.lnum;
19122 pos.col = (colnr_T)STRLEN(ml_get_curline());
19123 }
19124 return &pos;
19125 }
19126 return NULL;
19127}
19128
19129/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019130 * Convert list in "arg" into a position and optional file number.
19131 * When "fnump" is NULL there is no file number, only 3 items.
19132 * Note that the column is passed on as-is, the caller may want to decrement
19133 * it to use 1 for the first column.
19134 * Return FAIL when conversion is not possible, doesn't check the position for
19135 * validity.
19136 */
19137 static int
19138list2fpos(arg, posp, fnump)
19139 typval_T *arg;
19140 pos_T *posp;
19141 int *fnump;
19142{
19143 list_T *l = arg->vval.v_list;
19144 long i = 0;
19145 long n;
19146
Bram Moolenaarbde35262006-07-23 20:12:24 +000019147 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19148 * when "fnump" isn't NULL and "coladd" is optional. */
19149 if (arg->v_type != VAR_LIST
19150 || l == NULL
19151 || l->lv_len < (fnump == NULL ? 2 : 3)
19152 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019153 return FAIL;
19154
19155 if (fnump != NULL)
19156 {
19157 n = list_find_nr(l, i++, NULL); /* fnum */
19158 if (n < 0)
19159 return FAIL;
19160 if (n == 0)
19161 n = curbuf->b_fnum; /* current buffer */
19162 *fnump = n;
19163 }
19164
19165 n = list_find_nr(l, i++, NULL); /* lnum */
19166 if (n < 0)
19167 return FAIL;
19168 posp->lnum = n;
19169
19170 n = list_find_nr(l, i++, NULL); /* col */
19171 if (n < 0)
19172 return FAIL;
19173 posp->col = n;
19174
19175#ifdef FEAT_VIRTUALEDIT
19176 n = list_find_nr(l, i, NULL);
19177 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019178 posp->coladd = 0;
19179 else
19180 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019181#endif
19182
19183 return OK;
19184}
19185
19186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019187 * Get the length of an environment variable name.
19188 * Advance "arg" to the first character after the name.
19189 * Return 0 for error.
19190 */
19191 static int
19192get_env_len(arg)
19193 char_u **arg;
19194{
19195 char_u *p;
19196 int len;
19197
19198 for (p = *arg; vim_isIDc(*p); ++p)
19199 ;
19200 if (p == *arg) /* no name found */
19201 return 0;
19202
19203 len = (int)(p - *arg);
19204 *arg = p;
19205 return len;
19206}
19207
19208/*
19209 * Get the length of the name of a function or internal variable.
19210 * "arg" is advanced to the first non-white character after the name.
19211 * Return 0 if something is wrong.
19212 */
19213 static int
19214get_id_len(arg)
19215 char_u **arg;
19216{
19217 char_u *p;
19218 int len;
19219
19220 /* Find the end of the name. */
19221 for (p = *arg; eval_isnamec(*p); ++p)
19222 ;
19223 if (p == *arg) /* no name found */
19224 return 0;
19225
19226 len = (int)(p - *arg);
19227 *arg = skipwhite(p);
19228
19229 return len;
19230}
19231
19232/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019233 * Get the length of the name of a variable or function.
19234 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019235 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019236 * Return -1 if curly braces expansion failed.
19237 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019238 * If the name contains 'magic' {}'s, expand them and return the
19239 * expanded name in an allocated string via 'alias' - caller must free.
19240 */
19241 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019242get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019243 char_u **arg;
19244 char_u **alias;
19245 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019246 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019247{
19248 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249 char_u *p;
19250 char_u *expr_start;
19251 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019252
19253 *alias = NULL; /* default to no alias */
19254
19255 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19256 && (*arg)[2] == (int)KE_SNR)
19257 {
19258 /* hard coded <SNR>, already translated */
19259 *arg += 3;
19260 return get_id_len(arg) + 3;
19261 }
19262 len = eval_fname_script(*arg);
19263 if (len > 0)
19264 {
19265 /* literal "<SID>", "s:" or "<SNR>" */
19266 *arg += len;
19267 }
19268
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019270 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019271 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019272 p = find_name_end(*arg, &expr_start, &expr_end,
19273 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019274 if (expr_start != NULL)
19275 {
19276 char_u *temp_string;
19277
19278 if (!evaluate)
19279 {
19280 len += (int)(p - *arg);
19281 *arg = skipwhite(p);
19282 return len;
19283 }
19284
19285 /*
19286 * Include any <SID> etc in the expanded string:
19287 * Thus the -len here.
19288 */
19289 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19290 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019291 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292 *alias = temp_string;
19293 *arg = skipwhite(p);
19294 return (int)STRLEN(temp_string);
19295 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296
19297 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019298 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019299 EMSG2(_(e_invexpr2), *arg);
19300
19301 return len;
19302}
19303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019304/*
19305 * Find the end of a variable or function name, taking care of magic braces.
19306 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19307 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019308 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019309 * Return a pointer to just after the name. Equal to "arg" if there is no
19310 * valid name.
19311 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019312 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019313find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314 char_u *arg;
19315 char_u **expr_start;
19316 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019317 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019318{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019319 int mb_nest = 0;
19320 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019321 char_u *p;
19322
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019323 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019325 *expr_start = NULL;
19326 *expr_end = NULL;
19327 }
19328
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019329 /* Quick check for valid starting character. */
19330 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19331 return arg;
19332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019333 for (p = arg; *p != NUL
19334 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019335 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019336 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019337 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019338 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019339 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019340 if (*p == '\'')
19341 {
19342 /* skip over 'string' to avoid counting [ and ] inside it. */
19343 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19344 ;
19345 if (*p == NUL)
19346 break;
19347 }
19348 else if (*p == '"')
19349 {
19350 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19351 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19352 if (*p == '\\' && p[1] != NUL)
19353 ++p;
19354 if (*p == NUL)
19355 break;
19356 }
19357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019358 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019359 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019360 if (*p == '[')
19361 ++br_nest;
19362 else if (*p == ']')
19363 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019364 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019365
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019366 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019368 if (*p == '{')
19369 {
19370 mb_nest++;
19371 if (expr_start != NULL && *expr_start == NULL)
19372 *expr_start = p;
19373 }
19374 else if (*p == '}')
19375 {
19376 mb_nest--;
19377 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19378 *expr_end = p;
19379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 }
19382
19383 return p;
19384}
19385
19386/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019387 * Expands out the 'magic' {}'s in a variable/function name.
19388 * Note that this can call itself recursively, to deal with
19389 * constructs like foo{bar}{baz}{bam}
19390 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19391 * "in_start" ^
19392 * "expr_start" ^
19393 * "expr_end" ^
19394 * "in_end" ^
19395 *
19396 * Returns a new allocated string, which the caller must free.
19397 * Returns NULL for failure.
19398 */
19399 static char_u *
19400make_expanded_name(in_start, expr_start, expr_end, in_end)
19401 char_u *in_start;
19402 char_u *expr_start;
19403 char_u *expr_end;
19404 char_u *in_end;
19405{
19406 char_u c1;
19407 char_u *retval = NULL;
19408 char_u *temp_result;
19409 char_u *nextcmd = NULL;
19410
19411 if (expr_end == NULL || in_end == NULL)
19412 return NULL;
19413 *expr_start = NUL;
19414 *expr_end = NUL;
19415 c1 = *in_end;
19416 *in_end = NUL;
19417
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019418 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019419 if (temp_result != NULL && nextcmd == NULL)
19420 {
19421 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19422 + (in_end - expr_end) + 1));
19423 if (retval != NULL)
19424 {
19425 STRCPY(retval, in_start);
19426 STRCAT(retval, temp_result);
19427 STRCAT(retval, expr_end + 1);
19428 }
19429 }
19430 vim_free(temp_result);
19431
19432 *in_end = c1; /* put char back for error messages */
19433 *expr_start = '{';
19434 *expr_end = '}';
19435
19436 if (retval != NULL)
19437 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019438 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019439 if (expr_start != NULL)
19440 {
19441 /* Further expansion! */
19442 temp_result = make_expanded_name(retval, expr_start,
19443 expr_end, temp_result);
19444 vim_free(retval);
19445 retval = temp_result;
19446 }
19447 }
19448
19449 return retval;
19450}
19451
19452/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019454 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019455 */
19456 static int
19457eval_isnamec(c)
19458 int c;
19459{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019460 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19461}
19462
19463/*
19464 * Return TRUE if character "c" can be used as the first character in a
19465 * variable or function name (excluding '{' and '}').
19466 */
19467 static int
19468eval_isnamec1(c)
19469 int c;
19470{
19471 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472}
19473
19474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019475 * Set number v: variable to "val".
19476 */
19477 void
19478set_vim_var_nr(idx, val)
19479 int idx;
19480 long val;
19481{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019482 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019483}
19484
19485/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019486 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487 */
19488 long
19489get_vim_var_nr(idx)
19490 int idx;
19491{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019492 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493}
19494
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019495/*
19496 * Get string v: variable value. Uses a static buffer, can only be used once.
19497 */
19498 char_u *
19499get_vim_var_str(idx)
19500 int idx;
19501{
19502 return get_tv_string(&vimvars[idx].vv_tv);
19503}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019504
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019506 * Get List v: variable value. Caller must take care of reference count when
19507 * needed.
19508 */
19509 list_T *
19510get_vim_var_list(idx)
19511 int idx;
19512{
19513 return vimvars[idx].vv_list;
19514}
19515
19516/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019517 * Set v:char to character "c".
19518 */
19519 void
19520set_vim_var_char(c)
19521 int c;
19522{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019523 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019524
19525#ifdef FEAT_MBYTE
19526 if (has_mbyte)
19527 buf[(*mb_char2bytes)(c, buf)] = NUL;
19528 else
19529#endif
19530 {
19531 buf[0] = c;
19532 buf[1] = NUL;
19533 }
19534 set_vim_var_string(VV_CHAR, buf, -1);
19535}
19536
19537/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019538 * Set v:count to "count" and v:count1 to "count1".
19539 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540 */
19541 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019542set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 long count;
19544 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019545 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019547 if (set_prevcount)
19548 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019549 vimvars[VV_COUNT].vv_nr = count;
19550 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551}
19552
19553/*
19554 * Set string v: variable to a copy of "val".
19555 */
19556 void
19557set_vim_var_string(idx, val, len)
19558 int idx;
19559 char_u *val;
19560 int len; /* length of "val" to use or -1 (whole string) */
19561{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019562 /* Need to do this (at least) once, since we can't initialize a union.
19563 * Will always be invoked when "v:progname" is set. */
19564 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19565
Bram Moolenaare9a41262005-01-15 22:18:47 +000019566 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019568 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019570 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019572 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573}
19574
19575/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019576 * Set List v: variable to "val".
19577 */
19578 void
19579set_vim_var_list(idx, val)
19580 int idx;
19581 list_T *val;
19582{
19583 list_unref(vimvars[idx].vv_list);
19584 vimvars[idx].vv_list = val;
19585 if (val != NULL)
19586 ++val->lv_refcount;
19587}
19588
19589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590 * Set v:register if needed.
19591 */
19592 void
19593set_reg_var(c)
19594 int c;
19595{
19596 char_u regname;
19597
19598 if (c == 0 || c == ' ')
19599 regname = '"';
19600 else
19601 regname = c;
19602 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019603 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604 set_vim_var_string(VV_REG, &regname, 1);
19605}
19606
19607/*
19608 * Get or set v:exception. If "oldval" == NULL, return the current value.
19609 * Otherwise, restore the value to "oldval" and return NULL.
19610 * Must always be called in pairs to save and restore v:exception! Does not
19611 * take care of memory allocations.
19612 */
19613 char_u *
19614v_exception(oldval)
19615 char_u *oldval;
19616{
19617 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019618 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019619
Bram Moolenaare9a41262005-01-15 22:18:47 +000019620 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 return NULL;
19622}
19623
19624/*
19625 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19626 * Otherwise, restore the value to "oldval" and return NULL.
19627 * Must always be called in pairs to save and restore v:throwpoint! Does not
19628 * take care of memory allocations.
19629 */
19630 char_u *
19631v_throwpoint(oldval)
19632 char_u *oldval;
19633{
19634 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019635 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636
Bram Moolenaare9a41262005-01-15 22:18:47 +000019637 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 return NULL;
19639}
19640
19641#if defined(FEAT_AUTOCMD) || defined(PROTO)
19642/*
19643 * Set v:cmdarg.
19644 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19645 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19646 * Must always be called in pairs!
19647 */
19648 char_u *
19649set_cmdarg(eap, oldarg)
19650 exarg_T *eap;
19651 char_u *oldarg;
19652{
19653 char_u *oldval;
19654 char_u *newval;
19655 unsigned len;
19656
Bram Moolenaare9a41262005-01-15 22:18:47 +000019657 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019658 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019660 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019661 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019662 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663 }
19664
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019665 if (eap->force_bin == FORCE_BIN)
19666 len = 6;
19667 else if (eap->force_bin == FORCE_NOBIN)
19668 len = 8;
19669 else
19670 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019671
19672 if (eap->read_edit)
19673 len += 7;
19674
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019675 if (eap->force_ff != 0)
19676 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19677# ifdef FEAT_MBYTE
19678 if (eap->force_enc != 0)
19679 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019680 if (eap->bad_char != 0)
19681 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019682# endif
19683
19684 newval = alloc(len + 1);
19685 if (newval == NULL)
19686 return NULL;
19687
19688 if (eap->force_bin == FORCE_BIN)
19689 sprintf((char *)newval, " ++bin");
19690 else if (eap->force_bin == FORCE_NOBIN)
19691 sprintf((char *)newval, " ++nobin");
19692 else
19693 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019694
19695 if (eap->read_edit)
19696 STRCAT(newval, " ++edit");
19697
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019698 if (eap->force_ff != 0)
19699 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19700 eap->cmd + eap->force_ff);
19701# ifdef FEAT_MBYTE
19702 if (eap->force_enc != 0)
19703 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19704 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019705 if (eap->bad_char == BAD_KEEP)
19706 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19707 else if (eap->bad_char == BAD_DROP)
19708 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19709 else if (eap->bad_char != 0)
19710 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019711# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019712 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019713 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019714}
19715#endif
19716
19717/*
19718 * Get the value of internal variable "name".
19719 * Return OK or FAIL.
19720 */
19721 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019722get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019723 char_u *name;
19724 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019725 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019726 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019727{
19728 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019729 typval_T *tv = NULL;
19730 typval_T atv;
19731 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733
19734 /* truncate the name, so that we can use strcmp() */
19735 cc = name[len];
19736 name[len] = NUL;
19737
19738 /*
19739 * Check for "b:changedtick".
19740 */
19741 if (STRCMP(name, "b:changedtick") == 0)
19742 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019743 atv.v_type = VAR_NUMBER;
19744 atv.vval.v_number = curbuf->b_changedtick;
19745 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746 }
19747
19748 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 * Check for user-defined variables.
19750 */
19751 else
19752 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019753 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019755 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019756 }
19757
Bram Moolenaare9a41262005-01-15 22:18:47 +000019758 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019759 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019760 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019761 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019762 ret = FAIL;
19763 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019764 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019765 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766
19767 name[len] = cc;
19768
19769 return ret;
19770}
19771
19772/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019773 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19774 * Also handle function call with Funcref variable: func(expr)
19775 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19776 */
19777 static int
19778handle_subscript(arg, rettv, evaluate, verbose)
19779 char_u **arg;
19780 typval_T *rettv;
19781 int evaluate; /* do more than finding the end */
19782 int verbose; /* give error messages */
19783{
19784 int ret = OK;
19785 dict_T *selfdict = NULL;
19786 char_u *s;
19787 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019788 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019789
19790 while (ret == OK
19791 && (**arg == '['
19792 || (**arg == '.' && rettv->v_type == VAR_DICT)
19793 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19794 && !vim_iswhite(*(*arg - 1)))
19795 {
19796 if (**arg == '(')
19797 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019798 /* need to copy the funcref so that we can clear rettv */
19799 functv = *rettv;
19800 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019801
19802 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019803 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019804 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019805 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19806 &len, evaluate, selfdict);
19807
19808 /* Clear the funcref afterwards, so that deleting it while
19809 * evaluating the arguments is possible (see test55). */
19810 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019811
19812 /* Stop the expression evaluation when immediately aborting on
19813 * error, or when an interrupt occurred or an exception was thrown
19814 * but not caught. */
19815 if (aborting())
19816 {
19817 if (ret == OK)
19818 clear_tv(rettv);
19819 ret = FAIL;
19820 }
19821 dict_unref(selfdict);
19822 selfdict = NULL;
19823 }
19824 else /* **arg == '[' || **arg == '.' */
19825 {
19826 dict_unref(selfdict);
19827 if (rettv->v_type == VAR_DICT)
19828 {
19829 selfdict = rettv->vval.v_dict;
19830 if (selfdict != NULL)
19831 ++selfdict->dv_refcount;
19832 }
19833 else
19834 selfdict = NULL;
19835 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19836 {
19837 clear_tv(rettv);
19838 ret = FAIL;
19839 }
19840 }
19841 }
19842 dict_unref(selfdict);
19843 return ret;
19844}
19845
19846/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019847 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019848 * value).
19849 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019850 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019851alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019852{
Bram Moolenaar33570922005-01-25 22:26:29 +000019853 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019854}
19855
19856/*
19857 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019858 * The string "s" must have been allocated, it is consumed.
19859 * Return NULL for out of memory, the variable otherwise.
19860 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019861 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019862alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019863 char_u *s;
19864{
Bram Moolenaar33570922005-01-25 22:26:29 +000019865 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019866
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019867 rettv = alloc_tv();
19868 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019870 rettv->v_type = VAR_STRING;
19871 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019872 }
19873 else
19874 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019875 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876}
19877
19878/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019879 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019881 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019882free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019883 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884{
19885 if (varp != NULL)
19886 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019887 switch (varp->v_type)
19888 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019889 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019890 func_unref(varp->vval.v_string);
19891 /*FALLTHROUGH*/
19892 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019893 vim_free(varp->vval.v_string);
19894 break;
19895 case VAR_LIST:
19896 list_unref(varp->vval.v_list);
19897 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019898 case VAR_DICT:
19899 dict_unref(varp->vval.v_dict);
19900 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019901 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019902#ifdef FEAT_FLOAT
19903 case VAR_FLOAT:
19904#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019905 case VAR_UNKNOWN:
19906 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019907 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019908 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019909 break;
19910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019911 vim_free(varp);
19912 }
19913}
19914
19915/*
19916 * Free the memory for a variable value and set the value to NULL or 0.
19917 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019918 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019919clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019920 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921{
19922 if (varp != NULL)
19923 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019924 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019926 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019927 func_unref(varp->vval.v_string);
19928 /*FALLTHROUGH*/
19929 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019930 vim_free(varp->vval.v_string);
19931 varp->vval.v_string = NULL;
19932 break;
19933 case VAR_LIST:
19934 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019935 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019936 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019937 case VAR_DICT:
19938 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019939 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019940 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019941 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019942 varp->vval.v_number = 0;
19943 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019944#ifdef FEAT_FLOAT
19945 case VAR_FLOAT:
19946 varp->vval.v_float = 0.0;
19947 break;
19948#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019949 case VAR_UNKNOWN:
19950 break;
19951 default:
19952 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019953 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019954 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 }
19956}
19957
19958/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019959 * Set the value of a variable to NULL without freeing items.
19960 */
19961 static void
19962init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019963 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019964{
19965 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019966 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019967}
19968
19969/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 * Get the number value of a variable.
19971 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019972 * For incompatible types, return 0.
19973 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19974 * caller of incompatible types: it sets *denote to TRUE if "denote"
19975 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 */
19977 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019978get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019979 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019981 int error = FALSE;
19982
19983 return get_tv_number_chk(varp, &error); /* return 0L on error */
19984}
19985
Bram Moolenaar4be06f92005-07-29 22:36:03 +000019986 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019987get_tv_number_chk(varp, denote)
19988 typval_T *varp;
19989 int *denote;
19990{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019991 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019992
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019993 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019995 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019996 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019997#ifdef FEAT_FLOAT
19998 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000019999 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020000 break;
20001#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020002 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020003 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020004 break;
20005 case VAR_STRING:
20006 if (varp->vval.v_string != NULL)
20007 vim_str2nr(varp->vval.v_string, NULL, NULL,
20008 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020009 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020010 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020011 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020012 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020013 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020014 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020015 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020016 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020017 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020018 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020019 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020020 if (denote == NULL) /* useful for values that must be unsigned */
20021 n = -1;
20022 else
20023 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020024 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020025}
20026
20027/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020028 * Get the lnum from the first argument.
20029 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020030 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020031 */
20032 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020033get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020034 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020035{
Bram Moolenaar33570922005-01-25 22:26:29 +000020036 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 linenr_T lnum;
20038
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020039 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020040 if (lnum == 0) /* no valid number, try using line() */
20041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020042 rettv.v_type = VAR_NUMBER;
20043 f_line(argvars, &rettv);
20044 lnum = rettv.vval.v_number;
20045 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020046 }
20047 return lnum;
20048}
20049
20050/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020051 * Get the lnum from the first argument.
20052 * Also accepts "$", then "buf" is used.
20053 * Returns 0 on error.
20054 */
20055 static linenr_T
20056get_tv_lnum_buf(argvars, buf)
20057 typval_T *argvars;
20058 buf_T *buf;
20059{
20060 if (argvars[0].v_type == VAR_STRING
20061 && argvars[0].vval.v_string != NULL
20062 && argvars[0].vval.v_string[0] == '$'
20063 && buf != NULL)
20064 return buf->b_ml.ml_line_count;
20065 return get_tv_number_chk(&argvars[0], NULL);
20066}
20067
20068/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020069 * Get the string value of a variable.
20070 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020071 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20072 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073 * If the String variable has never been set, return an empty string.
20074 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020075 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20076 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020077 */
20078 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020079get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020080 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020081{
20082 static char_u mybuf[NUMBUFLEN];
20083
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020084 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085}
20086
20087 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020088get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020089 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020090 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020092 char_u *res = get_tv_string_buf_chk(varp, buf);
20093
20094 return res != NULL ? res : (char_u *)"";
20095}
20096
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020097 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020098get_tv_string_chk(varp)
20099 typval_T *varp;
20100{
20101 static char_u mybuf[NUMBUFLEN];
20102
20103 return get_tv_string_buf_chk(varp, mybuf);
20104}
20105
20106 static char_u *
20107get_tv_string_buf_chk(varp, buf)
20108 typval_T *varp;
20109 char_u *buf;
20110{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020111 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020113 case VAR_NUMBER:
20114 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20115 return buf;
20116 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020117 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020118 break;
20119 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020120 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020121 break;
20122 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020123 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020124 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020125#ifdef FEAT_FLOAT
20126 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020127 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020128 break;
20129#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020130 case VAR_STRING:
20131 if (varp->vval.v_string != NULL)
20132 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020133 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020134 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020135 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020136 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020137 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020138 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139}
20140
20141/*
20142 * Find variable "name" in the list of variables.
20143 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020144 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020145 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020146 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020147 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020148 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020149find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020151 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020152{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020153 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020154 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155
Bram Moolenaara7043832005-01-21 11:56:39 +000020156 ht = find_var_ht(name, &varname);
20157 if (htp != NULL)
20158 *htp = ht;
20159 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020160 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020161 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020162}
20163
20164/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020165 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020166 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020168 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020169find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020170 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020171 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020172 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020173 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020174{
Bram Moolenaar33570922005-01-25 22:26:29 +000020175 hashitem_T *hi;
20176
20177 if (*varname == NUL)
20178 {
20179 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020180 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020181 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020182 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020183 case 'g': return &globvars_var;
20184 case 'v': return &vimvars_var;
20185 case 'b': return &curbuf->b_bufvar;
20186 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020187#ifdef FEAT_WINDOWS
20188 case 't': return &curtab->tp_winvar;
20189#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020190 case 'l': return current_funccal == NULL
20191 ? NULL : &current_funccal->l_vars_var;
20192 case 'a': return current_funccal == NULL
20193 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020194 }
20195 return NULL;
20196 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020197
20198 hi = hash_find(ht, varname);
20199 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020200 {
20201 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020202 * worked find the variable again. Don't auto-load a script if it was
20203 * loaded already, otherwise it would be loaded every time when
20204 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020205 if (ht == &globvarht && !writing)
20206 {
20207 /* Note: script_autoload() may make "hi" invalid. It must either
20208 * be obtained again or not used. */
20209 if (!script_autoload(varname, FALSE) || aborting())
20210 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020211 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020212 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020213 if (HASHITEM_EMPTY(hi))
20214 return NULL;
20215 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020216 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020217}
20218
20219/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020220 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020221 * Set "varname" to the start of name without ':'.
20222 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020223 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020224find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020225 char_u *name;
20226 char_u **varname;
20227{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020228 hashitem_T *hi;
20229
Bram Moolenaar071d4272004-06-13 20:20:40 +000020230 if (name[1] != ':')
20231 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020232 /* The name must not start with a colon or #. */
20233 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 return NULL;
20235 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020236
20237 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020238 hi = hash_find(&compat_hashtab, name);
20239 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020240 return &compat_hashtab;
20241
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020243 return &globvarht; /* global variable */
20244 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245 }
20246 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020247 if (*name == 'g') /* global variable */
20248 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020249 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20250 */
20251 if (vim_strchr(name + 2, ':') != NULL
20252 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020253 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020254 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020255 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020256 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020257 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020258#ifdef FEAT_WINDOWS
20259 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020260 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020261#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020262 if (*name == 'v') /* v: variable */
20263 return &vimvarht;
20264 if (*name == 'a' && current_funccal != NULL) /* function argument */
20265 return &current_funccal->l_avars.dv_hashtab;
20266 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20267 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020268 if (*name == 's' /* script variable */
20269 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20270 return &SCRIPT_VARS(current_SID);
20271 return NULL;
20272}
20273
20274/*
20275 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020276 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020277 * Returns NULL when it doesn't exist.
20278 */
20279 char_u *
20280get_var_value(name)
20281 char_u *name;
20282{
Bram Moolenaar33570922005-01-25 22:26:29 +000020283 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020284
Bram Moolenaara7043832005-01-21 11:56:39 +000020285 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 if (v == NULL)
20287 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020288 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020289}
20290
20291/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020292 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020293 * sourcing this script and when executing functions defined in the script.
20294 */
20295 void
20296new_script_vars(id)
20297 scid_T id;
20298{
Bram Moolenaara7043832005-01-21 11:56:39 +000020299 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020300 hashtab_T *ht;
20301 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020302
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20304 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020305 /* Re-allocating ga_data means that an ht_array pointing to
20306 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020307 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020308 for (i = 1; i <= ga_scripts.ga_len; ++i)
20309 {
20310 ht = &SCRIPT_VARS(i);
20311 if (ht->ht_mask == HT_INIT_SIZE - 1)
20312 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020313 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020314 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020315 }
20316
Bram Moolenaar071d4272004-06-13 20:20:40 +000020317 while (ga_scripts.ga_len < id)
20318 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020319 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020320 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020321 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020323 }
20324 }
20325}
20326
20327/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020328 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20329 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020330 */
20331 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020332init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020333 dict_T *dict;
20334 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020335 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336{
Bram Moolenaar33570922005-01-25 22:26:29 +000020337 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020338 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020339 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020340 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020341 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020342 dict_var->di_tv.vval.v_dict = dict;
20343 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020344 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020345 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20346 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347}
20348
20349/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020350 * Unreference a dictionary initialized by init_var_dict().
20351 */
20352 void
20353unref_var_dict(dict)
20354 dict_T *dict;
20355{
20356 /* Now the dict needs to be freed if no one else is using it, go back to
20357 * normal reference counting. */
20358 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20359 dict_unref(dict);
20360}
20361
20362/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020363 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020364 * Frees all allocated variables and the value they contain.
20365 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366 */
20367 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020368vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020369 hashtab_T *ht;
20370{
20371 vars_clear_ext(ht, TRUE);
20372}
20373
20374/*
20375 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20376 */
20377 static void
20378vars_clear_ext(ht, free_val)
20379 hashtab_T *ht;
20380 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381{
Bram Moolenaara7043832005-01-21 11:56:39 +000020382 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020383 hashitem_T *hi;
20384 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020385
Bram Moolenaar33570922005-01-25 22:26:29 +000020386 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020387 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020388 for (hi = ht->ht_array; todo > 0; ++hi)
20389 {
20390 if (!HASHITEM_EMPTY(hi))
20391 {
20392 --todo;
20393
Bram Moolenaar33570922005-01-25 22:26:29 +000020394 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020395 * ht_array might change then. hash_clear() takes care of it
20396 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020397 v = HI2DI(hi);
20398 if (free_val)
20399 clear_tv(&v->di_tv);
20400 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20401 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020402 }
20403 }
20404 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020405 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020406}
20407
Bram Moolenaara7043832005-01-21 11:56:39 +000020408/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020409 * Delete a variable from hashtab "ht" at item "hi".
20410 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020412 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020413delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020414 hashtab_T *ht;
20415 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020416{
Bram Moolenaar33570922005-01-25 22:26:29 +000020417 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020418
20419 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020420 clear_tv(&di->di_tv);
20421 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020422}
20423
20424/*
20425 * List the value of one internal variable.
20426 */
20427 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020428list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020429 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020431 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020432{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020433 char_u *tofree;
20434 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020435 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020436
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020437 current_copyID += COPYID_INC;
20438 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020439 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020440 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020441 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020442}
20443
Bram Moolenaar071d4272004-06-13 20:20:40 +000020444 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020445list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020446 char_u *prefix;
20447 char_u *name;
20448 int type;
20449 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020450 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451{
Bram Moolenaar31859182007-08-14 20:41:13 +000020452 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20453 msg_start();
20454 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455 if (name != NULL) /* "a:" vars don't have a name stored */
20456 msg_puts(name);
20457 msg_putchar(' ');
20458 msg_advance(22);
20459 if (type == VAR_NUMBER)
20460 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020461 else if (type == VAR_FUNC)
20462 msg_putchar('*');
20463 else if (type == VAR_LIST)
20464 {
20465 msg_putchar('[');
20466 if (*string == '[')
20467 ++string;
20468 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020469 else if (type == VAR_DICT)
20470 {
20471 msg_putchar('{');
20472 if (*string == '{')
20473 ++string;
20474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475 else
20476 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020477
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020479
20480 if (type == VAR_FUNC)
20481 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020482 if (*first)
20483 {
20484 msg_clr_eos();
20485 *first = FALSE;
20486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020487}
20488
20489/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020490 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020491 * If the variable already exists, the value is updated.
20492 * Otherwise the variable is created.
20493 */
20494 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020495set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020497 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020498 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499{
Bram Moolenaar33570922005-01-25 22:26:29 +000020500 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020501 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020502 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020504 ht = find_var_ht(name, &varname);
20505 if (ht == NULL || *varname == NUL)
20506 {
20507 EMSG2(_(e_illvar), name);
20508 return;
20509 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020510 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020511
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020512 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20513 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020514
Bram Moolenaar33570922005-01-25 22:26:29 +000020515 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020517 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020518 if (var_check_ro(v->di_flags, name)
20519 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020520 return;
20521 if (v->di_tv.v_type != tv->v_type
20522 && !((v->di_tv.v_type == VAR_STRING
20523 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020524 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020525 || tv->v_type == VAR_NUMBER))
20526#ifdef FEAT_FLOAT
20527 && !((v->di_tv.v_type == VAR_NUMBER
20528 || v->di_tv.v_type == VAR_FLOAT)
20529 && (tv->v_type == VAR_NUMBER
20530 || tv->v_type == VAR_FLOAT))
20531#endif
20532 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020533 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020534 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020535 return;
20536 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020537
20538 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020539 * Handle setting internal v: variables separately: we don't change
20540 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020541 */
20542 if (ht == &vimvarht)
20543 {
20544 if (v->di_tv.v_type == VAR_STRING)
20545 {
20546 vim_free(v->di_tv.vval.v_string);
20547 if (copy || tv->v_type != VAR_STRING)
20548 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20549 else
20550 {
20551 /* Take over the string to avoid an extra alloc/free. */
20552 v->di_tv.vval.v_string = tv->vval.v_string;
20553 tv->vval.v_string = NULL;
20554 }
20555 }
20556 else if (v->di_tv.v_type != VAR_NUMBER)
20557 EMSG2(_(e_intern2), "set_var()");
20558 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020559 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020560 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020561 if (STRCMP(varname, "searchforward") == 0)
20562 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20563 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020564 return;
20565 }
20566
20567 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568 }
20569 else /* add a new variable */
20570 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020571 /* Can't add "v:" variable. */
20572 if (ht == &vimvarht)
20573 {
20574 EMSG2(_(e_illvar), name);
20575 return;
20576 }
20577
Bram Moolenaar92124a32005-06-17 22:03:40 +000020578 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020579 if (!valid_varname(varname))
20580 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020581
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020582 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20583 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020584 if (v == NULL)
20585 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020586 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020587 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020589 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590 return;
20591 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020592 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020594
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020595 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020596 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020597 else
20598 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020599 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020600 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020601 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603}
20604
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020605/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020606 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020607 * Also give an error message.
20608 */
20609 static int
20610var_check_ro(flags, name)
20611 int flags;
20612 char_u *name;
20613{
20614 if (flags & DI_FLAGS_RO)
20615 {
20616 EMSG2(_(e_readonlyvar), name);
20617 return TRUE;
20618 }
20619 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20620 {
20621 EMSG2(_(e_readonlysbx), name);
20622 return TRUE;
20623 }
20624 return FALSE;
20625}
20626
20627/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020628 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20629 * Also give an error message.
20630 */
20631 static int
20632var_check_fixed(flags, name)
20633 int flags;
20634 char_u *name;
20635{
20636 if (flags & DI_FLAGS_FIX)
20637 {
20638 EMSG2(_("E795: Cannot delete variable %s"), name);
20639 return TRUE;
20640 }
20641 return FALSE;
20642}
20643
20644/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020645 * Check if a funcref is assigned to a valid variable name.
20646 * Return TRUE and give an error if not.
20647 */
20648 static int
20649var_check_func_name(name, new_var)
20650 char_u *name; /* points to start of variable name */
20651 int new_var; /* TRUE when creating the variable */
20652{
20653 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20654 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20655 ? name[2] : name[0]))
20656 {
20657 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20658 name);
20659 return TRUE;
20660 }
20661 /* Don't allow hiding a function. When "v" is not NULL we might be
20662 * assigning another function to the same var, the type is checked
20663 * below. */
20664 if (new_var && function_exists(name))
20665 {
20666 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20667 name);
20668 return TRUE;
20669 }
20670 return FALSE;
20671}
20672
20673/*
20674 * Check if a variable name is valid.
20675 * Return FALSE and give an error if not.
20676 */
20677 static int
20678valid_varname(varname)
20679 char_u *varname;
20680{
20681 char_u *p;
20682
20683 for (p = varname; *p != NUL; ++p)
20684 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20685 && *p != AUTOLOAD_CHAR)
20686 {
20687 EMSG2(_(e_illvar), varname);
20688 return FALSE;
20689 }
20690 return TRUE;
20691}
20692
20693/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020694 * Return TRUE if typeval "tv" is set to be locked (immutable).
20695 * Also give an error message, using "name".
20696 */
20697 static int
20698tv_check_lock(lock, name)
20699 int lock;
20700 char_u *name;
20701{
20702 if (lock & VAR_LOCKED)
20703 {
20704 EMSG2(_("E741: Value is locked: %s"),
20705 name == NULL ? (char_u *)_("Unknown") : name);
20706 return TRUE;
20707 }
20708 if (lock & VAR_FIXED)
20709 {
20710 EMSG2(_("E742: Cannot change value of %s"),
20711 name == NULL ? (char_u *)_("Unknown") : name);
20712 return TRUE;
20713 }
20714 return FALSE;
20715}
20716
20717/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020718 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020719 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020720 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020721 * It is OK for "from" and "to" to point to the same item. This is used to
20722 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020723 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020724 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020725copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020726 typval_T *from;
20727 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020728{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020729 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020730 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020731 switch (from->v_type)
20732 {
20733 case VAR_NUMBER:
20734 to->vval.v_number = from->vval.v_number;
20735 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020736#ifdef FEAT_FLOAT
20737 case VAR_FLOAT:
20738 to->vval.v_float = from->vval.v_float;
20739 break;
20740#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020741 case VAR_STRING:
20742 case VAR_FUNC:
20743 if (from->vval.v_string == NULL)
20744 to->vval.v_string = NULL;
20745 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020746 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020747 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020748 if (from->v_type == VAR_FUNC)
20749 func_ref(to->vval.v_string);
20750 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020751 break;
20752 case VAR_LIST:
20753 if (from->vval.v_list == NULL)
20754 to->vval.v_list = NULL;
20755 else
20756 {
20757 to->vval.v_list = from->vval.v_list;
20758 ++to->vval.v_list->lv_refcount;
20759 }
20760 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020761 case VAR_DICT:
20762 if (from->vval.v_dict == NULL)
20763 to->vval.v_dict = NULL;
20764 else
20765 {
20766 to->vval.v_dict = from->vval.v_dict;
20767 ++to->vval.v_dict->dv_refcount;
20768 }
20769 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020770 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020771 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020772 break;
20773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774}
20775
20776/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020777 * Make a copy of an item.
20778 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020779 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20780 * reference to an already copied list/dict can be used.
20781 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020782 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020783 static int
20784item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020785 typval_T *from;
20786 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020787 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020788 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020789{
20790 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020791 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020792
Bram Moolenaar33570922005-01-25 22:26:29 +000020793 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020794 {
20795 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020796 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020797 }
20798 ++recurse;
20799
20800 switch (from->v_type)
20801 {
20802 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020803#ifdef FEAT_FLOAT
20804 case VAR_FLOAT:
20805#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020806 case VAR_STRING:
20807 case VAR_FUNC:
20808 copy_tv(from, to);
20809 break;
20810 case VAR_LIST:
20811 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020812 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020813 if (from->vval.v_list == NULL)
20814 to->vval.v_list = NULL;
20815 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20816 {
20817 /* use the copy made earlier */
20818 to->vval.v_list = from->vval.v_list->lv_copylist;
20819 ++to->vval.v_list->lv_refcount;
20820 }
20821 else
20822 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20823 if (to->vval.v_list == NULL)
20824 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020825 break;
20826 case VAR_DICT:
20827 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020828 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020829 if (from->vval.v_dict == NULL)
20830 to->vval.v_dict = NULL;
20831 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20832 {
20833 /* use the copy made earlier */
20834 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20835 ++to->vval.v_dict->dv_refcount;
20836 }
20837 else
20838 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20839 if (to->vval.v_dict == NULL)
20840 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020841 break;
20842 default:
20843 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020844 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020845 }
20846 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020847 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020848}
20849
20850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020851 * ":echo expr1 ..." print each argument separated with a space, add a
20852 * newline at the end.
20853 * ":echon expr1 ..." print each argument plain.
20854 */
20855 void
20856ex_echo(eap)
20857 exarg_T *eap;
20858{
20859 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020860 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020861 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862 char_u *p;
20863 int needclr = TRUE;
20864 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020865 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866
20867 if (eap->skip)
20868 ++emsg_skip;
20869 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20870 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020871 /* If eval1() causes an error message the text from the command may
20872 * still need to be cleared. E.g., "echo 22,44". */
20873 need_clr_eos = needclr;
20874
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020876 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020877 {
20878 /*
20879 * Report the invalid expression unless the expression evaluation
20880 * has been cancelled due to an aborting error, an interrupt, or an
20881 * exception.
20882 */
20883 if (!aborting())
20884 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020885 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886 break;
20887 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020888 need_clr_eos = FALSE;
20889
Bram Moolenaar071d4272004-06-13 20:20:40 +000020890 if (!eap->skip)
20891 {
20892 if (atstart)
20893 {
20894 atstart = FALSE;
20895 /* Call msg_start() after eval1(), evaluating the expression
20896 * may cause a message to appear. */
20897 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020898 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020899 /* Mark the saved text as finishing the line, so that what
20900 * follows is displayed on a new line when scrolling back
20901 * at the more prompt. */
20902 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020904 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905 }
20906 else if (eap->cmdidx == CMD_echo)
20907 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020908 current_copyID += COPYID_INC;
20909 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020910 if (p != NULL)
20911 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020913 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020915 if (*p != TAB && needclr)
20916 {
20917 /* remove any text still there from the command */
20918 msg_clr_eos();
20919 needclr = FALSE;
20920 }
20921 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 }
20923 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020924 {
20925#ifdef FEAT_MBYTE
20926 if (has_mbyte)
20927 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020928 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020929
20930 (void)msg_outtrans_len_attr(p, i, echo_attr);
20931 p += i - 1;
20932 }
20933 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020935 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020938 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020940 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 arg = skipwhite(arg);
20942 }
20943 eap->nextcmd = check_nextcmd(arg);
20944
20945 if (eap->skip)
20946 --emsg_skip;
20947 else
20948 {
20949 /* remove text that may still be there from the command */
20950 if (needclr)
20951 msg_clr_eos();
20952 if (eap->cmdidx == CMD_echo)
20953 msg_end();
20954 }
20955}
20956
20957/*
20958 * ":echohl {name}".
20959 */
20960 void
20961ex_echohl(eap)
20962 exarg_T *eap;
20963{
20964 int id;
20965
20966 id = syn_name2id(eap->arg);
20967 if (id == 0)
20968 echo_attr = 0;
20969 else
20970 echo_attr = syn_id2attr(id);
20971}
20972
20973/*
20974 * ":execute expr1 ..." execute the result of an expression.
20975 * ":echomsg expr1 ..." Print a message
20976 * ":echoerr expr1 ..." Print an error
20977 * Each gets spaces around each argument and a newline at the end for
20978 * echo commands
20979 */
20980 void
20981ex_execute(eap)
20982 exarg_T *eap;
20983{
20984 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020985 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 int ret = OK;
20987 char_u *p;
20988 garray_T ga;
20989 int len;
20990 int save_did_emsg;
20991
20992 ga_init2(&ga, 1, 80);
20993
20994 if (eap->skip)
20995 ++emsg_skip;
20996 while (*arg != NUL && *arg != '|' && *arg != '\n')
20997 {
20998 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020999 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021000 {
21001 /*
21002 * Report the invalid expression unless the expression evaluation
21003 * has been cancelled due to an aborting error, an interrupt, or an
21004 * exception.
21005 */
21006 if (!aborting())
21007 EMSG2(_(e_invexpr2), p);
21008 ret = FAIL;
21009 break;
21010 }
21011
21012 if (!eap->skip)
21013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021014 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015 len = (int)STRLEN(p);
21016 if (ga_grow(&ga, len + 2) == FAIL)
21017 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021018 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 ret = FAIL;
21020 break;
21021 }
21022 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025 ga.ga_len += len;
21026 }
21027
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021028 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021029 arg = skipwhite(arg);
21030 }
21031
21032 if (ret != FAIL && ga.ga_data != NULL)
21033 {
21034 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021035 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021037 out_flush();
21038 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021039 else if (eap->cmdidx == CMD_echoerr)
21040 {
21041 /* We don't want to abort following commands, restore did_emsg. */
21042 save_did_emsg = did_emsg;
21043 EMSG((char_u *)ga.ga_data);
21044 if (!force_abort)
21045 did_emsg = save_did_emsg;
21046 }
21047 else if (eap->cmdidx == CMD_execute)
21048 do_cmdline((char_u *)ga.ga_data,
21049 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21050 }
21051
21052 ga_clear(&ga);
21053
21054 if (eap->skip)
21055 --emsg_skip;
21056
21057 eap->nextcmd = check_nextcmd(arg);
21058}
21059
21060/*
21061 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21062 * "arg" points to the "&" or '+' when called, to "option" when returning.
21063 * Returns NULL when no option name found. Otherwise pointer to the char
21064 * after the option name.
21065 */
21066 static char_u *
21067find_option_end(arg, opt_flags)
21068 char_u **arg;
21069 int *opt_flags;
21070{
21071 char_u *p = *arg;
21072
21073 ++p;
21074 if (*p == 'g' && p[1] == ':')
21075 {
21076 *opt_flags = OPT_GLOBAL;
21077 p += 2;
21078 }
21079 else if (*p == 'l' && p[1] == ':')
21080 {
21081 *opt_flags = OPT_LOCAL;
21082 p += 2;
21083 }
21084 else
21085 *opt_flags = 0;
21086
21087 if (!ASCII_ISALPHA(*p))
21088 return NULL;
21089 *arg = p;
21090
21091 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21092 p += 4; /* termcap option */
21093 else
21094 while (ASCII_ISALPHA(*p))
21095 ++p;
21096 return p;
21097}
21098
21099/*
21100 * ":function"
21101 */
21102 void
21103ex_function(eap)
21104 exarg_T *eap;
21105{
21106 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021107 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021108 int j;
21109 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021110 int saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021111 char_u *name = NULL;
21112 char_u *p;
21113 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021114 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021115 garray_T newargs;
21116 garray_T newlines;
21117 int varargs = FALSE;
21118 int mustend = FALSE;
21119 int flags = 0;
21120 ufunc_T *fp;
21121 int indent;
21122 int nesting;
21123 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021124 dictitem_T *v;
21125 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021126 static int func_nr = 0; /* number for nameless function */
21127 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021128 hashtab_T *ht;
21129 int todo;
21130 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021131 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021132
21133 /*
21134 * ":function" without argument: list functions.
21135 */
21136 if (ends_excmd(*eap->arg))
21137 {
21138 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021139 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021140 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021141 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021142 {
21143 if (!HASHITEM_EMPTY(hi))
21144 {
21145 --todo;
21146 fp = HI2UF(hi);
21147 if (!isdigit(*fp->uf_name))
21148 list_func_head(fp, FALSE);
21149 }
21150 }
21151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152 eap->nextcmd = check_nextcmd(eap->arg);
21153 return;
21154 }
21155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021156 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021157 * ":function /pat": list functions matching pattern.
21158 */
21159 if (*eap->arg == '/')
21160 {
21161 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21162 if (!eap->skip)
21163 {
21164 regmatch_T regmatch;
21165
21166 c = *p;
21167 *p = NUL;
21168 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21169 *p = c;
21170 if (regmatch.regprog != NULL)
21171 {
21172 regmatch.rm_ic = p_ic;
21173
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021174 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021175 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21176 {
21177 if (!HASHITEM_EMPTY(hi))
21178 {
21179 --todo;
21180 fp = HI2UF(hi);
21181 if (!isdigit(*fp->uf_name)
21182 && vim_regexec(&regmatch, fp->uf_name, 0))
21183 list_func_head(fp, FALSE);
21184 }
21185 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021186 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021187 }
21188 }
21189 if (*p == '/')
21190 ++p;
21191 eap->nextcmd = check_nextcmd(p);
21192 return;
21193 }
21194
21195 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021196 * Get the function name. There are these situations:
21197 * func normal function name
21198 * "name" == func, "fudi.fd_dict" == NULL
21199 * dict.func new dictionary entry
21200 * "name" == NULL, "fudi.fd_dict" set,
21201 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21202 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021203 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021204 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21205 * dict.func existing dict entry that's not a Funcref
21206 * "name" == NULL, "fudi.fd_dict" set,
21207 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21208 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021210 name = trans_function_name(&p, eap->skip, 0, &fudi);
21211 paren = (vim_strchr(p, '(') != NULL);
21212 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 {
21214 /*
21215 * Return on an invalid expression in braces, unless the expression
21216 * evaluation has been cancelled due to an aborting error, an
21217 * interrupt, or an exception.
21218 */
21219 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021220 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021221 if (!eap->skip && fudi.fd_newkey != NULL)
21222 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021223 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021225 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 else
21227 eap->skip = TRUE;
21228 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021229
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 /* An error in a function call during evaluation of an expression in magic
21231 * braces should not cause the function not to be defined. */
21232 saved_did_emsg = did_emsg;
21233 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021234
21235 /*
21236 * ":function func" with only function name: list function.
21237 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021238 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021239 {
21240 if (!ends_excmd(*skipwhite(p)))
21241 {
21242 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021243 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 }
21245 eap->nextcmd = check_nextcmd(p);
21246 if (eap->nextcmd != NULL)
21247 *p = NUL;
21248 if (!eap->skip && !got_int)
21249 {
21250 fp = find_func(name);
21251 if (fp != NULL)
21252 {
21253 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021254 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021256 if (FUNCLINE(fp, j) == NULL)
21257 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258 msg_putchar('\n');
21259 msg_outnum((long)(j + 1));
21260 if (j < 9)
21261 msg_putchar(' ');
21262 if (j < 99)
21263 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021264 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265 out_flush(); /* show a line at a time */
21266 ui_breakcheck();
21267 }
21268 if (!got_int)
21269 {
21270 msg_putchar('\n');
21271 msg_puts((char_u *)" endfunction");
21272 }
21273 }
21274 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021275 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021277 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 }
21279
21280 /*
21281 * ":function name(arg1, arg2)" Define function.
21282 */
21283 p = skipwhite(p);
21284 if (*p != '(')
21285 {
21286 if (!eap->skip)
21287 {
21288 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021289 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021290 }
21291 /* attempt to continue by skipping some text */
21292 if (vim_strchr(p, '(') != NULL)
21293 p = vim_strchr(p, '(');
21294 }
21295 p = skipwhite(p + 1);
21296
21297 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21298 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21299
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021300 if (!eap->skip)
21301 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021302 /* Check the name of the function. Unless it's a dictionary function
21303 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021304 if (name != NULL)
21305 arg = name;
21306 else
21307 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021308 if (arg != NULL && (fudi.fd_di == NULL
21309 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021310 {
21311 if (*arg == K_SPECIAL)
21312 j = 3;
21313 else
21314 j = 0;
21315 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21316 : eval_isnamec(arg[j])))
21317 ++j;
21318 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021319 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021320 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021321 /* Disallow using the g: dict. */
21322 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21323 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021324 }
21325
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326 /*
21327 * Isolate the arguments: "arg1, arg2, ...)"
21328 */
21329 while (*p != ')')
21330 {
21331 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21332 {
21333 varargs = TRUE;
21334 p += 3;
21335 mustend = TRUE;
21336 }
21337 else
21338 {
21339 arg = p;
21340 while (ASCII_ISALNUM(*p) || *p == '_')
21341 ++p;
21342 if (arg == p || isdigit(*arg)
21343 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21344 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21345 {
21346 if (!eap->skip)
21347 EMSG2(_("E125: Illegal argument: %s"), arg);
21348 break;
21349 }
21350 if (ga_grow(&newargs, 1) == FAIL)
21351 goto erret;
21352 c = *p;
21353 *p = NUL;
21354 arg = vim_strsave(arg);
21355 if (arg == NULL)
21356 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021357
21358 /* Check for duplicate argument name. */
21359 for (i = 0; i < newargs.ga_len; ++i)
21360 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21361 {
21362 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21363 goto erret;
21364 }
21365
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21367 *p = c;
21368 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021369 if (*p == ',')
21370 ++p;
21371 else
21372 mustend = TRUE;
21373 }
21374 p = skipwhite(p);
21375 if (mustend && *p != ')')
21376 {
21377 if (!eap->skip)
21378 EMSG2(_(e_invarg2), eap->arg);
21379 break;
21380 }
21381 }
21382 ++p; /* skip the ')' */
21383
Bram Moolenaare9a41262005-01-15 22:18:47 +000021384 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 for (;;)
21386 {
21387 p = skipwhite(p);
21388 if (STRNCMP(p, "range", 5) == 0)
21389 {
21390 flags |= FC_RANGE;
21391 p += 5;
21392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021393 else if (STRNCMP(p, "dict", 4) == 0)
21394 {
21395 flags |= FC_DICT;
21396 p += 4;
21397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021398 else if (STRNCMP(p, "abort", 5) == 0)
21399 {
21400 flags |= FC_ABORT;
21401 p += 5;
21402 }
21403 else
21404 break;
21405 }
21406
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021407 /* When there is a line break use what follows for the function body.
21408 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21409 if (*p == '\n')
21410 line_arg = p + 1;
21411 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021412 EMSG(_(e_trailing));
21413
21414 /*
21415 * Read the body of the function, until ":endfunction" is found.
21416 */
21417 if (KeyTyped)
21418 {
21419 /* Check if the function already exists, don't let the user type the
21420 * whole function before telling him it doesn't work! For a script we
21421 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021422 if (!eap->skip && !eap->forceit)
21423 {
21424 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21425 EMSG(_(e_funcdict));
21426 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021427 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021428 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021429
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021430 if (!eap->skip && did_emsg)
21431 goto erret;
21432
Bram Moolenaar071d4272004-06-13 20:20:40 +000021433 msg_putchar('\n'); /* don't overwrite the function name */
21434 cmdline_row = msg_row;
21435 }
21436
21437 indent = 2;
21438 nesting = 0;
21439 for (;;)
21440 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021441 if (KeyTyped)
21442 msg_scroll = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021444 sourcing_lnum_off = sourcing_lnum;
21445
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021446 if (line_arg != NULL)
21447 {
21448 /* Use eap->arg, split up in parts by line breaks. */
21449 theline = line_arg;
21450 p = vim_strchr(theline, '\n');
21451 if (p == NULL)
21452 line_arg += STRLEN(line_arg);
21453 else
21454 {
21455 *p = NUL;
21456 line_arg = p + 1;
21457 }
21458 }
21459 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460 theline = getcmdline(':', 0L, indent);
21461 else
21462 theline = eap->getline(':', eap->cookie, indent);
21463 if (KeyTyped)
21464 lines_left = Rows - 1;
21465 if (theline == NULL)
21466 {
21467 EMSG(_("E126: Missing :endfunction"));
21468 goto erret;
21469 }
21470
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021471 /* Detect line continuation: sourcing_lnum increased more than one. */
21472 if (sourcing_lnum > sourcing_lnum_off + 1)
21473 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21474 else
21475 sourcing_lnum_off = 0;
21476
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 if (skip_until != NULL)
21478 {
21479 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21480 * don't check for ":endfunc". */
21481 if (STRCMP(theline, skip_until) == 0)
21482 {
21483 vim_free(skip_until);
21484 skip_until = NULL;
21485 }
21486 }
21487 else
21488 {
21489 /* skip ':' and blanks*/
21490 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21491 ;
21492
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021493 /* Check for "endfunction". */
21494 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021495 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021496 if (line_arg == NULL)
21497 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 break;
21499 }
21500
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021501 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 * at "end". */
21503 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21504 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021505 else if (STRNCMP(p, "if", 2) == 0
21506 || STRNCMP(p, "wh", 2) == 0
21507 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021508 || STRNCMP(p, "try", 3) == 0)
21509 indent += 2;
21510
21511 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021512 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021514 if (*p == '!')
21515 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 p += eval_fname_script(p);
21517 if (ASCII_ISALPHA(*p))
21518 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021519 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520 if (*skipwhite(p) == '(')
21521 {
21522 ++nesting;
21523 indent += 2;
21524 }
21525 }
21526 }
21527
21528 /* Check for ":append" or ":insert". */
21529 p = skip_range(p, NULL);
21530 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21531 || (p[0] == 'i'
21532 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21533 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21534 skip_until = vim_strsave((char_u *)".");
21535
21536 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21537 arg = skipwhite(skiptowhite(p));
21538 if (arg[0] == '<' && arg[1] =='<'
21539 && ((p[0] == 'p' && p[1] == 'y'
21540 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21541 || (p[0] == 'p' && p[1] == 'e'
21542 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21543 || (p[0] == 't' && p[1] == 'c'
21544 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021545 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21546 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021547 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21548 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021549 || (p[0] == 'm' && p[1] == 'z'
21550 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551 ))
21552 {
21553 /* ":python <<" continues until a dot, like ":append" */
21554 p = skipwhite(arg + 2);
21555 if (*p == NUL)
21556 skip_until = vim_strsave((char_u *)".");
21557 else
21558 skip_until = vim_strsave(p);
21559 }
21560 }
21561
21562 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021563 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021564 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021565 if (line_arg == NULL)
21566 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021568 }
21569
21570 /* Copy the line to newly allocated memory. get_one_sourceline()
21571 * allocates 250 bytes per line, this saves 80% on average. The cost
21572 * is an extra alloc/free. */
21573 p = vim_strsave(theline);
21574 if (p != NULL)
21575 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021576 if (line_arg == NULL)
21577 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021578 theline = p;
21579 }
21580
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021581 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21582
21583 /* Add NULL lines for continuation lines, so that the line count is
21584 * equal to the index in the growarray. */
21585 while (sourcing_lnum_off-- > 0)
21586 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021587
21588 /* Check for end of eap->arg. */
21589 if (line_arg != NULL && *line_arg == NUL)
21590 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021591 }
21592
21593 /* Don't define the function when skipping commands or when an error was
21594 * detected. */
21595 if (eap->skip || did_emsg)
21596 goto erret;
21597
21598 /*
21599 * If there are no errors, add the function
21600 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021601 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021602 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021603 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021604 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021605 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021606 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021607 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021608 goto erret;
21609 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021610
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021611 fp = find_func(name);
21612 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021614 if (!eap->forceit)
21615 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021616 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021617 goto erret;
21618 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021619 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021620 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021621 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021622 name);
21623 goto erret;
21624 }
21625 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021626 ga_clear_strings(&(fp->uf_args));
21627 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021628 vim_free(name);
21629 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021631 }
21632 else
21633 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021634 char numbuf[20];
21635
21636 fp = NULL;
21637 if (fudi.fd_newkey == NULL && !eap->forceit)
21638 {
21639 EMSG(_(e_funcdict));
21640 goto erret;
21641 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021642 if (fudi.fd_di == NULL)
21643 {
21644 /* Can't add a function to a locked dictionary */
21645 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21646 goto erret;
21647 }
21648 /* Can't change an existing function if it is locked */
21649 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21650 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021651
21652 /* Give the function a sequential number. Can only be used with a
21653 * Funcref! */
21654 vim_free(name);
21655 sprintf(numbuf, "%d", ++func_nr);
21656 name = vim_strsave((char_u *)numbuf);
21657 if (name == NULL)
21658 goto erret;
21659 }
21660
21661 if (fp == NULL)
21662 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021663 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021664 {
21665 int slen, plen;
21666 char_u *scriptname;
21667
21668 /* Check that the autoload name matches the script name. */
21669 j = FAIL;
21670 if (sourcing_name != NULL)
21671 {
21672 scriptname = autoload_name(name);
21673 if (scriptname != NULL)
21674 {
21675 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021676 plen = (int)STRLEN(p);
21677 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021678 if (slen > plen && fnamecmp(p,
21679 sourcing_name + slen - plen) == 0)
21680 j = OK;
21681 vim_free(scriptname);
21682 }
21683 }
21684 if (j == FAIL)
21685 {
21686 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21687 goto erret;
21688 }
21689 }
21690
21691 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021692 if (fp == NULL)
21693 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021694
21695 if (fudi.fd_dict != NULL)
21696 {
21697 if (fudi.fd_di == NULL)
21698 {
21699 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021700 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021701 if (fudi.fd_di == NULL)
21702 {
21703 vim_free(fp);
21704 goto erret;
21705 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021706 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21707 {
21708 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021709 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021710 goto erret;
21711 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021712 }
21713 else
21714 /* overwrite existing dict entry */
21715 clear_tv(&fudi.fd_di->di_tv);
21716 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021717 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021718 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021719 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021720
21721 /* behave like "dict" was used */
21722 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021723 }
21724
Bram Moolenaar071d4272004-06-13 20:20:40 +000021725 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021726 STRCPY(fp->uf_name, name);
21727 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021729 fp->uf_args = newargs;
21730 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021731#ifdef FEAT_PROFILE
21732 fp->uf_tml_count = NULL;
21733 fp->uf_tml_total = NULL;
21734 fp->uf_tml_self = NULL;
21735 fp->uf_profiling = FALSE;
21736 if (prof_def_func())
21737 func_do_profile(fp);
21738#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021739 fp->uf_varargs = varargs;
21740 fp->uf_flags = flags;
21741 fp->uf_calls = 0;
21742 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021743 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744
21745erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746 ga_clear_strings(&newargs);
21747 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021748ret_free:
21749 vim_free(skip_until);
21750 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021751 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752 did_emsg |= saved_did_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753}
21754
21755/*
21756 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021757 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021759 * flags:
21760 * TFN_INT: internal function name OK
21761 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762 * Advances "pp" to just after the function name (if no error).
21763 */
21764 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021765trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766 char_u **pp;
21767 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021768 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021769 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021770{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021771 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021772 char_u *start;
21773 char_u *end;
21774 int lead;
21775 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021777 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021778
21779 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021780 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021782
21783 /* Check for hard coded <SNR>: already translated function ID (from a user
21784 * command). */
21785 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21786 && (*pp)[2] == (int)KE_SNR)
21787 {
21788 *pp += 3;
21789 len = get_id_len(pp) + 3;
21790 return vim_strnsave(start, len);
21791 }
21792
21793 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21794 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021796 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021797 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021798
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021799 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21800 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021801 if (end == start)
21802 {
21803 if (!skip)
21804 EMSG(_("E129: Function name required"));
21805 goto theend;
21806 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021807 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021808 {
21809 /*
21810 * Report an invalid expression in braces, unless the expression
21811 * evaluation has been cancelled due to an aborting error, an
21812 * interrupt, or an exception.
21813 */
21814 if (!aborting())
21815 {
21816 if (end != NULL)
21817 EMSG2(_(e_invarg2), start);
21818 }
21819 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021820 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021821 goto theend;
21822 }
21823
21824 if (lv.ll_tv != NULL)
21825 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021826 if (fdp != NULL)
21827 {
21828 fdp->fd_dict = lv.ll_dict;
21829 fdp->fd_newkey = lv.ll_newkey;
21830 lv.ll_newkey = NULL;
21831 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021832 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021833 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21834 {
21835 name = vim_strsave(lv.ll_tv->vval.v_string);
21836 *pp = end;
21837 }
21838 else
21839 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021840 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21841 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021842 EMSG(_(e_funcref));
21843 else
21844 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021845 name = NULL;
21846 }
21847 goto theend;
21848 }
21849
21850 if (lv.ll_name == NULL)
21851 {
21852 /* Error found, but continue after the function name. */
21853 *pp = end;
21854 goto theend;
21855 }
21856
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021857 /* Check if the name is a Funcref. If so, use the value. */
21858 if (lv.ll_exp_name != NULL)
21859 {
21860 len = (int)STRLEN(lv.ll_exp_name);
21861 name = deref_func_name(lv.ll_exp_name, &len);
21862 if (name == lv.ll_exp_name)
21863 name = NULL;
21864 }
21865 else
21866 {
21867 len = (int)(end - *pp);
21868 name = deref_func_name(*pp, &len);
21869 if (name == *pp)
21870 name = NULL;
21871 }
21872 if (name != NULL)
21873 {
21874 name = vim_strsave(name);
21875 *pp = end;
21876 goto theend;
21877 }
21878
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021879 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021880 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021881 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021882 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21883 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21884 {
21885 /* When there was "s:" already or the name expanded to get a
21886 * leading "s:" then remove it. */
21887 lv.ll_name += 2;
21888 len -= 2;
21889 lead = 2;
21890 }
21891 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021892 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021893 {
21894 if (lead == 2) /* skip over "s:" */
21895 lv.ll_name += 2;
21896 len = (int)(end - lv.ll_name);
21897 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021898
21899 /*
21900 * Copy the function name to allocated memory.
21901 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21902 * Accept <SNR>123_name() outside a script.
21903 */
21904 if (skip)
21905 lead = 0; /* do nothing */
21906 else if (lead > 0)
21907 {
21908 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021909 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21910 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021911 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021912 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021913 if (current_SID <= 0)
21914 {
21915 EMSG(_(e_usingsid));
21916 goto theend;
21917 }
21918 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21919 lead += (int)STRLEN(sid_buf);
21920 }
21921 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021922 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021923 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021924 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021925 goto theend;
21926 }
21927 name = alloc((unsigned)(len + lead + 1));
21928 if (name != NULL)
21929 {
21930 if (lead > 0)
21931 {
21932 name[0] = K_SPECIAL;
21933 name[1] = KS_EXTRA;
21934 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021935 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021936 STRCPY(name + 3, sid_buf);
21937 }
21938 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21939 name[len + lead] = NUL;
21940 }
21941 *pp = end;
21942
21943theend:
21944 clear_lval(&lv);
21945 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946}
21947
21948/*
21949 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21950 * Return 2 if "p" starts with "s:".
21951 * Return 0 otherwise.
21952 */
21953 static int
21954eval_fname_script(p)
21955 char_u *p;
21956{
21957 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21958 || STRNICMP(p + 1, "SNR>", 4) == 0))
21959 return 5;
21960 if (p[0] == 's' && p[1] == ':')
21961 return 2;
21962 return 0;
21963}
21964
21965/*
21966 * Return TRUE if "p" starts with "<SID>" or "s:".
21967 * Only works if eval_fname_script() returned non-zero for "p"!
21968 */
21969 static int
21970eval_fname_sid(p)
21971 char_u *p;
21972{
21973 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21974}
21975
21976/*
21977 * List the head of the function: "name(arg1, arg2)".
21978 */
21979 static void
21980list_func_head(fp, indent)
21981 ufunc_T *fp;
21982 int indent;
21983{
21984 int j;
21985
21986 msg_start();
21987 if (indent)
21988 MSG_PUTS(" ");
21989 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021990 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 {
21992 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021993 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 }
21995 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021996 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021998 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999 {
22000 if (j)
22001 MSG_PUTS(", ");
22002 msg_puts(FUNCARG(fp, j));
22003 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022004 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022005 {
22006 if (j)
22007 MSG_PUTS(", ");
22008 MSG_PUTS("...");
22009 }
22010 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022011 if (fp->uf_flags & FC_ABORT)
22012 MSG_PUTS(" abort");
22013 if (fp->uf_flags & FC_RANGE)
22014 MSG_PUTS(" range");
22015 if (fp->uf_flags & FC_DICT)
22016 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022017 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022018 if (p_verbose > 0)
22019 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020}
22021
22022/*
22023 * Find a function by name, return pointer to it in ufuncs.
22024 * Return NULL for unknown function.
22025 */
22026 static ufunc_T *
22027find_func(name)
22028 char_u *name;
22029{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022030 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022031
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022032 hi = hash_find(&func_hashtab, name);
22033 if (!HASHITEM_EMPTY(hi))
22034 return HI2UF(hi);
22035 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036}
22037
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022038#if defined(EXITFREE) || defined(PROTO)
22039 void
22040free_all_functions()
22041{
22042 hashitem_T *hi;
22043
22044 /* Need to start all over every time, because func_free() may change the
22045 * hash table. */
22046 while (func_hashtab.ht_used > 0)
22047 for (hi = func_hashtab.ht_array; ; ++hi)
22048 if (!HASHITEM_EMPTY(hi))
22049 {
22050 func_free(HI2UF(hi));
22051 break;
22052 }
22053}
22054#endif
22055
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022056 int
22057translated_function_exists(name)
22058 char_u *name;
22059{
22060 if (builtin_function(name))
22061 return find_internal_func(name) >= 0;
22062 return find_func(name) != NULL;
22063}
22064
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022065/*
22066 * Return TRUE if a function "name" exists.
22067 */
22068 static int
22069function_exists(name)
22070 char_u *name;
22071{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022072 char_u *nm = name;
22073 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022074 int n = FALSE;
22075
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022076 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022077 nm = skipwhite(nm);
22078
22079 /* Only accept "funcname", "funcname ", "funcname (..." and
22080 * "funcname(...", not "funcname!...". */
22081 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022082 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022083 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022084 return n;
22085}
22086
Bram Moolenaara1544c02013-05-30 12:35:52 +020022087 char_u *
22088get_expanded_name(name, check)
22089 char_u *name;
22090 int check;
22091{
22092 char_u *nm = name;
22093 char_u *p;
22094
22095 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22096
22097 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022098 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022099 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022100
Bram Moolenaara1544c02013-05-30 12:35:52 +020022101 vim_free(p);
22102 return NULL;
22103}
22104
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022105/*
22106 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022107 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022108 */
22109 static int
22110builtin_function(name)
22111 char_u *name;
22112{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022113 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22114 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022115}
22116
Bram Moolenaar05159a02005-02-26 23:04:13 +000022117#if defined(FEAT_PROFILE) || defined(PROTO)
22118/*
22119 * Start profiling function "fp".
22120 */
22121 static void
22122func_do_profile(fp)
22123 ufunc_T *fp;
22124{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022125 int len = fp->uf_lines.ga_len;
22126
22127 if (len == 0)
22128 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022129 fp->uf_tm_count = 0;
22130 profile_zero(&fp->uf_tm_self);
22131 profile_zero(&fp->uf_tm_total);
22132 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022133 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022134 if (fp->uf_tml_total == NULL)
22135 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022136 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022137 if (fp->uf_tml_self == NULL)
22138 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022139 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022140 fp->uf_tml_idx = -1;
22141 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22142 || fp->uf_tml_self == NULL)
22143 return; /* out of memory */
22144
22145 fp->uf_profiling = TRUE;
22146}
22147
22148/*
22149 * Dump the profiling results for all functions in file "fd".
22150 */
22151 void
22152func_dump_profile(fd)
22153 FILE *fd;
22154{
22155 hashitem_T *hi;
22156 int todo;
22157 ufunc_T *fp;
22158 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022159 ufunc_T **sorttab;
22160 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022161
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022162 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022163 if (todo == 0)
22164 return; /* nothing to dump */
22165
Bram Moolenaar73830342005-02-28 22:48:19 +000022166 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22167
Bram Moolenaar05159a02005-02-26 23:04:13 +000022168 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22169 {
22170 if (!HASHITEM_EMPTY(hi))
22171 {
22172 --todo;
22173 fp = HI2UF(hi);
22174 if (fp->uf_profiling)
22175 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022176 if (sorttab != NULL)
22177 sorttab[st_len++] = fp;
22178
Bram Moolenaar05159a02005-02-26 23:04:13 +000022179 if (fp->uf_name[0] == K_SPECIAL)
22180 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22181 else
22182 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22183 if (fp->uf_tm_count == 1)
22184 fprintf(fd, "Called 1 time\n");
22185 else
22186 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22187 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22188 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22189 fprintf(fd, "\n");
22190 fprintf(fd, "count total (s) self (s)\n");
22191
22192 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22193 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022194 if (FUNCLINE(fp, i) == NULL)
22195 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022196 prof_func_line(fd, fp->uf_tml_count[i],
22197 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022198 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22199 }
22200 fprintf(fd, "\n");
22201 }
22202 }
22203 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022204
22205 if (sorttab != NULL && st_len > 0)
22206 {
22207 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22208 prof_total_cmp);
22209 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22210 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22211 prof_self_cmp);
22212 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22213 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022214
22215 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022216}
Bram Moolenaar73830342005-02-28 22:48:19 +000022217
22218 static void
22219prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22220 FILE *fd;
22221 ufunc_T **sorttab;
22222 int st_len;
22223 char *title;
22224 int prefer_self; /* when equal print only self time */
22225{
22226 int i;
22227 ufunc_T *fp;
22228
22229 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22230 fprintf(fd, "count total (s) self (s) function\n");
22231 for (i = 0; i < 20 && i < st_len; ++i)
22232 {
22233 fp = sorttab[i];
22234 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22235 prefer_self);
22236 if (fp->uf_name[0] == K_SPECIAL)
22237 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22238 else
22239 fprintf(fd, " %s()\n", fp->uf_name);
22240 }
22241 fprintf(fd, "\n");
22242}
22243
22244/*
22245 * Print the count and times for one function or function line.
22246 */
22247 static void
22248prof_func_line(fd, count, total, self, prefer_self)
22249 FILE *fd;
22250 int count;
22251 proftime_T *total;
22252 proftime_T *self;
22253 int prefer_self; /* when equal print only self time */
22254{
22255 if (count > 0)
22256 {
22257 fprintf(fd, "%5d ", count);
22258 if (prefer_self && profile_equal(total, self))
22259 fprintf(fd, " ");
22260 else
22261 fprintf(fd, "%s ", profile_msg(total));
22262 if (!prefer_self && profile_equal(total, self))
22263 fprintf(fd, " ");
22264 else
22265 fprintf(fd, "%s ", profile_msg(self));
22266 }
22267 else
22268 fprintf(fd, " ");
22269}
22270
22271/*
22272 * Compare function for total time sorting.
22273 */
22274 static int
22275#ifdef __BORLANDC__
22276_RTLENTRYF
22277#endif
22278prof_total_cmp(s1, s2)
22279 const void *s1;
22280 const void *s2;
22281{
22282 ufunc_T *p1, *p2;
22283
22284 p1 = *(ufunc_T **)s1;
22285 p2 = *(ufunc_T **)s2;
22286 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22287}
22288
22289/*
22290 * Compare function for self time sorting.
22291 */
22292 static int
22293#ifdef __BORLANDC__
22294_RTLENTRYF
22295#endif
22296prof_self_cmp(s1, s2)
22297 const void *s1;
22298 const void *s2;
22299{
22300 ufunc_T *p1, *p2;
22301
22302 p1 = *(ufunc_T **)s1;
22303 p2 = *(ufunc_T **)s2;
22304 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22305}
22306
Bram Moolenaar05159a02005-02-26 23:04:13 +000022307#endif
22308
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022309/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022310 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022311 * Return TRUE if a package was loaded.
22312 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022313 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022314script_autoload(name, reload)
22315 char_u *name;
22316 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022317{
22318 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022319 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022320 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022321 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022322
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022323 /* Return quickly when autoload disabled. */
22324 if (no_autoload)
22325 return FALSE;
22326
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022327 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022328 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022329 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022330 return FALSE;
22331
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022332 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022333
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022334 /* Find the name in the list of previously loaded package names. Skip
22335 * "autoload/", it's always the same. */
22336 for (i = 0; i < ga_loaded.ga_len; ++i)
22337 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22338 break;
22339 if (!reload && i < ga_loaded.ga_len)
22340 ret = FALSE; /* was loaded already */
22341 else
22342 {
22343 /* Remember the name if it wasn't loaded already. */
22344 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22345 {
22346 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22347 tofree = NULL;
22348 }
22349
22350 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022351 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022352 ret = TRUE;
22353 }
22354
22355 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022356 return ret;
22357}
22358
22359/*
22360 * Return the autoload script name for a function or variable name.
22361 * Returns NULL when out of memory.
22362 */
22363 static char_u *
22364autoload_name(name)
22365 char_u *name;
22366{
22367 char_u *p;
22368 char_u *scriptname;
22369
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022370 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022371 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22372 if (scriptname == NULL)
22373 return FALSE;
22374 STRCPY(scriptname, "autoload/");
22375 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022376 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022377 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022378 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022379 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022380 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022381}
22382
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22384
22385/*
22386 * Function given to ExpandGeneric() to obtain the list of user defined
22387 * function names.
22388 */
22389 char_u *
22390get_user_func_name(xp, idx)
22391 expand_T *xp;
22392 int idx;
22393{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022394 static long_u done;
22395 static hashitem_T *hi;
22396 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397
22398 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022399 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022400 done = 0;
22401 hi = func_hashtab.ht_array;
22402 }
22403 if (done < func_hashtab.ht_used)
22404 {
22405 if (done++ > 0)
22406 ++hi;
22407 while (HASHITEM_EMPTY(hi))
22408 ++hi;
22409 fp = HI2UF(hi);
22410
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022411 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022412 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022413
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022414 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22415 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022416
22417 cat_func_name(IObuff, fp);
22418 if (xp->xp_context != EXPAND_USER_FUNC)
22419 {
22420 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022421 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 STRCAT(IObuff, ")");
22423 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022424 return IObuff;
22425 }
22426 return NULL;
22427}
22428
22429#endif /* FEAT_CMDL_COMPL */
22430
22431/*
22432 * Copy the function name of "fp" to buffer "buf".
22433 * "buf" must be able to hold the function name plus three bytes.
22434 * Takes care of script-local function names.
22435 */
22436 static void
22437cat_func_name(buf, fp)
22438 char_u *buf;
22439 ufunc_T *fp;
22440{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022441 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442 {
22443 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022444 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 }
22446 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022447 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022448}
22449
22450/*
22451 * ":delfunction {name}"
22452 */
22453 void
22454ex_delfunction(eap)
22455 exarg_T *eap;
22456{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022457 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022458 char_u *p;
22459 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022460 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461
22462 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022463 name = trans_function_name(&p, eap->skip, 0, &fudi);
22464 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022466 {
22467 if (fudi.fd_dict != NULL && !eap->skip)
22468 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471 if (!ends_excmd(*skipwhite(p)))
22472 {
22473 vim_free(name);
22474 EMSG(_(e_trailing));
22475 return;
22476 }
22477 eap->nextcmd = check_nextcmd(p);
22478 if (eap->nextcmd != NULL)
22479 *p = NUL;
22480
22481 if (!eap->skip)
22482 fp = find_func(name);
22483 vim_free(name);
22484
22485 if (!eap->skip)
22486 {
22487 if (fp == NULL)
22488 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022489 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490 return;
22491 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022492 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493 {
22494 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22495 return;
22496 }
22497
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022498 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022500 /* Delete the dict item that refers to the function, it will
22501 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022502 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022504 else
22505 func_free(fp);
22506 }
22507}
22508
22509/*
22510 * Free a function and remove it from the list of functions.
22511 */
22512 static void
22513func_free(fp)
22514 ufunc_T *fp;
22515{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022516 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022517
22518 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022519 ga_clear_strings(&(fp->uf_args));
22520 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022521#ifdef FEAT_PROFILE
22522 vim_free(fp->uf_tml_count);
22523 vim_free(fp->uf_tml_total);
22524 vim_free(fp->uf_tml_self);
22525#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022526
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022527 /* remove the function from the function hashtable */
22528 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22529 if (HASHITEM_EMPTY(hi))
22530 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022531 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022532 hash_remove(&func_hashtab, hi);
22533
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022534 vim_free(fp);
22535}
22536
22537/*
22538 * Unreference a Function: decrement the reference count and free it when it
22539 * becomes zero. Only for numbered functions.
22540 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022541 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022542func_unref(name)
22543 char_u *name;
22544{
22545 ufunc_T *fp;
22546
22547 if (name != NULL && isdigit(*name))
22548 {
22549 fp = find_func(name);
22550 if (fp == NULL)
22551 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022552 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022553 {
22554 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022555 * when "uf_calls" becomes zero. */
22556 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022557 func_free(fp);
22558 }
22559 }
22560}
22561
22562/*
22563 * Count a reference to a Function.
22564 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022565 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022566func_ref(name)
22567 char_u *name;
22568{
22569 ufunc_T *fp;
22570
22571 if (name != NULL && isdigit(*name))
22572 {
22573 fp = find_func(name);
22574 if (fp == NULL)
22575 EMSG2(_(e_intern2), "func_ref()");
22576 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022577 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578 }
22579}
22580
22581/*
22582 * Call a user function.
22583 */
22584 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022585call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586 ufunc_T *fp; /* pointer to function */
22587 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022588 typval_T *argvars; /* arguments */
22589 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022590 linenr_T firstline; /* first line of range */
22591 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022592 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593{
Bram Moolenaar33570922005-01-25 22:26:29 +000022594 char_u *save_sourcing_name;
22595 linenr_T save_sourcing_lnum;
22596 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022597 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022598 int save_did_emsg;
22599 static int depth = 0;
22600 dictitem_T *v;
22601 int fixvar_idx = 0; /* index in fixvar[] */
22602 int i;
22603 int ai;
22604 char_u numbuf[NUMBUFLEN];
22605 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022606#ifdef FEAT_PROFILE
22607 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022608 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022610
22611 /* If depth of calling is getting too high, don't execute the function */
22612 if (depth >= p_mfd)
22613 {
22614 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022615 rettv->v_type = VAR_NUMBER;
22616 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022617 return;
22618 }
22619 ++depth;
22620
22621 line_breakcheck(); /* check for CTRL-C hit */
22622
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022623 fc = (funccall_T *)alloc(sizeof(funccall_T));
22624 fc->caller = current_funccal;
22625 current_funccal = fc;
22626 fc->func = fp;
22627 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022628 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022629 fc->linenr = 0;
22630 fc->returned = FALSE;
22631 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022633 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22634 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022635
Bram Moolenaar33570922005-01-25 22:26:29 +000022636 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022637 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022638 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22639 * each argument variable and saves a lot of time.
22640 */
22641 /*
22642 * Init l: variables.
22643 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022644 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022645 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022646 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022647 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22648 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022649 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022650 name = v->di_key;
22651 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022652 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022653 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022654 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022655 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022656 v->di_tv.vval.v_dict = selfdict;
22657 ++selfdict->dv_refcount;
22658 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022659
Bram Moolenaar33570922005-01-25 22:26:29 +000022660 /*
22661 * Init a: variables.
22662 * Set a:0 to "argcount".
22663 * Set a:000 to a list with room for the "..." arguments.
22664 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022665 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022666 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022667 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022668 /* Use "name" to avoid a warning from some compiler that checks the
22669 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022670 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022671 name = v->di_key;
22672 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022673 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022674 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022675 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022676 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022677 v->di_tv.vval.v_list = &fc->l_varlist;
22678 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22679 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22680 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022681
22682 /*
22683 * Set a:firstline to "firstline" and a:lastline to "lastline".
22684 * Set a:name to named arguments.
22685 * Set a:N to the "..." arguments.
22686 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022687 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022688 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022689 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022690 (varnumber_T)lastline);
22691 for (i = 0; i < argcount; ++i)
22692 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022693 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022694 if (ai < 0)
22695 /* named argument a:name */
22696 name = FUNCARG(fp, i);
22697 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022698 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022699 /* "..." argument a:1, a:2, etc. */
22700 sprintf((char *)numbuf, "%d", ai + 1);
22701 name = numbuf;
22702 }
22703 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22704 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022705 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022706 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22707 }
22708 else
22709 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022710 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22711 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022712 if (v == NULL)
22713 break;
22714 v->di_flags = DI_FLAGS_RO;
22715 }
22716 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022717 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022718
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022719 /* Note: the values are copied directly to avoid alloc/free.
22720 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022721 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022722 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022723
22724 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22725 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022726 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22727 fc->l_listitems[ai].li_tv = argvars[i];
22728 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022729 }
22730 }
22731
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732 /* Don't redraw while executing the function. */
22733 ++RedrawingDisabled;
22734 save_sourcing_name = sourcing_name;
22735 save_sourcing_lnum = sourcing_lnum;
22736 sourcing_lnum = 1;
22737 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022738 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739 if (sourcing_name != NULL)
22740 {
22741 if (save_sourcing_name != NULL
22742 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22743 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22744 else
22745 STRCPY(sourcing_name, "function ");
22746 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22747
22748 if (p_verbose >= 12)
22749 {
22750 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022751 verbose_enter_scroll();
22752
Bram Moolenaar555b2802005-05-19 21:08:39 +000022753 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754 if (p_verbose >= 14)
22755 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022756 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022757 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022758 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022759 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760
22761 msg_puts((char_u *)"(");
22762 for (i = 0; i < argcount; ++i)
22763 {
22764 if (i > 0)
22765 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022766 if (argvars[i].v_type == VAR_NUMBER)
22767 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022768 else
22769 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022770 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22771 if (s != NULL)
22772 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022773 if (vim_strsize(s) > MSG_BUF_CLEN)
22774 {
22775 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22776 s = buf;
22777 }
22778 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022779 vim_free(tofree);
22780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022781 }
22782 }
22783 msg_puts((char_u *)")");
22784 }
22785 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022786
22787 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022788 --no_wait_return;
22789 }
22790 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022791#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022792 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022793 {
22794 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22795 func_do_profile(fp);
22796 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022797 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022798 {
22799 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022800 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022801 profile_zero(&fp->uf_tm_children);
22802 }
22803 script_prof_save(&wait_start);
22804 }
22805#endif
22806
Bram Moolenaar071d4272004-06-13 20:20:40 +000022807 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022808 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809 save_did_emsg = did_emsg;
22810 did_emsg = FALSE;
22811
22812 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022813 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022814 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22815
22816 --RedrawingDisabled;
22817
22818 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022819 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022820 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022821 clear_tv(rettv);
22822 rettv->v_type = VAR_NUMBER;
22823 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824 }
22825
Bram Moolenaar05159a02005-02-26 23:04:13 +000022826#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022827 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022828 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022829 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022830 profile_end(&call_start);
22831 profile_sub_wait(&wait_start, &call_start);
22832 profile_add(&fp->uf_tm_total, &call_start);
22833 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022834 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022835 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022836 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22837 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022838 }
22839 }
22840#endif
22841
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842 /* when being verbose, mention the return value */
22843 if (p_verbose >= 12)
22844 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022846 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022849 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022850 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022851 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022852 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022853 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022855 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022856 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022857 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022858 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022859
Bram Moolenaar555b2802005-05-19 21:08:39 +000022860 /* The value may be very long. Skip the middle part, so that we
22861 * have some idea how it starts and ends. smsg() would always
22862 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022863 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022864 if (s != NULL)
22865 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022866 if (vim_strsize(s) > MSG_BUF_CLEN)
22867 {
22868 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22869 s = buf;
22870 }
22871 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022872 vim_free(tofree);
22873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022874 }
22875 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022876
22877 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878 --no_wait_return;
22879 }
22880
22881 vim_free(sourcing_name);
22882 sourcing_name = save_sourcing_name;
22883 sourcing_lnum = save_sourcing_lnum;
22884 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022885#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022886 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022887 script_prof_restore(&wait_start);
22888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022889
22890 if (p_verbose >= 12 && sourcing_name != NULL)
22891 {
22892 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022893 verbose_enter_scroll();
22894
Bram Moolenaar555b2802005-05-19 21:08:39 +000022895 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022897
22898 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022899 --no_wait_return;
22900 }
22901
22902 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022903 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022904 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022905
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022906 /* If the a:000 list and the l: and a: dicts are not referenced we can
22907 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022908 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22909 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22910 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22911 {
22912 free_funccal(fc, FALSE);
22913 }
22914 else
22915 {
22916 hashitem_T *hi;
22917 listitem_T *li;
22918 int todo;
22919
22920 /* "fc" is still in use. This can happen when returning "a:000" or
22921 * assigning "l:" to a global variable.
22922 * Link "fc" in the list for garbage collection later. */
22923 fc->caller = previous_funccal;
22924 previous_funccal = fc;
22925
22926 /* Make a copy of the a: variables, since we didn't do that above. */
22927 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22928 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22929 {
22930 if (!HASHITEM_EMPTY(hi))
22931 {
22932 --todo;
22933 v = HI2DI(hi);
22934 copy_tv(&v->di_tv, &v->di_tv);
22935 }
22936 }
22937
22938 /* Make a copy of the a:000 items, since we didn't do that above. */
22939 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22940 copy_tv(&li->li_tv, &li->li_tv);
22941 }
22942}
22943
22944/*
22945 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022946 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022947 */
22948 static int
22949can_free_funccal(fc, copyID)
22950 funccall_T *fc;
22951 int copyID;
22952{
22953 return (fc->l_varlist.lv_copyID != copyID
22954 && fc->l_vars.dv_copyID != copyID
22955 && fc->l_avars.dv_copyID != copyID);
22956}
22957
22958/*
22959 * Free "fc" and what it contains.
22960 */
22961 static void
22962free_funccal(fc, free_val)
22963 funccall_T *fc;
22964 int free_val; /* a: vars were allocated */
22965{
22966 listitem_T *li;
22967
22968 /* The a: variables typevals may not have been allocated, only free the
22969 * allocated variables. */
22970 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22971
22972 /* free all l: variables */
22973 vars_clear(&fc->l_vars.dv_hashtab);
22974
22975 /* Free the a:000 variables if they were allocated. */
22976 if (free_val)
22977 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22978 clear_tv(&li->li_tv);
22979
22980 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022981}
22982
22983/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022984 * Add a number variable "name" to dict "dp" with value "nr".
22985 */
22986 static void
22987add_nr_var(dp, v, name, nr)
22988 dict_T *dp;
22989 dictitem_T *v;
22990 char *name;
22991 varnumber_T nr;
22992{
22993 STRCPY(v->di_key, name);
22994 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22995 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
22996 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022997 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022998 v->di_tv.vval.v_number = nr;
22999}
23000
23001/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023002 * ":return [expr]"
23003 */
23004 void
23005ex_return(eap)
23006 exarg_T *eap;
23007{
23008 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023009 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023010 int returning = FALSE;
23011
23012 if (current_funccal == NULL)
23013 {
23014 EMSG(_("E133: :return not inside a function"));
23015 return;
23016 }
23017
23018 if (eap->skip)
23019 ++emsg_skip;
23020
23021 eap->nextcmd = NULL;
23022 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023023 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024 {
23025 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023026 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023027 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023028 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 }
23030 /* It's safer to return also on error. */
23031 else if (!eap->skip)
23032 {
23033 /*
23034 * Return unless the expression evaluation has been cancelled due to an
23035 * aborting error, an interrupt, or an exception.
23036 */
23037 if (!aborting())
23038 returning = do_return(eap, FALSE, TRUE, NULL);
23039 }
23040
23041 /* When skipping or the return gets pending, advance to the next command
23042 * in this line (!returning). Otherwise, ignore the rest of the line.
23043 * Following lines will be ignored by get_func_line(). */
23044 if (returning)
23045 eap->nextcmd = NULL;
23046 else if (eap->nextcmd == NULL) /* no argument */
23047 eap->nextcmd = check_nextcmd(arg);
23048
23049 if (eap->skip)
23050 --emsg_skip;
23051}
23052
23053/*
23054 * Return from a function. Possibly makes the return pending. Also called
23055 * for a pending return at the ":endtry" or after returning from an extra
23056 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023057 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023058 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023059 * FALSE when the return gets pending.
23060 */
23061 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023062do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063 exarg_T *eap;
23064 int reanimate;
23065 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023066 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067{
23068 int idx;
23069 struct condstack *cstack = eap->cstack;
23070
23071 if (reanimate)
23072 /* Undo the return. */
23073 current_funccal->returned = FALSE;
23074
23075 /*
23076 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23077 * not in its finally clause (which then is to be executed next) is found.
23078 * In this case, make the ":return" pending for execution at the ":endtry".
23079 * Otherwise, return normally.
23080 */
23081 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23082 if (idx >= 0)
23083 {
23084 cstack->cs_pending[idx] = CSTP_RETURN;
23085
23086 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023087 /* A pending return again gets pending. "rettv" points to an
23088 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023090 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 else
23092 {
23093 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023094 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023095 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023096 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023098 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023099 {
23100 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023101 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023102 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 else
23104 EMSG(_(e_outofmem));
23105 }
23106 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023107 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108
23109 if (reanimate)
23110 {
23111 /* The pending return value could be overwritten by a ":return"
23112 * without argument in a finally clause; reset the default
23113 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023114 current_funccal->rettv->v_type = VAR_NUMBER;
23115 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 }
23117 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023118 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023119 }
23120 else
23121 {
23122 current_funccal->returned = TRUE;
23123
23124 /* If the return is carried out now, store the return value. For
23125 * a return immediately after reanimation, the value is already
23126 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023127 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023129 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023130 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023132 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023133 }
23134 }
23135
23136 return idx < 0;
23137}
23138
23139/*
23140 * Free the variable with a pending return value.
23141 */
23142 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023143discard_pending_return(rettv)
23144 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145{
Bram Moolenaar33570922005-01-25 22:26:29 +000023146 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023147}
23148
23149/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023150 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 * is an allocated string. Used by report_pending() for verbose messages.
23152 */
23153 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023154get_return_cmd(rettv)
23155 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023157 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023158 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023159 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023160
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023161 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023162 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023163 if (s == NULL)
23164 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023165
23166 STRCPY(IObuff, ":return ");
23167 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23168 if (STRLEN(s) + 8 >= IOSIZE)
23169 STRCPY(IObuff + IOSIZE - 4, "...");
23170 vim_free(tofree);
23171 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172}
23173
23174/*
23175 * Get next function line.
23176 * Called by do_cmdline() to get the next line.
23177 * Returns allocated string, or NULL for end of function.
23178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179 char_u *
23180get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023181 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023182 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023183 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023184{
Bram Moolenaar33570922005-01-25 22:26:29 +000023185 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023186 ufunc_T *fp = fcp->func;
23187 char_u *retval;
23188 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023189
23190 /* If breakpoints have been added/deleted need to check for it. */
23191 if (fcp->dbg_tick != debug_tick)
23192 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023193 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194 sourcing_lnum);
23195 fcp->dbg_tick = debug_tick;
23196 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023197#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023198 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023199 func_line_end(cookie);
23200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201
Bram Moolenaar05159a02005-02-26 23:04:13 +000023202 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023203 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23204 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205 retval = NULL;
23206 else
23207 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023208 /* Skip NULL lines (continuation lines). */
23209 while (fcp->linenr < gap->ga_len
23210 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23211 ++fcp->linenr;
23212 if (fcp->linenr >= gap->ga_len)
23213 retval = NULL;
23214 else
23215 {
23216 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23217 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023218#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023219 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023220 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023221#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023223 }
23224
23225 /* Did we encounter a breakpoint? */
23226 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23227 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023228 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023229 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023230 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231 sourcing_lnum);
23232 fcp->dbg_tick = debug_tick;
23233 }
23234
23235 return retval;
23236}
23237
Bram Moolenaar05159a02005-02-26 23:04:13 +000023238#if defined(FEAT_PROFILE) || defined(PROTO)
23239/*
23240 * Called when starting to read a function line.
23241 * "sourcing_lnum" must be correct!
23242 * When skipping lines it may not actually be executed, but we won't find out
23243 * until later and we need to store the time now.
23244 */
23245 void
23246func_line_start(cookie)
23247 void *cookie;
23248{
23249 funccall_T *fcp = (funccall_T *)cookie;
23250 ufunc_T *fp = fcp->func;
23251
23252 if (fp->uf_profiling && sourcing_lnum >= 1
23253 && sourcing_lnum <= fp->uf_lines.ga_len)
23254 {
23255 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023256 /* Skip continuation lines. */
23257 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23258 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023259 fp->uf_tml_execed = FALSE;
23260 profile_start(&fp->uf_tml_start);
23261 profile_zero(&fp->uf_tml_children);
23262 profile_get_wait(&fp->uf_tml_wait);
23263 }
23264}
23265
23266/*
23267 * Called when actually executing a function line.
23268 */
23269 void
23270func_line_exec(cookie)
23271 void *cookie;
23272{
23273 funccall_T *fcp = (funccall_T *)cookie;
23274 ufunc_T *fp = fcp->func;
23275
23276 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23277 fp->uf_tml_execed = TRUE;
23278}
23279
23280/*
23281 * Called when done with a function line.
23282 */
23283 void
23284func_line_end(cookie)
23285 void *cookie;
23286{
23287 funccall_T *fcp = (funccall_T *)cookie;
23288 ufunc_T *fp = fcp->func;
23289
23290 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23291 {
23292 if (fp->uf_tml_execed)
23293 {
23294 ++fp->uf_tml_count[fp->uf_tml_idx];
23295 profile_end(&fp->uf_tml_start);
23296 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023297 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023298 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23299 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023300 }
23301 fp->uf_tml_idx = -1;
23302 }
23303}
23304#endif
23305
Bram Moolenaar071d4272004-06-13 20:20:40 +000023306/*
23307 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023308 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 */
23310 int
23311func_has_ended(cookie)
23312 void *cookie;
23313{
Bram Moolenaar33570922005-01-25 22:26:29 +000023314 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315
23316 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23317 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023318 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 || fcp->returned);
23320}
23321
23322/*
23323 * return TRUE if cookie indicates a function which "abort"s on errors.
23324 */
23325 int
23326func_has_abort(cookie)
23327 void *cookie;
23328{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023329 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330}
23331
23332#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23333typedef enum
23334{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023335 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23336 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23337 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023338} var_flavour_T;
23339
23340static var_flavour_T var_flavour __ARGS((char_u *varname));
23341
23342 static var_flavour_T
23343var_flavour(varname)
23344 char_u *varname;
23345{
23346 char_u *p = varname;
23347
23348 if (ASCII_ISUPPER(*p))
23349 {
23350 while (*(++p))
23351 if (ASCII_ISLOWER(*p))
23352 return VAR_FLAVOUR_SESSION;
23353 return VAR_FLAVOUR_VIMINFO;
23354 }
23355 else
23356 return VAR_FLAVOUR_DEFAULT;
23357}
23358#endif
23359
23360#if defined(FEAT_VIMINFO) || defined(PROTO)
23361/*
23362 * Restore global vars that start with a capital from the viminfo file
23363 */
23364 int
23365read_viminfo_varlist(virp, writing)
23366 vir_T *virp;
23367 int writing;
23368{
23369 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023370 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023371 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023372
23373 if (!writing && (find_viminfo_parameter('!') != NULL))
23374 {
23375 tab = vim_strchr(virp->vir_line + 1, '\t');
23376 if (tab != NULL)
23377 {
23378 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023379 switch (*tab)
23380 {
23381 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023382#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023383 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023384#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023385 case 'D': type = VAR_DICT; break;
23386 case 'L': type = VAR_LIST; break;
23387 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023388
23389 tab = vim_strchr(tab, '\t');
23390 if (tab != NULL)
23391 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023392 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023393 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023394 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023396#ifdef FEAT_FLOAT
23397 else if (type == VAR_FLOAT)
23398 (void)string2float(tab + 1, &tv.vval.v_float);
23399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023400 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023401 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023402 if (type == VAR_DICT || type == VAR_LIST)
23403 {
23404 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23405
23406 if (etv == NULL)
23407 /* Failed to parse back the dict or list, use it as a
23408 * string. */
23409 tv.v_type = VAR_STRING;
23410 else
23411 {
23412 vim_free(tv.vval.v_string);
23413 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023414 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023415 }
23416 }
23417
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023418 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023419
23420 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023421 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023422 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23423 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424 }
23425 }
23426 }
23427
23428 return viminfo_readline(virp);
23429}
23430
23431/*
23432 * Write global vars that start with a capital to the viminfo file
23433 */
23434 void
23435write_viminfo_varlist(fp)
23436 FILE *fp;
23437{
Bram Moolenaar33570922005-01-25 22:26:29 +000023438 hashitem_T *hi;
23439 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023440 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023441 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023442 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023443 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023444 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023445
23446 if (find_viminfo_parameter('!') == NULL)
23447 return;
23448
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023449 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023450
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023451 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023452 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023453 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023454 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023455 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023456 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023457 this_var = HI2DI(hi);
23458 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023459 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023460 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023461 {
23462 case VAR_STRING: s = "STR"; break;
23463 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023464#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023465 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023466#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023467 case VAR_DICT: s = "DIC"; break;
23468 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023469 default: continue;
23470 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023471 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023472 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023473 if (p != NULL)
23474 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023475 vim_free(tofree);
23476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 }
23478 }
23479}
23480#endif
23481
23482#if defined(FEAT_SESSION) || defined(PROTO)
23483 int
23484store_session_globals(fd)
23485 FILE *fd;
23486{
Bram Moolenaar33570922005-01-25 22:26:29 +000023487 hashitem_T *hi;
23488 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023489 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490 char_u *p, *t;
23491
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023492 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023493 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023494 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023495 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023497 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023498 this_var = HI2DI(hi);
23499 if ((this_var->di_tv.v_type == VAR_NUMBER
23500 || this_var->di_tv.v_type == VAR_STRING)
23501 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023502 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023503 /* Escape special characters with a backslash. Turn a LF and
23504 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023505 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023506 (char_u *)"\\\"\n\r");
23507 if (p == NULL) /* out of memory */
23508 break;
23509 for (t = p; *t != NUL; ++t)
23510 if (*t == '\n')
23511 *t = 'n';
23512 else if (*t == '\r')
23513 *t = 'r';
23514 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023515 this_var->di_key,
23516 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23517 : ' ',
23518 p,
23519 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23520 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023521 || put_eol(fd) == FAIL)
23522 {
23523 vim_free(p);
23524 return FAIL;
23525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023526 vim_free(p);
23527 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023528#ifdef FEAT_FLOAT
23529 else if (this_var->di_tv.v_type == VAR_FLOAT
23530 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23531 {
23532 float_T f = this_var->di_tv.vval.v_float;
23533 int sign = ' ';
23534
23535 if (f < 0)
23536 {
23537 f = -f;
23538 sign = '-';
23539 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023540 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023541 this_var->di_key, sign, f) < 0)
23542 || put_eol(fd) == FAIL)
23543 return FAIL;
23544 }
23545#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 }
23547 }
23548 return OK;
23549}
23550#endif
23551
Bram Moolenaar661b1822005-07-28 22:36:45 +000023552/*
23553 * Display script name where an item was last set.
23554 * Should only be invoked when 'verbose' is non-zero.
23555 */
23556 void
23557last_set_msg(scriptID)
23558 scid_T scriptID;
23559{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023560 char_u *p;
23561
Bram Moolenaar661b1822005-07-28 22:36:45 +000023562 if (scriptID != 0)
23563 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023564 p = home_replace_save(NULL, get_scriptname(scriptID));
23565 if (p != NULL)
23566 {
23567 verbose_enter();
23568 MSG_PUTS(_("\n\tLast set from "));
23569 MSG_PUTS(p);
23570 vim_free(p);
23571 verbose_leave();
23572 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023573 }
23574}
23575
Bram Moolenaard812df62008-11-09 12:46:09 +000023576/*
23577 * List v:oldfiles in a nice way.
23578 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023579 void
23580ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023581 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023582{
23583 list_T *l = vimvars[VV_OLDFILES].vv_list;
23584 listitem_T *li;
23585 int nr = 0;
23586
23587 if (l == NULL)
23588 msg((char_u *)_("No old files"));
23589 else
23590 {
23591 msg_start();
23592 msg_scroll = TRUE;
23593 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23594 {
23595 msg_outnum((long)++nr);
23596 MSG_PUTS(": ");
23597 msg_outtrans(get_tv_string(&li->li_tv));
23598 msg_putchar('\n');
23599 out_flush(); /* output one line at a time */
23600 ui_breakcheck();
23601 }
23602 /* Assume "got_int" was set to truncate the listing. */
23603 got_int = FALSE;
23604
23605#ifdef FEAT_BROWSE_CMD
23606 if (cmdmod.browse)
23607 {
23608 quit_more = FALSE;
23609 nr = prompt_for_number(FALSE);
23610 msg_starthere();
23611 if (nr > 0)
23612 {
23613 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23614 (long)nr);
23615
23616 if (p != NULL)
23617 {
23618 p = expand_env_save(p);
23619 eap->arg = p;
23620 eap->cmdidx = CMD_edit;
23621 cmdmod.browse = FALSE;
23622 do_exedit(eap, NULL);
23623 vim_free(p);
23624 }
23625 }
23626 }
23627#endif
23628 }
23629}
23630
Bram Moolenaar071d4272004-06-13 20:20:40 +000023631#endif /* FEAT_EVAL */
23632
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023634#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023635
23636#ifdef WIN3264
23637/*
23638 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23639 */
23640static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23641static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23642static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23643
23644/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023645 * Get the short path (8.3) for the filename in "fnamep".
23646 * Only works for a valid file name.
23647 * When the path gets longer "fnamep" is changed and the allocated buffer
23648 * is put in "bufp".
23649 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23650 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 */
23652 static int
23653get_short_pathname(fnamep, bufp, fnamelen)
23654 char_u **fnamep;
23655 char_u **bufp;
23656 int *fnamelen;
23657{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023658 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 char_u *newbuf;
23660
23661 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023662 l = GetShortPathName(*fnamep, *fnamep, len);
23663 if (l > len - 1)
23664 {
23665 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023666 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 newbuf = vim_strnsave(*fnamep, l);
23668 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023669 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670
23671 vim_free(*bufp);
23672 *fnamep = *bufp = newbuf;
23673
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023674 /* Really should always succeed, as the buffer is big enough. */
23675 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676 }
23677
23678 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023679 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680}
23681
23682/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023683 * Get the short path (8.3) for the filename in "fname". The converted
23684 * path is returned in "bufp".
23685 *
23686 * Some of the directories specified in "fname" may not exist. This function
23687 * will shorten the existing directories at the beginning of the path and then
23688 * append the remaining non-existing path.
23689 *
23690 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023691 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023692 * bufp - Pointer to an allocated buffer for the filename.
23693 * fnamelen - Length of the filename pointed to by fname
23694 *
23695 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023696 */
23697 static int
23698shortpath_for_invalid_fname(fname, bufp, fnamelen)
23699 char_u **fname;
23700 char_u **bufp;
23701 int *fnamelen;
23702{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023703 char_u *short_fname, *save_fname, *pbuf_unused;
23704 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023706 int old_len, len;
23707 int new_len, sfx_len;
23708 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023709
23710 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023711 old_len = *fnamelen;
23712 save_fname = vim_strnsave(*fname, old_len);
23713 pbuf_unused = NULL;
23714 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023715
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023716 endp = save_fname + old_len - 1; /* Find the end of the copy */
23717 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023719 /*
23720 * Try shortening the supplied path till it succeeds by removing one
23721 * directory at a time from the tail of the path.
23722 */
23723 len = 0;
23724 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023725 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023726 /* go back one path-separator */
23727 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23728 --endp;
23729 if (endp <= save_fname)
23730 break; /* processed the complete path */
23731
23732 /*
23733 * Replace the path separator with a NUL and try to shorten the
23734 * resulting path.
23735 */
23736 ch = *endp;
23737 *endp = 0;
23738 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023739 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023740 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23741 {
23742 retval = FAIL;
23743 goto theend;
23744 }
23745 *endp = ch; /* preserve the string */
23746
23747 if (len > 0)
23748 break; /* successfully shortened the path */
23749
23750 /* failed to shorten the path. Skip the path separator */
23751 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023752 }
23753
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023754 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023756 /*
23757 * Succeeded in shortening the path. Now concatenate the shortened
23758 * path with the remaining path at the tail.
23759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023760
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023761 /* Compute the length of the new path. */
23762 sfx_len = (int)(save_endp - endp) + 1;
23763 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023764
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023765 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023766 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023767 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023768 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023769 /* There is not enough space in the currently allocated string,
23770 * copy it to a buffer big enough. */
23771 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023772 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023773 {
23774 retval = FAIL;
23775 goto theend;
23776 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023777 }
23778 else
23779 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023780 /* Transfer short_fname to the main buffer (it's big enough),
23781 * unless get_short_pathname() did its work in-place. */
23782 *fname = *bufp = save_fname;
23783 if (short_fname != save_fname)
23784 vim_strncpy(save_fname, short_fname, len);
23785 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023786 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023787
23788 /* concat the not-shortened part of the path */
23789 vim_strncpy(*fname + len, endp, sfx_len);
23790 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023791 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023792
23793theend:
23794 vim_free(pbuf_unused);
23795 vim_free(save_fname);
23796
23797 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023798}
23799
23800/*
23801 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023802 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023803 */
23804 static int
23805shortpath_for_partial(fnamep, bufp, fnamelen)
23806 char_u **fnamep;
23807 char_u **bufp;
23808 int *fnamelen;
23809{
23810 int sepcount, len, tflen;
23811 char_u *p;
23812 char_u *pbuf, *tfname;
23813 int hasTilde;
23814
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023815 /* Count up the path separators from the RHS.. so we know which part
23816 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023818 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023819 if (vim_ispathsep(*p))
23820 ++sepcount;
23821
23822 /* Need full path first (use expand_env() to remove a "~/") */
23823 hasTilde = (**fnamep == '~');
23824 if (hasTilde)
23825 pbuf = tfname = expand_env_save(*fnamep);
23826 else
23827 pbuf = tfname = FullName_save(*fnamep, FALSE);
23828
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023829 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023831 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23832 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023833
23834 if (len == 0)
23835 {
23836 /* Don't have a valid filename, so shorten the rest of the
23837 * path if we can. This CAN give us invalid 8.3 filenames, but
23838 * there's not a lot of point in guessing what it might be.
23839 */
23840 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023841 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23842 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023843 }
23844
23845 /* Count the paths backward to find the beginning of the desired string. */
23846 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023847 {
23848#ifdef FEAT_MBYTE
23849 if (has_mbyte)
23850 p -= mb_head_off(tfname, p);
23851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852 if (vim_ispathsep(*p))
23853 {
23854 if (sepcount == 0 || (hasTilde && sepcount == 1))
23855 break;
23856 else
23857 sepcount --;
23858 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023860 if (hasTilde)
23861 {
23862 --p;
23863 if (p >= tfname)
23864 *p = '~';
23865 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023866 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023867 }
23868 else
23869 ++p;
23870
23871 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23872 vim_free(*bufp);
23873 *fnamelen = (int)STRLEN(p);
23874 *bufp = pbuf;
23875 *fnamep = p;
23876
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023877 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023878}
23879#endif /* WIN3264 */
23880
23881/*
23882 * Adjust a filename, according to a string of modifiers.
23883 * *fnamep must be NUL terminated when called. When returning, the length is
23884 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023885 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 * When there is an error, *fnamep is set to NULL.
23887 */
23888 int
23889modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23890 char_u *src; /* string with modifiers */
23891 int *usedlen; /* characters after src that are used */
23892 char_u **fnamep; /* file name so far */
23893 char_u **bufp; /* buffer for allocated file name or NULL */
23894 int *fnamelen; /* length of fnamep */
23895{
23896 int valid = 0;
23897 char_u *tail;
23898 char_u *s, *p, *pbuf;
23899 char_u dirname[MAXPATHL];
23900 int c;
23901 int has_fullname = 0;
23902#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023903 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904 int has_shortname = 0;
23905#endif
23906
23907repeat:
23908 /* ":p" - full path/file_name */
23909 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23910 {
23911 has_fullname = 1;
23912
23913 valid |= VALID_PATH;
23914 *usedlen += 2;
23915
23916 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23917 if ((*fnamep)[0] == '~'
23918#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23919 && ((*fnamep)[1] == '/'
23920# ifdef BACKSLASH_IN_FILENAME
23921 || (*fnamep)[1] == '\\'
23922# endif
23923 || (*fnamep)[1] == NUL)
23924
23925#endif
23926 )
23927 {
23928 *fnamep = expand_env_save(*fnamep);
23929 vim_free(*bufp); /* free any allocated file name */
23930 *bufp = *fnamep;
23931 if (*fnamep == NULL)
23932 return -1;
23933 }
23934
23935 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023936 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 {
23938 if (vim_ispathsep(*p)
23939 && p[1] == '.'
23940 && (p[2] == NUL
23941 || vim_ispathsep(p[2])
23942 || (p[2] == '.'
23943 && (p[3] == NUL || vim_ispathsep(p[3])))))
23944 break;
23945 }
23946
23947 /* FullName_save() is slow, don't use it when not needed. */
23948 if (*p != NUL || !vim_isAbsName(*fnamep))
23949 {
23950 *fnamep = FullName_save(*fnamep, *p != NUL);
23951 vim_free(*bufp); /* free any allocated file name */
23952 *bufp = *fnamep;
23953 if (*fnamep == NULL)
23954 return -1;
23955 }
23956
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023957#ifdef WIN3264
23958# if _WIN32_WINNT >= 0x0500
23959 if (vim_strchr(*fnamep, '~') != NULL)
23960 {
23961 /* Expand 8.3 filename to full path. Needed to make sure the same
23962 * file does not have two different names.
23963 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23964 p = alloc(_MAX_PATH + 1);
23965 if (p != NULL)
23966 {
23967 if (GetLongPathName(*fnamep, p, MAXPATHL))
23968 {
23969 vim_free(*bufp);
23970 *bufp = *fnamep = p;
23971 }
23972 else
23973 vim_free(p);
23974 }
23975 }
23976# endif
23977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023978 /* Append a path separator to a directory. */
23979 if (mch_isdir(*fnamep))
23980 {
23981 /* Make room for one or two extra characters. */
23982 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
23983 vim_free(*bufp); /* free any allocated file name */
23984 *bufp = *fnamep;
23985 if (*fnamep == NULL)
23986 return -1;
23987 add_pathsep(*fnamep);
23988 }
23989 }
23990
23991 /* ":." - path relative to the current directory */
23992 /* ":~" - path relative to the home directory */
23993 /* ":8" - shortname path - postponed till after */
23994 while (src[*usedlen] == ':'
23995 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
23996 {
23997 *usedlen += 2;
23998 if (c == '8')
23999 {
24000#ifdef WIN3264
24001 has_shortname = 1; /* Postpone this. */
24002#endif
24003 continue;
24004 }
24005 pbuf = NULL;
24006 /* Need full path first (use expand_env() to remove a "~/") */
24007 if (!has_fullname)
24008 {
24009 if (c == '.' && **fnamep == '~')
24010 p = pbuf = expand_env_save(*fnamep);
24011 else
24012 p = pbuf = FullName_save(*fnamep, FALSE);
24013 }
24014 else
24015 p = *fnamep;
24016
24017 has_fullname = 0;
24018
24019 if (p != NULL)
24020 {
24021 if (c == '.')
24022 {
24023 mch_dirname(dirname, MAXPATHL);
24024 s = shorten_fname(p, dirname);
24025 if (s != NULL)
24026 {
24027 *fnamep = s;
24028 if (pbuf != NULL)
24029 {
24030 vim_free(*bufp); /* free any allocated file name */
24031 *bufp = pbuf;
24032 pbuf = NULL;
24033 }
24034 }
24035 }
24036 else
24037 {
24038 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24039 /* Only replace it when it starts with '~' */
24040 if (*dirname == '~')
24041 {
24042 s = vim_strsave(dirname);
24043 if (s != NULL)
24044 {
24045 *fnamep = s;
24046 vim_free(*bufp);
24047 *bufp = s;
24048 }
24049 }
24050 }
24051 vim_free(pbuf);
24052 }
24053 }
24054
24055 tail = gettail(*fnamep);
24056 *fnamelen = (int)STRLEN(*fnamep);
24057
24058 /* ":h" - head, remove "/file_name", can be repeated */
24059 /* Don't remove the first "/" or "c:\" */
24060 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24061 {
24062 valid |= VALID_HEAD;
24063 *usedlen += 2;
24064 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024065 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024066 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024067 *fnamelen = (int)(tail - *fnamep);
24068#ifdef VMS
24069 if (*fnamelen > 0)
24070 *fnamelen += 1; /* the path separator is part of the path */
24071#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024072 if (*fnamelen == 0)
24073 {
24074 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24075 p = vim_strsave((char_u *)".");
24076 if (p == NULL)
24077 return -1;
24078 vim_free(*bufp);
24079 *bufp = *fnamep = tail = p;
24080 *fnamelen = 1;
24081 }
24082 else
24083 {
24084 while (tail > s && !after_pathsep(s, tail))
24085 mb_ptr_back(*fnamep, tail);
24086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 }
24088
24089 /* ":8" - shortname */
24090 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24091 {
24092 *usedlen += 2;
24093#ifdef WIN3264
24094 has_shortname = 1;
24095#endif
24096 }
24097
24098#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024099 /*
24100 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024101 */
24102 if (has_shortname)
24103 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024104 /* Copy the string if it is shortened by :h and when it wasn't copied
24105 * yet, because we are going to change it in place. Avoids changing
24106 * the buffer name for "%:8". */
24107 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024108 {
24109 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024110 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024111 return -1;
24112 vim_free(*bufp);
24113 *bufp = *fnamep = p;
24114 }
24115
24116 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024117 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024118 if (!has_fullname && !vim_isAbsName(*fnamep))
24119 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024120 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121 return -1;
24122 }
24123 else
24124 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024125 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024126
Bram Moolenaardc935552011-08-17 15:23:23 +020024127 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024128 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024129 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130 return -1;
24131
24132 if (l == 0)
24133 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024134 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024135 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024136 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024137 return -1;
24138 }
24139 *fnamelen = l;
24140 }
24141 }
24142#endif /* WIN3264 */
24143
24144 /* ":t" - tail, just the basename */
24145 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24146 {
24147 *usedlen += 2;
24148 *fnamelen -= (int)(tail - *fnamep);
24149 *fnamep = tail;
24150 }
24151
24152 /* ":e" - extension, can be repeated */
24153 /* ":r" - root, without extension, can be repeated */
24154 while (src[*usedlen] == ':'
24155 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24156 {
24157 /* find a '.' in the tail:
24158 * - for second :e: before the current fname
24159 * - otherwise: The last '.'
24160 */
24161 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24162 s = *fnamep - 2;
24163 else
24164 s = *fnamep + *fnamelen - 1;
24165 for ( ; s > tail; --s)
24166 if (s[0] == '.')
24167 break;
24168 if (src[*usedlen + 1] == 'e') /* :e */
24169 {
24170 if (s > tail)
24171 {
24172 *fnamelen += (int)(*fnamep - (s + 1));
24173 *fnamep = s + 1;
24174#ifdef VMS
24175 /* cut version from the extension */
24176 s = *fnamep + *fnamelen - 1;
24177 for ( ; s > *fnamep; --s)
24178 if (s[0] == ';')
24179 break;
24180 if (s > *fnamep)
24181 *fnamelen = s - *fnamep;
24182#endif
24183 }
24184 else if (*fnamep <= tail)
24185 *fnamelen = 0;
24186 }
24187 else /* :r */
24188 {
24189 if (s > tail) /* remove one extension */
24190 *fnamelen = (int)(s - *fnamep);
24191 }
24192 *usedlen += 2;
24193 }
24194
24195 /* ":s?pat?foo?" - substitute */
24196 /* ":gs?pat?foo?" - global substitute */
24197 if (src[*usedlen] == ':'
24198 && (src[*usedlen + 1] == 's'
24199 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24200 {
24201 char_u *str;
24202 char_u *pat;
24203 char_u *sub;
24204 int sep;
24205 char_u *flags;
24206 int didit = FALSE;
24207
24208 flags = (char_u *)"";
24209 s = src + *usedlen + 2;
24210 if (src[*usedlen + 1] == 'g')
24211 {
24212 flags = (char_u *)"g";
24213 ++s;
24214 }
24215
24216 sep = *s++;
24217 if (sep)
24218 {
24219 /* find end of pattern */
24220 p = vim_strchr(s, sep);
24221 if (p != NULL)
24222 {
24223 pat = vim_strnsave(s, (int)(p - s));
24224 if (pat != NULL)
24225 {
24226 s = p + 1;
24227 /* find end of substitution */
24228 p = vim_strchr(s, sep);
24229 if (p != NULL)
24230 {
24231 sub = vim_strnsave(s, (int)(p - s));
24232 str = vim_strnsave(*fnamep, *fnamelen);
24233 if (sub != NULL && str != NULL)
24234 {
24235 *usedlen = (int)(p + 1 - src);
24236 s = do_string_sub(str, pat, sub, flags);
24237 if (s != NULL)
24238 {
24239 *fnamep = s;
24240 *fnamelen = (int)STRLEN(s);
24241 vim_free(*bufp);
24242 *bufp = s;
24243 didit = TRUE;
24244 }
24245 }
24246 vim_free(sub);
24247 vim_free(str);
24248 }
24249 vim_free(pat);
24250 }
24251 }
24252 /* after using ":s", repeat all the modifiers */
24253 if (didit)
24254 goto repeat;
24255 }
24256 }
24257
24258 return valid;
24259}
24260
24261/*
24262 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24263 * "flags" can be "g" to do a global substitute.
24264 * Returns an allocated string, NULL for error.
24265 */
24266 char_u *
24267do_string_sub(str, pat, sub, flags)
24268 char_u *str;
24269 char_u *pat;
24270 char_u *sub;
24271 char_u *flags;
24272{
24273 int sublen;
24274 regmatch_T regmatch;
24275 int i;
24276 int do_all;
24277 char_u *tail;
24278 garray_T ga;
24279 char_u *ret;
24280 char_u *save_cpo;
24281
24282 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24283 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024284 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024285
24286 ga_init2(&ga, 1, 200);
24287
24288 do_all = (flags[0] == 'g');
24289
24290 regmatch.rm_ic = p_ic;
24291 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24292 if (regmatch.regprog != NULL)
24293 {
24294 tail = str;
24295 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24296 {
24297 /*
24298 * Get some space for a temporary buffer to do the substitution
24299 * into. It will contain:
24300 * - The text up to where the match is.
24301 * - The substituted text.
24302 * - The text after the match.
24303 */
24304 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24305 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24306 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24307 {
24308 ga_clear(&ga);
24309 break;
24310 }
24311
24312 /* copy the text up to where the match is */
24313 i = (int)(regmatch.startp[0] - tail);
24314 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24315 /* add the substituted text */
24316 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24317 + ga.ga_len + i, TRUE, TRUE, FALSE);
24318 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024319 /* avoid getting stuck on a match with an empty string */
24320 if (tail == regmatch.endp[0])
24321 {
24322 if (*tail == NUL)
24323 break;
24324 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24325 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024326 }
24327 else
24328 {
24329 tail = regmatch.endp[0];
24330 if (*tail == NUL)
24331 break;
24332 }
24333 if (!do_all)
24334 break;
24335 }
24336
24337 if (ga.ga_data != NULL)
24338 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24339
Bram Moolenaar473de612013-06-08 18:19:48 +020024340 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024341 }
24342
24343 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24344 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024345 if (p_cpo == empty_option)
24346 p_cpo = save_cpo;
24347 else
24348 /* Darn, evaluating {sub} expression changed the value. */
24349 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024350
24351 return ret;
24352}
24353
24354#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */