blob: 68f80fc7dbac92fdfc3c12cb76bd1ed35d65f5a1 [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 Moolenaarcca74132013-09-25 21:00:28 +0200918 /* Script-local variables. First clear all the variables and in a second
919 * loop free the scriptvar_T, because a variable in one script might hold
920 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200921 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200922 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +0200923 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200924 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200925 ga_clear(&ga_scripts);
926
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000927 /* unreferenced lists and dicts */
928 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000929
930 /* functions */
931 free_all_functions();
932 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000933}
934#endif
935
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000936/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 * Return the name of the executed function.
938 */
939 char_u *
940func_name(cookie)
941 void *cookie;
942{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000943 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944}
945
946/*
947 * Return the address holding the next breakpoint line for a funccall cookie.
948 */
949 linenr_T *
950func_breakpoint(cookie)
951 void *cookie;
952{
Bram Moolenaar33570922005-01-25 22:26:29 +0000953 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954}
955
956/*
957 * Return the address holding the debug tick for a funccall cookie.
958 */
959 int *
960func_dbg_tick(cookie)
961 void *cookie;
962{
Bram Moolenaar33570922005-01-25 22:26:29 +0000963 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964}
965
966/*
967 * Return the nesting level for a funccall cookie.
968 */
969 int
970func_level(cookie)
971 void *cookie;
972{
Bram Moolenaar33570922005-01-25 22:26:29 +0000973 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974}
975
976/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000977funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000979/* pointer to list of previously used funccal, still around because some
980 * item in it is still being used. */
981funccall_T *previous_funccal = NULL;
982
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983/*
984 * Return TRUE when a function was ended by a ":return" command.
985 */
986 int
987current_func_returned()
988{
989 return current_funccal->returned;
990}
991
992
993/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 * Set an internal variable to a string value. Creates the variable if it does
995 * not already exist.
996 */
997 void
998set_internal_string_var(name, value)
999 char_u *name;
1000 char_u *value;
1001{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001002 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001003 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004
1005 val = vim_strsave(value);
1006 if (val != NULL)
1007 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001008 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001009 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001011 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001012 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 }
1014 }
1015}
1016
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001017static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001018static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001019static char_u *redir_endp = NULL;
1020static char_u *redir_varname = NULL;
1021
1022/*
1023 * Start recording command output to a variable
1024 * Returns OK if successfully completed the setup. FAIL otherwise.
1025 */
1026 int
1027var_redir_start(name, append)
1028 char_u *name;
1029 int append; /* append to an existing variable */
1030{
1031 int save_emsg;
1032 int err;
1033 typval_T tv;
1034
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001035 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001036 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001037 {
1038 EMSG(_(e_invarg));
1039 return FAIL;
1040 }
1041
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001042 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001043 redir_varname = vim_strsave(name);
1044 if (redir_varname == NULL)
1045 return FAIL;
1046
1047 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1048 if (redir_lval == NULL)
1049 {
1050 var_redir_stop();
1051 return FAIL;
1052 }
1053
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001054 /* The output is stored in growarray "redir_ga" until redirection ends. */
1055 ga_init2(&redir_ga, (int)sizeof(char), 500);
1056
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001057 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001058 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1059 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001060 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1061 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001062 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001063 if (redir_endp != NULL && *redir_endp != NUL)
1064 /* Trailing characters are present after the variable name */
1065 EMSG(_(e_trailing));
1066 else
1067 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001068 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001069 var_redir_stop();
1070 return FAIL;
1071 }
1072
1073 /* check if we can write to the variable: set it to or append an empty
1074 * string */
1075 save_emsg = did_emsg;
1076 did_emsg = FALSE;
1077 tv.v_type = VAR_STRING;
1078 tv.vval.v_string = (char_u *)"";
1079 if (append)
1080 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1081 else
1082 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001083 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001084 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001085 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001086 if (err)
1087 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001088 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001089 var_redir_stop();
1090 return FAIL;
1091 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001092
1093 return OK;
1094}
1095
1096/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001097 * Append "value[value_len]" to the variable set by var_redir_start().
1098 * The actual appending is postponed until redirection ends, because the value
1099 * appended may in fact be the string we write to, changing it may cause freed
1100 * memory to be used:
1101 * :redir => foo
1102 * :let foo
1103 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104 */
1105 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001106var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001108 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001110 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111
1112 if (redir_lval == NULL)
1113 return;
1114
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001115 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001116 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001118 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001119
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001120 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001121 {
1122 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001123 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001124 }
1125 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127}
1128
1129/*
1130 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001131 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001132 */
1133 void
1134var_redir_stop()
1135{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001136 typval_T tv;
1137
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001138 if (redir_lval != NULL)
1139 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001140 /* If there was no error: assign the text to the variable. */
1141 if (redir_endp != NULL)
1142 {
1143 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1144 tv.v_type = VAR_STRING;
1145 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001146 /* Call get_lval() again, if it's inside a Dict or List it may
1147 * have changed. */
1148 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1149 FALSE, FALSE, FALSE, FNE_CHECK_START);
1150 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1151 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1152 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001153 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001154
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001155 /* free the collected output */
1156 vim_free(redir_ga.ga_data);
1157 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001158
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 vim_free(redir_lval);
1160 redir_lval = NULL;
1161 }
1162 vim_free(redir_varname);
1163 redir_varname = NULL;
1164}
1165
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166# if defined(FEAT_MBYTE) || defined(PROTO)
1167 int
1168eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1169 char_u *enc_from;
1170 char_u *enc_to;
1171 char_u *fname_from;
1172 char_u *fname_to;
1173{
1174 int err = FALSE;
1175
1176 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1177 set_vim_var_string(VV_CC_TO, enc_to, -1);
1178 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1179 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1180 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1181 err = TRUE;
1182 set_vim_var_string(VV_CC_FROM, NULL, -1);
1183 set_vim_var_string(VV_CC_TO, NULL, -1);
1184 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1185 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1186
1187 if (err)
1188 return FAIL;
1189 return OK;
1190}
1191# endif
1192
1193# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1194 int
1195eval_printexpr(fname, args)
1196 char_u *fname;
1197 char_u *args;
1198{
1199 int err = FALSE;
1200
1201 set_vim_var_string(VV_FNAME_IN, fname, -1);
1202 set_vim_var_string(VV_CMDARG, args, -1);
1203 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1204 err = TRUE;
1205 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1206 set_vim_var_string(VV_CMDARG, NULL, -1);
1207
1208 if (err)
1209 {
1210 mch_remove(fname);
1211 return FAIL;
1212 }
1213 return OK;
1214}
1215# endif
1216
1217# if defined(FEAT_DIFF) || defined(PROTO)
1218 void
1219eval_diff(origfile, newfile, outfile)
1220 char_u *origfile;
1221 char_u *newfile;
1222 char_u *outfile;
1223{
1224 int err = FALSE;
1225
1226 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1227 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1228 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1229 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1230 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1231 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1232 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1233}
1234
1235 void
1236eval_patch(origfile, difffile, outfile)
1237 char_u *origfile;
1238 char_u *difffile;
1239 char_u *outfile;
1240{
1241 int err;
1242
1243 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1244 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1245 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1246 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1247 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1248 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1249 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1250}
1251# endif
1252
1253/*
1254 * Top level evaluation function, returning a boolean.
1255 * Sets "error" to TRUE if there was an error.
1256 * Return TRUE or FALSE.
1257 */
1258 int
1259eval_to_bool(arg, error, nextcmd, skip)
1260 char_u *arg;
1261 int *error;
1262 char_u **nextcmd;
1263 int skip; /* only parse, don't execute */
1264{
Bram Moolenaar33570922005-01-25 22:26:29 +00001265 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 int retval = FALSE;
1267
1268 if (skip)
1269 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001270 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 else
1273 {
1274 *error = FALSE;
1275 if (!skip)
1276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001277 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001278 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 }
1280 }
1281 if (skip)
1282 --emsg_skip;
1283
1284 return retval;
1285}
1286
1287/*
1288 * Top level evaluation function, returning a string. If "skip" is TRUE,
1289 * only parsing to "nextcmd" is done, without reporting errors. Return
1290 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1291 */
1292 char_u *
1293eval_to_string_skip(arg, nextcmd, skip)
1294 char_u *arg;
1295 char_u **nextcmd;
1296 int skip; /* only parse, don't execute */
1297{
Bram Moolenaar33570922005-01-25 22:26:29 +00001298 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 char_u *retval;
1300
1301 if (skip)
1302 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001303 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 retval = NULL;
1305 else
1306 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001307 retval = vim_strsave(get_tv_string(&tv));
1308 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 }
1310 if (skip)
1311 --emsg_skip;
1312
1313 return retval;
1314}
1315
1316/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001317 * Skip over an expression at "*pp".
1318 * Return FAIL for an error, OK otherwise.
1319 */
1320 int
1321skip_expr(pp)
1322 char_u **pp;
1323{
Bram Moolenaar33570922005-01-25 22:26:29 +00001324 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001325
1326 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001327 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001328}
1329
1330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001332 * When "convert" is TRUE convert a List into a sequence of lines and convert
1333 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 * Return pointer to allocated memory, or NULL for failure.
1335 */
1336 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001337eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 char_u *arg;
1339 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001340 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341{
Bram Moolenaar33570922005-01-25 22:26:29 +00001342 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001344 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001345#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001346 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001349 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 retval = NULL;
1351 else
1352 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001353 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001354 {
1355 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001356 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001357 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001358 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001359 if (tv.vval.v_list->lv_len > 0)
1360 ga_append(&ga, NL);
1361 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001362 ga_append(&ga, NUL);
1363 retval = (char_u *)ga.ga_data;
1364 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001365#ifdef FEAT_FLOAT
1366 else if (convert && tv.v_type == VAR_FLOAT)
1367 {
1368 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1369 retval = vim_strsave(numbuf);
1370 }
1371#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001372 else
1373 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001374 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 }
1376
1377 return retval;
1378}
1379
1380/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001381 * Call eval_to_string() without using current local variables and using
1382 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 */
1384 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001385eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386 char_u *arg;
1387 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001388 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389{
1390 char_u *retval;
1391 void *save_funccalp;
1392
1393 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001394 if (use_sandbox)
1395 ++sandbox;
1396 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001397 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001398 if (use_sandbox)
1399 --sandbox;
1400 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 restore_funccal(save_funccalp);
1402 return retval;
1403}
1404
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405/*
1406 * Top level evaluation function, returning a number.
1407 * Evaluates "expr" silently.
1408 * Returns -1 for an error.
1409 */
1410 int
1411eval_to_number(expr)
1412 char_u *expr;
1413{
Bram Moolenaar33570922005-01-25 22:26:29 +00001414 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001416 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417
1418 ++emsg_off;
1419
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001420 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 retval = -1;
1422 else
1423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001424 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001425 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 }
1427 --emsg_off;
1428
1429 return retval;
1430}
1431
Bram Moolenaara40058a2005-07-11 22:42:07 +00001432/*
1433 * Prepare v: variable "idx" to be used.
1434 * Save the current typeval in "save_tv".
1435 * When not used yet add the variable to the v: hashtable.
1436 */
1437 static void
1438prepare_vimvar(idx, save_tv)
1439 int idx;
1440 typval_T *save_tv;
1441{
1442 *save_tv = vimvars[idx].vv_tv;
1443 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1444 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1445}
1446
1447/*
1448 * Restore v: variable "idx" to typeval "save_tv".
1449 * When no longer defined, remove the variable from the v: hashtable.
1450 */
1451 static void
1452restore_vimvar(idx, save_tv)
1453 int idx;
1454 typval_T *save_tv;
1455{
1456 hashitem_T *hi;
1457
Bram Moolenaara40058a2005-07-11 22:42:07 +00001458 vimvars[idx].vv_tv = *save_tv;
1459 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1460 {
1461 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1462 if (HASHITEM_EMPTY(hi))
1463 EMSG2(_(e_intern2), "restore_vimvar()");
1464 else
1465 hash_remove(&vimvarht, hi);
1466 }
1467}
1468
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001469#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001470/*
1471 * Evaluate an expression to a list with suggestions.
1472 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001473 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001474 */
1475 list_T *
1476eval_spell_expr(badword, expr)
1477 char_u *badword;
1478 char_u *expr;
1479{
1480 typval_T save_val;
1481 typval_T rettv;
1482 list_T *list = NULL;
1483 char_u *p = skipwhite(expr);
1484
1485 /* Set "v:val" to the bad word. */
1486 prepare_vimvar(VV_VAL, &save_val);
1487 vimvars[VV_VAL].vv_type = VAR_STRING;
1488 vimvars[VV_VAL].vv_str = badword;
1489 if (p_verbose == 0)
1490 ++emsg_off;
1491
1492 if (eval1(&p, &rettv, TRUE) == OK)
1493 {
1494 if (rettv.v_type != VAR_LIST)
1495 clear_tv(&rettv);
1496 else
1497 list = rettv.vval.v_list;
1498 }
1499
1500 if (p_verbose == 0)
1501 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001502 restore_vimvar(VV_VAL, &save_val);
1503
1504 return list;
1505}
1506
1507/*
1508 * "list" is supposed to contain two items: a word and a number. Return the
1509 * word in "pp" and the number as the return value.
1510 * Return -1 if anything isn't right.
1511 * Used to get the good word and score from the eval_spell_expr() result.
1512 */
1513 int
1514get_spellword(list, pp)
1515 list_T *list;
1516 char_u **pp;
1517{
1518 listitem_T *li;
1519
1520 li = list->lv_first;
1521 if (li == NULL)
1522 return -1;
1523 *pp = get_tv_string(&li->li_tv);
1524
1525 li = li->li_next;
1526 if (li == NULL)
1527 return -1;
1528 return get_tv_number(&li->li_tv);
1529}
1530#endif
1531
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001532/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001533 * Top level evaluation function.
1534 * Returns an allocated typval_T with the result.
1535 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001536 */
1537 typval_T *
1538eval_expr(arg, nextcmd)
1539 char_u *arg;
1540 char_u **nextcmd;
1541{
1542 typval_T *tv;
1543
1544 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001545 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001546 {
1547 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001548 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001549 }
1550
1551 return tv;
1552}
1553
1554
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001556 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001557 * Uses argv[argc] for the function arguments. Only Number and String
1558 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001559 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001560 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001561 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001562call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 char_u *func;
1564 int argc;
1565 char_u **argv;
1566 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001567 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001568 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569{
Bram Moolenaar33570922005-01-25 22:26:29 +00001570 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 long n;
1572 int len;
1573 int i;
1574 int doesrange;
1575 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001576 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001578 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001580 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581
1582 for (i = 0; i < argc; i++)
1583 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001584 /* Pass a NULL or empty argument as an empty string */
1585 if (argv[i] == NULL || *argv[i] == NUL)
1586 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001587 argvars[i].v_type = VAR_STRING;
1588 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001589 continue;
1590 }
1591
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001592 if (str_arg_only)
1593 len = 0;
1594 else
1595 /* Recognize a number argument, the others must be strings. */
1596 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001597 if (len != 0 && len == (int)STRLEN(argv[i]))
1598 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001599 argvars[i].v_type = VAR_NUMBER;
1600 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602 else
1603 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001604 argvars[i].v_type = VAR_STRING;
1605 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 }
1608
1609 if (safe)
1610 {
1611 save_funccalp = save_funccal();
1612 ++sandbox;
1613 }
1614
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001615 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1616 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001618 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (safe)
1620 {
1621 --sandbox;
1622 restore_funccal(save_funccalp);
1623 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 vim_free(argvars);
1625
1626 if (ret == FAIL)
1627 clear_tv(rettv);
1628
1629 return ret;
1630}
1631
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001632/*
1633 * Call vimL function "func" and return the result as a number.
1634 * Returns -1 when calling the function fails.
1635 * Uses argv[argc] for the function arguments.
1636 */
1637 long
1638call_func_retnr(func, argc, argv, safe)
1639 char_u *func;
1640 int argc;
1641 char_u **argv;
1642 int safe; /* use the sandbox */
1643{
1644 typval_T rettv;
1645 long retval;
1646
1647 /* All arguments are passed as strings, no conversion to number. */
1648 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1649 return -1;
1650
1651 retval = get_tv_number_chk(&rettv, NULL);
1652 clear_tv(&rettv);
1653 return retval;
1654}
1655
1656#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1657 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1658
Bram Moolenaar4f688582007-07-24 12:34:30 +00001659# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001660/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001661 * Call vimL function "func" and return the result as a string.
1662 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001663 * Uses argv[argc] for the function arguments.
1664 */
1665 void *
1666call_func_retstr(func, argc, argv, safe)
1667 char_u *func;
1668 int argc;
1669 char_u **argv;
1670 int safe; /* use the sandbox */
1671{
1672 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001673 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001674
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001675 /* All arguments are passed as strings, no conversion to number. */
1676 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001677 return NULL;
1678
1679 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001680 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 return retval;
1682}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001683# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001684
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001685/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001686 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001688 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 */
1690 void *
1691call_func_retlist(func, argc, argv, safe)
1692 char_u *func;
1693 int argc;
1694 char_u **argv;
1695 int safe; /* use the sandbox */
1696{
1697 typval_T rettv;
1698
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001699 /* All arguments are passed as strings, no conversion to number. */
1700 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001701 return NULL;
1702
1703 if (rettv.v_type != VAR_LIST)
1704 {
1705 clear_tv(&rettv);
1706 return NULL;
1707 }
1708
1709 return rettv.vval.v_list;
1710}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711#endif
1712
1713/*
1714 * Save the current function call pointer, and set it to NULL.
1715 * Used when executing autocommands and for ":source".
1716 */
1717 void *
1718save_funccal()
1719{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001720 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 current_funccal = NULL;
1723 return (void *)fc;
1724}
1725
1726 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001727restore_funccal(vfc)
1728 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001730 funccall_T *fc = (funccall_T *)vfc;
1731
1732 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733}
1734
Bram Moolenaar05159a02005-02-26 23:04:13 +00001735#if defined(FEAT_PROFILE) || defined(PROTO)
1736/*
1737 * Prepare profiling for entering a child or something else that is not
1738 * counted for the script/function itself.
1739 * Should always be called in pair with prof_child_exit().
1740 */
1741 void
1742prof_child_enter(tm)
1743 proftime_T *tm; /* place to store waittime */
1744{
1745 funccall_T *fc = current_funccal;
1746
1747 if (fc != NULL && fc->func->uf_profiling)
1748 profile_start(&fc->prof_child);
1749 script_prof_save(tm);
1750}
1751
1752/*
1753 * Take care of time spent in a child.
1754 * Should always be called after prof_child_enter().
1755 */
1756 void
1757prof_child_exit(tm)
1758 proftime_T *tm; /* where waittime was stored */
1759{
1760 funccall_T *fc = current_funccal;
1761
1762 if (fc != NULL && fc->func->uf_profiling)
1763 {
1764 profile_end(&fc->prof_child);
1765 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1766 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1767 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1768 }
1769 script_prof_restore(tm);
1770}
1771#endif
1772
1773
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774#ifdef FEAT_FOLDING
1775/*
1776 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1777 * it in "*cp". Doesn't give error messages.
1778 */
1779 int
1780eval_foldexpr(arg, cp)
1781 char_u *arg;
1782 int *cp;
1783{
Bram Moolenaar33570922005-01-25 22:26:29 +00001784 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 int retval;
1786 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001787 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1788 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
1790 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001791 if (use_sandbox)
1792 ++sandbox;
1793 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001795 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 retval = 0;
1797 else
1798 {
1799 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001800 if (tv.v_type == VAR_NUMBER)
1801 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001802 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 retval = 0;
1804 else
1805 {
1806 /* If the result is a string, check if there is a non-digit before
1807 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001808 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 if (!VIM_ISDIGIT(*s) && *s != '-')
1810 *cp = *s++;
1811 retval = atol((char *)s);
1812 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001813 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
1815 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001816 if (use_sandbox)
1817 --sandbox;
1818 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819
1820 return retval;
1821}
1822#endif
1823
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001825 * ":let" list all variable values
1826 * ":let var1 var2" list variable values
1827 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001828 * ":let var += expr" assignment command.
1829 * ":let var -= expr" assignment command.
1830 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001831 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 */
1833 void
1834ex_let(eap)
1835 exarg_T *eap;
1836{
1837 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001838 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001839 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001841 int var_count = 0;
1842 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001843 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001844 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001845 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846
Bram Moolenaardb552d602006-03-23 22:59:57 +00001847 argend = skip_var_list(arg, &var_count, &semicolon);
1848 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001849 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001850 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1851 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001852 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001853 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001855 /*
1856 * ":let" without "=": list variables
1857 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001858 if (*arg == '[')
1859 EMSG(_(e_invarg));
1860 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001862 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001863 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001864 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001866 list_glob_vars(&first);
1867 list_buf_vars(&first);
1868 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001869#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001870 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001871#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001872 list_script_vars(&first);
1873 list_func_vars(&first);
1874 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 eap->nextcmd = check_nextcmd(arg);
1877 }
1878 else
1879 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001880 op[0] = '=';
1881 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001882 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001883 {
1884 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1885 op[0] = expr[-1]; /* +=, -= or .= */
1886 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001887 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (eap->skip)
1890 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001891 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 if (eap->skip)
1893 {
1894 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 --emsg_skip;
1897 }
1898 else if (i != FAIL)
1899 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001900 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001901 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 }
1904 }
1905}
1906
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001907/*
1908 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1909 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001910 * When "nextchars" is not NULL it points to a string with characters that
1911 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1912 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 * Returns OK or FAIL;
1914 */
1915 static int
1916ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1917 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001918 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 int copy; /* copy values from "tv", don't move */
1920 int semicolon; /* from skip_var_list() */
1921 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001922 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923{
1924 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001925 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001926 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001927 listitem_T *item;
1928 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929
1930 if (*arg != '[')
1931 {
1932 /*
1933 * ":let var = expr" or ":for var in list"
1934 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001935 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001936 return FAIL;
1937 return OK;
1938 }
1939
1940 /*
1941 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1942 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001943 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001944 {
1945 EMSG(_(e_listreq));
1946 return FAIL;
1947 }
1948
1949 i = list_len(l);
1950 if (semicolon == 0 && var_count < i)
1951 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001952 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001953 return FAIL;
1954 }
1955 if (var_count - semicolon > i)
1956 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001957 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 return FAIL;
1959 }
1960
1961 item = l->lv_first;
1962 while (*arg != ']')
1963 {
1964 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001965 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 item = item->li_next;
1967 if (arg == NULL)
1968 return FAIL;
1969
1970 arg = skipwhite(arg);
1971 if (*arg == ';')
1972 {
1973 /* Put the rest of the list (may be empty) in the var after ';'.
1974 * Create a new list for this. */
1975 l = list_alloc();
1976 if (l == NULL)
1977 return FAIL;
1978 while (item != NULL)
1979 {
1980 list_append_tv(l, &item->li_tv);
1981 item = item->li_next;
1982 }
1983
1984 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001985 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 ltv.vval.v_list = l;
1987 l->lv_refcount = 1;
1988
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001989 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1990 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991 clear_tv(&ltv);
1992 if (arg == NULL)
1993 return FAIL;
1994 break;
1995 }
1996 else if (*arg != ',' && *arg != ']')
1997 {
1998 EMSG2(_(e_intern2), "ex_let_vars()");
1999 return FAIL;
2000 }
2001 }
2002
2003 return OK;
2004}
2005
2006/*
2007 * Skip over assignable variable "var" or list of variables "[var, var]".
2008 * Used for ":let varvar = expr" and ":for varvar in expr".
2009 * For "[var, var]" increment "*var_count" for each variable.
2010 * for "[var, var; var]" set "semicolon".
2011 * Return NULL for an error.
2012 */
2013 static char_u *
2014skip_var_list(arg, var_count, semicolon)
2015 char_u *arg;
2016 int *var_count;
2017 int *semicolon;
2018{
2019 char_u *p, *s;
2020
2021 if (*arg == '[')
2022 {
2023 /* "[var, var]": find the matching ']'. */
2024 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002025 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 {
2027 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2028 s = skip_var_one(p);
2029 if (s == p)
2030 {
2031 EMSG2(_(e_invarg2), p);
2032 return NULL;
2033 }
2034 ++*var_count;
2035
2036 p = skipwhite(s);
2037 if (*p == ']')
2038 break;
2039 else if (*p == ';')
2040 {
2041 if (*semicolon == 1)
2042 {
2043 EMSG(_("Double ; in list of variables"));
2044 return NULL;
2045 }
2046 *semicolon = 1;
2047 }
2048 else if (*p != ',')
2049 {
2050 EMSG2(_(e_invarg2), p);
2051 return NULL;
2052 }
2053 }
2054 return p + 1;
2055 }
2056 else
2057 return skip_var_one(arg);
2058}
2059
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002060/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002061 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002062 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002063 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064 static char_u *
2065skip_var_one(arg)
2066 char_u *arg;
2067{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002068 if (*arg == '@' && arg[1] != NUL)
2069 return arg + 2;
2070 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2071 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002072}
2073
Bram Moolenaara7043832005-01-21 11:56:39 +00002074/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002075 * List variables for hashtab "ht" with prefix "prefix".
2076 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002078 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002079list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002080 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002081 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002082 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002083 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002084{
Bram Moolenaar33570922005-01-25 22:26:29 +00002085 hashitem_T *hi;
2086 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002087 int todo;
2088
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002089 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002090 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2091 {
2092 if (!HASHITEM_EMPTY(hi))
2093 {
2094 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002095 di = HI2DI(hi);
2096 if (empty || di->di_tv.v_type != VAR_STRING
2097 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002098 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002099 }
2100 }
2101}
2102
2103/*
2104 * List global variables.
2105 */
2106 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002107list_glob_vars(first)
2108 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002109{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002110 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002111}
2112
2113/*
2114 * List buffer variables.
2115 */
2116 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002117list_buf_vars(first)
2118 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002119{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120 char_u numbuf[NUMBUFLEN];
2121
Bram Moolenaar429fa852013-04-15 12:27:36 +02002122 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002123 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002124
2125 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002126 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2127 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002128}
2129
2130/*
2131 * List window variables.
2132 */
2133 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002134list_win_vars(first)
2135 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002136{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002137 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002138 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002139}
2140
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002141#ifdef FEAT_WINDOWS
2142/*
2143 * List tab page variables.
2144 */
2145 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002146list_tab_vars(first)
2147 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002148{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002149 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002150 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002151}
2152#endif
2153
Bram Moolenaara7043832005-01-21 11:56:39 +00002154/*
2155 * List Vim variables.
2156 */
2157 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002158list_vim_vars(first)
2159 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002160{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002161 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002162}
2163
2164/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002165 * List script-local variables, if there is a script.
2166 */
2167 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168list_script_vars(first)
2169 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002170{
2171 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002172 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2173 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002174}
2175
2176/*
2177 * List function variables, if there is a function.
2178 */
2179 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180list_func_vars(first)
2181 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002182{
2183 if (current_funccal != NULL)
2184 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002185 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002186}
2187
2188/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002189 * List variables in "arg".
2190 */
2191 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002192list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002193 exarg_T *eap;
2194 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002195 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002196{
2197 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002198 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002199 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002200 char_u *name_start;
2201 char_u *arg_subsc;
2202 char_u *tofree;
2203 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002204
2205 while (!ends_excmd(*arg) && !got_int)
2206 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002207 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002208 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002209 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002210 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2211 {
2212 emsg_severe = TRUE;
2213 EMSG(_(e_trailing));
2214 break;
2215 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002217 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002218 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002219 /* get_name_len() takes care of expanding curly braces */
2220 name_start = name = arg;
2221 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2222 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002224 /* This is mainly to keep test 49 working: when expanding
2225 * curly braces fails overrule the exception error message. */
2226 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002228 emsg_severe = TRUE;
2229 EMSG2(_(e_invarg2), arg);
2230 break;
2231 }
2232 error = TRUE;
2233 }
2234 else
2235 {
2236 if (tofree != NULL)
2237 name = tofree;
2238 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002240 else
2241 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002242 /* handle d.key, l[idx], f(expr) */
2243 arg_subsc = arg;
2244 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002245 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002246 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002247 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002248 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002249 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002251 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002252 case 'g': list_glob_vars(first); break;
2253 case 'b': list_buf_vars(first); break;
2254 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002255#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002256 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002257#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002258 case 'v': list_vim_vars(first); break;
2259 case 's': list_script_vars(first); break;
2260 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002261 default:
2262 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002264 }
2265 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 {
2267 char_u numbuf[NUMBUFLEN];
2268 char_u *tf;
2269 int c;
2270 char_u *s;
2271
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002272 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 c = *arg;
2274 *arg = NUL;
2275 list_one_var_a((char_u *)"",
2276 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002277 tv.v_type,
2278 s == NULL ? (char_u *)"" : s,
2279 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 *arg = c;
2281 vim_free(tf);
2282 }
2283 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002284 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 }
2286 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002287
2288 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002290
2291 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 }
2293
2294 return arg;
2295}
2296
2297/*
2298 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2299 * Returns a pointer to the char just after the var name.
2300 * Returns NULL if there is an error.
2301 */
2302 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002303ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002304 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002305 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002306 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002307 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002308 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002309{
2310 int c1;
2311 char_u *name;
2312 char_u *p;
2313 char_u *arg_end = NULL;
2314 int len;
2315 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002316 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002317
2318 /*
2319 * ":let $VAR = expr": Set environment variable.
2320 */
2321 if (*arg == '$')
2322 {
2323 /* Find the end of the name. */
2324 ++arg;
2325 name = arg;
2326 len = get_env_len(&arg);
2327 if (len == 0)
2328 EMSG2(_(e_invarg2), name - 1);
2329 else
2330 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002331 if (op != NULL && (*op == '+' || *op == '-'))
2332 EMSG2(_(e_letwrong), op);
2333 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002334 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002335 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002336 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 {
2338 c1 = name[len];
2339 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002340 p = get_tv_string_chk(tv);
2341 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002342 {
2343 int mustfree = FALSE;
2344 char_u *s = vim_getenv(name, &mustfree);
2345
2346 if (s != NULL)
2347 {
2348 p = tofree = concat_str(s, p);
2349 if (mustfree)
2350 vim_free(s);
2351 }
2352 }
2353 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002354 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002355 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002356 if (STRICMP(name, "HOME") == 0)
2357 init_homedir();
2358 else if (didset_vim && STRICMP(name, "VIM") == 0)
2359 didset_vim = FALSE;
2360 else if (didset_vimruntime
2361 && STRICMP(name, "VIMRUNTIME") == 0)
2362 didset_vimruntime = FALSE;
2363 arg_end = arg;
2364 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002366 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002367 }
2368 }
2369 }
2370
2371 /*
2372 * ":let &option = expr": Set option value.
2373 * ":let &l:option = expr": Set local option value.
2374 * ":let &g:option = expr": Set global option value.
2375 */
2376 else if (*arg == '&')
2377 {
2378 /* Find the end of the name. */
2379 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002380 if (p == NULL || (endchars != NULL
2381 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002382 EMSG(_(e_letunexp));
2383 else
2384 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002385 long n;
2386 int opt_type;
2387 long numval;
2388 char_u *stringval = NULL;
2389 char_u *s;
2390
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002391 c1 = *p;
2392 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393
2394 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 s = get_tv_string_chk(tv); /* != NULL if number or string */
2396 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 {
2398 opt_type = get_option_value(arg, &numval,
2399 &stringval, opt_flags);
2400 if ((opt_type == 1 && *op == '.')
2401 || (opt_type == 0 && *op != '.'))
2402 EMSG2(_(e_letwrong), op);
2403 else
2404 {
2405 if (opt_type == 1) /* number */
2406 {
2407 if (*op == '+')
2408 n = numval + n;
2409 else
2410 n = numval - n;
2411 }
2412 else if (opt_type == 0 && stringval != NULL) /* string */
2413 {
2414 s = concat_str(stringval, s);
2415 vim_free(stringval);
2416 stringval = s;
2417 }
2418 }
2419 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002420 if (s != NULL)
2421 {
2422 set_option_value(arg, n, s, opt_flags);
2423 arg_end = p;
2424 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002425 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002426 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 }
2428 }
2429
2430 /*
2431 * ":let @r = expr": Set register contents.
2432 */
2433 else if (*arg == '@')
2434 {
2435 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002436 if (op != NULL && (*op == '+' || *op == '-'))
2437 EMSG2(_(e_letwrong), op);
2438 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002439 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 EMSG(_(e_letunexp));
2441 else
2442 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002443 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002444 char_u *s;
2445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002446 p = get_tv_string_chk(tv);
2447 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002449 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450 if (s != NULL)
2451 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002452 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 vim_free(s);
2454 }
2455 }
2456 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002458 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002459 arg_end = arg + 1;
2460 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002461 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002462 }
2463 }
2464
2465 /*
2466 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002467 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002468 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002469 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002470 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002471 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002472
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002473 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002474 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002475 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002476 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2477 EMSG(_(e_letunexp));
2478 else
2479 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002480 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002481 arg_end = p;
2482 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002484 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002485 }
2486
2487 else
2488 EMSG2(_(e_invarg2), arg);
2489
2490 return arg_end;
2491}
2492
2493/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002494 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2495 */
2496 static int
2497check_changedtick(arg)
2498 char_u *arg;
2499{
2500 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2501 {
2502 EMSG2(_(e_readonlyvar), arg);
2503 return TRUE;
2504 }
2505 return FALSE;
2506}
2507
2508/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002509 * Get an lval: variable, Dict item or List item that can be assigned a value
2510 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2511 * "name.key", "name.key[expr]" etc.
2512 * Indexing only works if "name" is an existing List or Dictionary.
2513 * "name" points to the start of the name.
2514 * If "rettv" is not NULL it points to the value to be assigned.
2515 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2516 * wrong; must end in space or cmd separator.
2517 *
2518 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002519 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002520 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521 */
2522 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002523get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002525 typval_T *rettv;
2526 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002527 int unlet;
2528 int skip;
2529 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002530 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 char_u *expr_start, *expr_end;
2534 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002535 dictitem_T *v;
2536 typval_T var1;
2537 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002539 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002541 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002542 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002543
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002544 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002545 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546
2547 if (skip)
2548 {
2549 /* When skipping just find the end of the name. */
2550 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002551 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002552 }
2553
2554 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002555 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002556 if (expr_start != NULL)
2557 {
2558 /* Don't expand the name when we already know there is an error. */
2559 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2560 && *p != '[' && *p != '.')
2561 {
2562 EMSG(_(e_trailing));
2563 return NULL;
2564 }
2565
2566 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2567 if (lp->ll_exp_name == NULL)
2568 {
2569 /* Report an invalid expression in braces, unless the
2570 * expression evaluation has been cancelled due to an
2571 * aborting error, an interrupt, or an exception. */
2572 if (!aborting() && !quiet)
2573 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002574 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 EMSG2(_(e_invarg2), name);
2576 return NULL;
2577 }
2578 }
2579 lp->ll_name = lp->ll_exp_name;
2580 }
2581 else
2582 lp->ll_name = name;
2583
2584 /* Without [idx] or .key we are done. */
2585 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2586 return p;
2587
2588 cc = *p;
2589 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002590 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 if (v == NULL && !quiet)
2592 EMSG2(_(e_undefvar), lp->ll_name);
2593 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002594 if (v == NULL)
2595 return NULL;
2596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 /*
2598 * Loop until no more [idx] or .key is following.
2599 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002600 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002601 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2604 && !(lp->ll_tv->v_type == VAR_DICT
2605 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607 if (!quiet)
2608 EMSG(_("E689: Can only index a List or Dictionary"));
2609 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002610 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002612 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 if (!quiet)
2614 EMSG(_("E708: [:] must come last"));
2615 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002616 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002617
Bram Moolenaar8c711452005-01-14 21:53:12 +00002618 len = -1;
2619 if (*p == '.')
2620 {
2621 key = p + 1;
2622 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2623 ;
2624 if (len == 0)
2625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002626 if (!quiet)
2627 EMSG(_(e_emptykey));
2628 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002629 }
2630 p = key + len;
2631 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002632 else
2633 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002634 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002635 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002636 if (*p == ':')
2637 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002638 else
2639 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002640 empty1 = FALSE;
2641 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002642 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002643 if (get_tv_string_chk(&var1) == NULL)
2644 {
2645 /* not a number or string */
2646 clear_tv(&var1);
2647 return NULL;
2648 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002649 }
2650
2651 /* Optionally get the second index [ :expr]. */
2652 if (*p == ':')
2653 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002657 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002658 if (!empty1)
2659 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002661 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (rettv != NULL && (rettv->v_type != VAR_LIST
2663 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002664 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002665 if (!quiet)
2666 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002667 if (!empty1)
2668 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002670 }
2671 p = skipwhite(p + 1);
2672 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 else
2675 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002676 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2678 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 if (!empty1)
2680 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002681 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002683 if (get_tv_string_chk(&var2) == NULL)
2684 {
2685 /* not a number or string */
2686 if (!empty1)
2687 clear_tv(&var1);
2688 clear_tv(&var2);
2689 return NULL;
2690 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002691 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002693 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002699 if (!quiet)
2700 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 if (!empty1)
2702 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002704 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002705 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 }
2707
2708 /* Skip to past ']'. */
2709 ++p;
2710 }
2711
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002713 {
2714 if (len == -1)
2715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002717 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002718 if (*key == NUL)
2719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (!quiet)
2721 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002722 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 }
2725 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002726 lp->ll_list = NULL;
2727 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002728 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002729
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002730 /* When assigning to a scope dictionary check that a function and
2731 * variable name is valid (only variable name unless it is l: or
2732 * g: dictionary). Disallow overwriting a builtin function. */
2733 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002734 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002735 int prevval;
2736 int wrong;
2737
2738 if (len != -1)
2739 {
2740 prevval = key[len];
2741 key[len] = NUL;
2742 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002743 else
2744 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002745 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2746 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002747 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002748 || !valid_varname(key);
2749 if (len != -1)
2750 key[len] = prevval;
2751 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002752 return NULL;
2753 }
2754
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002757 /* Can't add "v:" variable. */
2758 if (lp->ll_dict == &vimvardict)
2759 {
2760 EMSG2(_(e_illvar), name);
2761 return NULL;
2762 }
2763
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002764 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002767 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002768 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 if (len == -1)
2770 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 }
2773 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002774 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 if (len == -1)
2778 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 p = NULL;
2781 break;
2782 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002783 /* existing variable, need to check if it can be changed */
2784 else if (var_check_ro(lp->ll_di->di_flags, name))
2785 return NULL;
2786
Bram Moolenaar8c711452005-01-14 21:53:12 +00002787 if (len == -1)
2788 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 }
2791 else
2792 {
2793 /*
2794 * Get the number and item for the only or first index of the List.
2795 */
2796 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002797 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002798 else
2799 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002800 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002801 clear_tv(&var1);
2802 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002803 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002804 lp->ll_list = lp->ll_tv->vval.v_list;
2805 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2806 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002807 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002808 if (lp->ll_n1 < 0)
2809 {
2810 lp->ll_n1 = 0;
2811 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2812 }
2813 }
2814 if (lp->ll_li == NULL)
2815 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002818 if (!quiet)
2819 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002821 }
2822
2823 /*
2824 * May need to find the item or absolute index for the second
2825 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 * When no index given: "lp->ll_empty2" is TRUE.
2827 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002831 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002837 {
2838 if (!quiet)
2839 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002841 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 }
2844
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2846 if (lp->ll_n1 < 0)
2847 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2848 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002849 {
2850 if (!quiet)
2851 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002852 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002853 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002854 }
2855
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002857 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002858 }
2859
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 return p;
2861}
2862
2863/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002864 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 */
2866 static void
2867clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002868 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869{
2870 vim_free(lp->ll_exp_name);
2871 vim_free(lp->ll_newkey);
2872}
2873
2874/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002875 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002877 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002878 */
2879 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002880set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002881 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002883 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002884 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002885 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886{
2887 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002888 listitem_T *ri;
2889 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890
2891 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002892 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002894 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 cc = *endp;
2896 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002897 if (op != NULL && *op != '=')
2898 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002899 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002900
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002901 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002902 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002903 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002904 {
2905 if (tv_op(&tv, rettv, op) == OK)
2906 set_var(lp->ll_name, &tv, FALSE);
2907 clear_tv(&tv);
2908 }
2909 }
2910 else
2911 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002913 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002914 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002915 else if (tv_check_lock(lp->ll_newkey == NULL
2916 ? lp->ll_tv->v_lock
2917 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2918 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 else if (lp->ll_range)
2920 {
2921 /*
2922 * Assign the List values to the list items.
2923 */
2924 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002925 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002926 if (op != NULL && *op != '=')
2927 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2928 else
2929 {
2930 clear_tv(&lp->ll_li->li_tv);
2931 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2932 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 ri = ri->li_next;
2934 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2935 break;
2936 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002937 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002939 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002940 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 ri = NULL;
2942 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002944 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 lp->ll_li = lp->ll_li->li_next;
2946 ++lp->ll_n1;
2947 }
2948 if (ri != NULL)
2949 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002950 else if (lp->ll_empty2
2951 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 : lp->ll_n1 != lp->ll_n2)
2953 EMSG(_("E711: List value has not enough items"));
2954 }
2955 else
2956 {
2957 /*
2958 * Assign to a List or Dictionary item.
2959 */
2960 if (lp->ll_newkey != NULL)
2961 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 if (op != NULL && *op != '=')
2963 {
2964 EMSG2(_(e_letwrong), op);
2965 return;
2966 }
2967
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002968 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002969 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002970 if (di == NULL)
2971 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002972 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2973 {
2974 vim_free(di);
2975 return;
2976 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002978 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002979 else if (op != NULL && *op != '=')
2980 {
2981 tv_op(lp->ll_tv, rettv, op);
2982 return;
2983 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002984 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002985 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002986
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002987 /*
2988 * Assign the value to the variable or list item.
2989 */
2990 if (copy)
2991 copy_tv(rettv, lp->ll_tv);
2992 else
2993 {
2994 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002995 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002996 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002997 }
2998 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002999}
3000
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003001/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003002 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3003 * Returns OK or FAIL.
3004 */
3005 static int
3006tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003007 typval_T *tv1;
3008 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003009 char_u *op;
3010{
3011 long n;
3012 char_u numbuf[NUMBUFLEN];
3013 char_u *s;
3014
3015 /* Can't do anything with a Funcref or a Dict on the right. */
3016 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3017 {
3018 switch (tv1->v_type)
3019 {
3020 case VAR_DICT:
3021 case VAR_FUNC:
3022 break;
3023
3024 case VAR_LIST:
3025 if (*op != '+' || tv2->v_type != VAR_LIST)
3026 break;
3027 /* List += List */
3028 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3029 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3030 return OK;
3031
3032 case VAR_NUMBER:
3033 case VAR_STRING:
3034 if (tv2->v_type == VAR_LIST)
3035 break;
3036 if (*op == '+' || *op == '-')
3037 {
3038 /* nr += nr or nr -= nr*/
3039 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003040#ifdef FEAT_FLOAT
3041 if (tv2->v_type == VAR_FLOAT)
3042 {
3043 float_T f = n;
3044
3045 if (*op == '+')
3046 f += tv2->vval.v_float;
3047 else
3048 f -= tv2->vval.v_float;
3049 clear_tv(tv1);
3050 tv1->v_type = VAR_FLOAT;
3051 tv1->vval.v_float = f;
3052 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003054#endif
3055 {
3056 if (*op == '+')
3057 n += get_tv_number(tv2);
3058 else
3059 n -= get_tv_number(tv2);
3060 clear_tv(tv1);
3061 tv1->v_type = VAR_NUMBER;
3062 tv1->vval.v_number = n;
3063 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003064 }
3065 else
3066 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003067 if (tv2->v_type == VAR_FLOAT)
3068 break;
3069
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003070 /* str .= str */
3071 s = get_tv_string(tv1);
3072 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3073 clear_tv(tv1);
3074 tv1->v_type = VAR_STRING;
3075 tv1->vval.v_string = s;
3076 }
3077 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003078
3079#ifdef FEAT_FLOAT
3080 case VAR_FLOAT:
3081 {
3082 float_T f;
3083
3084 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3085 && tv2->v_type != VAR_NUMBER
3086 && tv2->v_type != VAR_STRING))
3087 break;
3088 if (tv2->v_type == VAR_FLOAT)
3089 f = tv2->vval.v_float;
3090 else
3091 f = get_tv_number(tv2);
3092 if (*op == '+')
3093 tv1->vval.v_float += f;
3094 else
3095 tv1->vval.v_float -= f;
3096 }
3097 return OK;
3098#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 }
3100 }
3101
3102 EMSG2(_(e_letwrong), op);
3103 return FAIL;
3104}
3105
3106/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003107 * Add a watcher to a list.
3108 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003109 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003110list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003111 list_T *l;
3112 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003113{
3114 lw->lw_next = l->lv_watch;
3115 l->lv_watch = lw;
3116}
3117
3118/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003119 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003120 * No warning when it isn't found...
3121 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003122 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003123list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003124 list_T *l;
3125 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003126{
Bram Moolenaar33570922005-01-25 22:26:29 +00003127 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003128
3129 lwp = &l->lv_watch;
3130 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3131 {
3132 if (lw == lwrem)
3133 {
3134 *lwp = lw->lw_next;
3135 break;
3136 }
3137 lwp = &lw->lw_next;
3138 }
3139}
3140
3141/*
3142 * Just before removing an item from a list: advance watchers to the next
3143 * item.
3144 */
3145 static void
3146list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003147 list_T *l;
3148 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003149{
Bram Moolenaar33570922005-01-25 22:26:29 +00003150 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003151
3152 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3153 if (lw->lw_item == item)
3154 lw->lw_item = item->li_next;
3155}
3156
3157/*
3158 * Evaluate the expression used in a ":for var in expr" command.
3159 * "arg" points to "var".
3160 * Set "*errp" to TRUE for an error, FALSE otherwise;
3161 * Return a pointer that holds the info. Null when there is an error.
3162 */
3163 void *
3164eval_for_line(arg, errp, nextcmdp, skip)
3165 char_u *arg;
3166 int *errp;
3167 char_u **nextcmdp;
3168 int skip;
3169{
Bram Moolenaar33570922005-01-25 22:26:29 +00003170 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003171 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003172 typval_T tv;
3173 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003174
3175 *errp = TRUE; /* default: there is an error */
3176
Bram Moolenaar33570922005-01-25 22:26:29 +00003177 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003178 if (fi == NULL)
3179 return NULL;
3180
3181 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3182 if (expr == NULL)
3183 return fi;
3184
3185 expr = skipwhite(expr);
3186 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3187 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003188 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 return fi;
3190 }
3191
3192 if (skip)
3193 ++emsg_skip;
3194 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3195 {
3196 *errp = FALSE;
3197 if (!skip)
3198 {
3199 l = tv.vval.v_list;
3200 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003201 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003203 clear_tv(&tv);
3204 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205 else
3206 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003207 /* No need to increment the refcount, it's already set for the
3208 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003209 fi->fi_list = l;
3210 list_add_watch(l, &fi->fi_lw);
3211 fi->fi_lw.lw_item = l->lv_first;
3212 }
3213 }
3214 }
3215 if (skip)
3216 --emsg_skip;
3217
3218 return fi;
3219}
3220
3221/*
3222 * Use the first item in a ":for" list. Advance to the next.
3223 * Assign the values to the variable (list). "arg" points to the first one.
3224 * Return TRUE when a valid item was found, FALSE when at end of list or
3225 * something wrong.
3226 */
3227 int
3228next_for_item(fi_void, arg)
3229 void *fi_void;
3230 char_u *arg;
3231{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003232 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
3236 item = fi->fi_lw.lw_item;
3237 if (item == NULL)
3238 result = FALSE;
3239 else
3240 {
3241 fi->fi_lw.lw_item = item->li_next;
3242 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3243 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3244 }
3245 return result;
3246}
3247
3248/*
3249 * Free the structure used to store info used by ":for".
3250 */
3251 void
3252free_for_info(fi_void)
3253 void *fi_void;
3254{
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003257 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003258 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003260 list_unref(fi->fi_list);
3261 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 vim_free(fi);
3263}
3264
Bram Moolenaar071d4272004-06-13 20:20:40 +00003265#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3266
3267 void
3268set_context_for_expression(xp, arg, cmdidx)
3269 expand_T *xp;
3270 char_u *arg;
3271 cmdidx_T cmdidx;
3272{
3273 int got_eq = FALSE;
3274 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003277 if (cmdidx == CMD_let)
3278 {
3279 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003280 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 {
3282 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003283 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003284 {
3285 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003286 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 if (vim_iswhite(*p))
3288 break;
3289 }
3290 return;
3291 }
3292 }
3293 else
3294 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3295 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003296 while ((xp->xp_pattern = vim_strpbrk(arg,
3297 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3298 {
3299 c = *xp->xp_pattern;
3300 if (c == '&')
3301 {
3302 c = xp->xp_pattern[1];
3303 if (c == '&')
3304 {
3305 ++xp->xp_pattern;
3306 xp->xp_context = cmdidx != CMD_let || got_eq
3307 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3308 }
3309 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003310 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003312 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3313 xp->xp_pattern += 2;
3314
3315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 }
3317 else if (c == '$')
3318 {
3319 /* environment variable */
3320 xp->xp_context = EXPAND_ENV_VARS;
3321 }
3322 else if (c == '=')
3323 {
3324 got_eq = TRUE;
3325 xp->xp_context = EXPAND_EXPRESSION;
3326 }
3327 else if (c == '<'
3328 && xp->xp_context == EXPAND_FUNCTIONS
3329 && vim_strchr(xp->xp_pattern, '(') == NULL)
3330 {
3331 /* Function name can start with "<SNR>" */
3332 break;
3333 }
3334 else if (cmdidx != CMD_let || got_eq)
3335 {
3336 if (c == '"') /* string */
3337 {
3338 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3339 if (c == '\\' && xp->xp_pattern[1] != NUL)
3340 ++xp->xp_pattern;
3341 xp->xp_context = EXPAND_NOTHING;
3342 }
3343 else if (c == '\'') /* literal string */
3344 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003345 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3347 /* skip */ ;
3348 xp->xp_context = EXPAND_NOTHING;
3349 }
3350 else if (c == '|')
3351 {
3352 if (xp->xp_pattern[1] == '|')
3353 {
3354 ++xp->xp_pattern;
3355 xp->xp_context = EXPAND_EXPRESSION;
3356 }
3357 else
3358 xp->xp_context = EXPAND_COMMANDS;
3359 }
3360 else
3361 xp->xp_context = EXPAND_EXPRESSION;
3362 }
3363 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003364 /* Doesn't look like something valid, expand as an expression
3365 * anyway. */
3366 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 arg = xp->xp_pattern;
3368 if (*arg != NUL)
3369 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3370 /* skip */ ;
3371 }
3372 xp->xp_pattern = arg;
3373}
3374
3375#endif /* FEAT_CMDL_COMPL */
3376
3377/*
3378 * ":1,25call func(arg1, arg2)" function call.
3379 */
3380 void
3381ex_call(eap)
3382 exarg_T *eap;
3383{
3384 char_u *arg = eap->arg;
3385 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003387 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003389 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 linenr_T lnum;
3391 int doesrange;
3392 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003393 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003395 if (eap->skip)
3396 {
3397 /* trans_function_name() doesn't work well when skipping, use eval0()
3398 * instead to skip to any following command, e.g. for:
3399 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003400 ++emsg_skip;
3401 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3402 clear_tv(&rettv);
3403 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003404 return;
3405 }
3406
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003407 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003408 if (fudi.fd_newkey != NULL)
3409 {
3410 /* Still need to give an error message for missing key. */
3411 EMSG2(_(e_dictkey), fudi.fd_newkey);
3412 vim_free(fudi.fd_newkey);
3413 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003414 if (tofree == NULL)
3415 return;
3416
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003417 /* Increase refcount on dictionary, it could get deleted when evaluating
3418 * the arguments. */
3419 if (fudi.fd_dict != NULL)
3420 ++fudi.fd_dict->dv_refcount;
3421
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003422 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003423 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003424 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003425
Bram Moolenaar532c7802005-01-27 14:44:31 +00003426 /* Skip white space to allow ":call func ()". Not good, but required for
3427 * backward compatibility. */
3428 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003429 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430
3431 if (*startarg != '(')
3432 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003433 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 goto end;
3435 }
3436
3437 /*
3438 * When skipping, evaluate the function once, to find the end of the
3439 * arguments.
3440 * When the function takes a range, this is discovered after the first
3441 * call, and the loop is broken.
3442 */
3443 if (eap->skip)
3444 {
3445 ++emsg_skip;
3446 lnum = eap->line2; /* do it once, also with an invalid range */
3447 }
3448 else
3449 lnum = eap->line1;
3450 for ( ; lnum <= eap->line2; ++lnum)
3451 {
3452 if (!eap->skip && eap->addr_count > 0)
3453 {
3454 curwin->w_cursor.lnum = lnum;
3455 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003456#ifdef FEAT_VIRTUALEDIT
3457 curwin->w_cursor.coladd = 0;
3458#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 }
3460 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003461 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003462 eap->line1, eap->line2, &doesrange,
3463 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 {
3465 failed = TRUE;
3466 break;
3467 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003468
3469 /* Handle a function returning a Funcref, Dictionary or List. */
3470 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3471 {
3472 failed = TRUE;
3473 break;
3474 }
3475
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003476 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477 if (doesrange || eap->skip)
3478 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003479
Bram Moolenaar071d4272004-06-13 20:20:40 +00003480 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003481 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003482 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003483 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003484 if (aborting())
3485 break;
3486 }
3487 if (eap->skip)
3488 --emsg_skip;
3489
3490 if (!failed)
3491 {
3492 /* Check for trailing illegal characters and a following command. */
3493 if (!ends_excmd(*arg))
3494 {
3495 emsg_severe = TRUE;
3496 EMSG(_(e_trailing));
3497 }
3498 else
3499 eap->nextcmd = check_nextcmd(arg);
3500 }
3501
3502end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003503 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003504 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505}
3506
3507/*
3508 * ":unlet[!] var1 ... " command.
3509 */
3510 void
3511ex_unlet(eap)
3512 exarg_T *eap;
3513{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003514 ex_unletlock(eap, eap->arg, 0);
3515}
3516
3517/*
3518 * ":lockvar" and ":unlockvar" commands
3519 */
3520 void
3521ex_lockvar(eap)
3522 exarg_T *eap;
3523{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003525 int deep = 2;
3526
3527 if (eap->forceit)
3528 deep = -1;
3529 else if (vim_isdigit(*arg))
3530 {
3531 deep = getdigits(&arg);
3532 arg = skipwhite(arg);
3533 }
3534
3535 ex_unletlock(eap, arg, deep);
3536}
3537
3538/*
3539 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3540 */
3541 static void
3542ex_unletlock(eap, argstart, deep)
3543 exarg_T *eap;
3544 char_u *argstart;
3545 int deep;
3546{
3547 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003550 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551
3552 do
3553 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003554 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003555 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3556 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003557 if (lv.ll_name == NULL)
3558 error = TRUE; /* error but continue parsing */
3559 if (name_end == NULL || (!vim_iswhite(*name_end)
3560 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003562 if (name_end != NULL)
3563 {
3564 emsg_severe = TRUE;
3565 EMSG(_(e_trailing));
3566 }
3567 if (!(eap->skip || error))
3568 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 break;
3570 }
3571
3572 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003573 {
3574 if (eap->cmdidx == CMD_unlet)
3575 {
3576 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3577 error = TRUE;
3578 }
3579 else
3580 {
3581 if (do_lock_var(&lv, name_end, deep,
3582 eap->cmdidx == CMD_lockvar) == FAIL)
3583 error = TRUE;
3584 }
3585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003587 if (!eap->skip)
3588 clear_lval(&lv);
3589
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 arg = skipwhite(name_end);
3591 } while (!ends_excmd(*arg));
3592
3593 eap->nextcmd = check_nextcmd(arg);
3594}
3595
Bram Moolenaar8c711452005-01-14 21:53:12 +00003596 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003597do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003598 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003599 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003600 int forceit;
3601{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003602 int ret = OK;
3603 int cc;
3604
3605 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003606 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003607 cc = *name_end;
3608 *name_end = NUL;
3609
3610 /* Normal name or expanded name. */
3611 if (check_changedtick(lp->ll_name))
3612 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003613 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003615 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003616 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003617 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3618 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003619 else if (lp->ll_range)
3620 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003621 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003622
3623 /* Delete a range of List items. */
3624 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3625 {
3626 li = lp->ll_li->li_next;
3627 listitem_remove(lp->ll_list, lp->ll_li);
3628 lp->ll_li = li;
3629 ++lp->ll_n1;
3630 }
3631 }
3632 else
3633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 /* unlet a List item. */
3636 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003638 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003639 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640 }
3641
3642 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003643}
3644
Bram Moolenaar071d4272004-06-13 20:20:40 +00003645/*
3646 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003647 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 */
3649 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003650do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003652 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
Bram Moolenaar33570922005-01-25 22:26:29 +00003654 hashtab_T *ht;
3655 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003656 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003657 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658
Bram Moolenaar33570922005-01-25 22:26:29 +00003659 ht = find_var_ht(name, &varname);
3660 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003662 hi = hash_find(ht, varname);
3663 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003664 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003665 di = HI2DI(hi);
3666 if (var_check_fixed(di->di_flags, name)
3667 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003668 return FAIL;
3669 delete_var(ht, hi);
3670 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003673 if (forceit)
3674 return OK;
3675 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676 return FAIL;
3677}
3678
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003679/*
3680 * Lock or unlock variable indicated by "lp".
3681 * "deep" is the levels to go (-1 for unlimited);
3682 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3683 */
3684 static int
3685do_lock_var(lp, name_end, deep, lock)
3686 lval_T *lp;
3687 char_u *name_end;
3688 int deep;
3689 int lock;
3690{
3691 int ret = OK;
3692 int cc;
3693 dictitem_T *di;
3694
3695 if (deep == 0) /* nothing to do */
3696 return OK;
3697
3698 if (lp->ll_tv == NULL)
3699 {
3700 cc = *name_end;
3701 *name_end = NUL;
3702
3703 /* Normal name or expanded name. */
3704 if (check_changedtick(lp->ll_name))
3705 ret = FAIL;
3706 else
3707 {
3708 di = find_var(lp->ll_name, NULL);
3709 if (di == NULL)
3710 ret = FAIL;
3711 else
3712 {
3713 if (lock)
3714 di->di_flags |= DI_FLAGS_LOCK;
3715 else
3716 di->di_flags &= ~DI_FLAGS_LOCK;
3717 item_lock(&di->di_tv, deep, lock);
3718 }
3719 }
3720 *name_end = cc;
3721 }
3722 else if (lp->ll_range)
3723 {
3724 listitem_T *li = lp->ll_li;
3725
3726 /* (un)lock a range of List items. */
3727 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3728 {
3729 item_lock(&li->li_tv, deep, lock);
3730 li = li->li_next;
3731 ++lp->ll_n1;
3732 }
3733 }
3734 else if (lp->ll_list != NULL)
3735 /* (un)lock a List item. */
3736 item_lock(&lp->ll_li->li_tv, deep, lock);
3737 else
3738 /* un(lock) a Dictionary item. */
3739 item_lock(&lp->ll_di->di_tv, deep, lock);
3740
3741 return ret;
3742}
3743
3744/*
3745 * Lock or unlock an item. "deep" is nr of levels to go.
3746 */
3747 static void
3748item_lock(tv, deep, lock)
3749 typval_T *tv;
3750 int deep;
3751 int lock;
3752{
3753 static int recurse = 0;
3754 list_T *l;
3755 listitem_T *li;
3756 dict_T *d;
3757 hashitem_T *hi;
3758 int todo;
3759
3760 if (recurse >= DICT_MAXNEST)
3761 {
3762 EMSG(_("E743: variable nested too deep for (un)lock"));
3763 return;
3764 }
3765 if (deep == 0)
3766 return;
3767 ++recurse;
3768
3769 /* lock/unlock the item itself */
3770 if (lock)
3771 tv->v_lock |= VAR_LOCKED;
3772 else
3773 tv->v_lock &= ~VAR_LOCKED;
3774
3775 switch (tv->v_type)
3776 {
3777 case VAR_LIST:
3778 if ((l = tv->vval.v_list) != NULL)
3779 {
3780 if (lock)
3781 l->lv_lock |= VAR_LOCKED;
3782 else
3783 l->lv_lock &= ~VAR_LOCKED;
3784 if (deep < 0 || deep > 1)
3785 /* recursive: lock/unlock the items the List contains */
3786 for (li = l->lv_first; li != NULL; li = li->li_next)
3787 item_lock(&li->li_tv, deep - 1, lock);
3788 }
3789 break;
3790 case VAR_DICT:
3791 if ((d = tv->vval.v_dict) != NULL)
3792 {
3793 if (lock)
3794 d->dv_lock |= VAR_LOCKED;
3795 else
3796 d->dv_lock &= ~VAR_LOCKED;
3797 if (deep < 0 || deep > 1)
3798 {
3799 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003800 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003801 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3802 {
3803 if (!HASHITEM_EMPTY(hi))
3804 {
3805 --todo;
3806 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3807 }
3808 }
3809 }
3810 }
3811 }
3812 --recurse;
3813}
3814
Bram Moolenaara40058a2005-07-11 22:42:07 +00003815/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003816 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3817 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003818 */
3819 static int
3820tv_islocked(tv)
3821 typval_T *tv;
3822{
3823 return (tv->v_lock & VAR_LOCKED)
3824 || (tv->v_type == VAR_LIST
3825 && tv->vval.v_list != NULL
3826 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3827 || (tv->v_type == VAR_DICT
3828 && tv->vval.v_dict != NULL
3829 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3830}
3831
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3833/*
3834 * Delete all "menutrans_" variables.
3835 */
3836 void
3837del_menutrans_vars()
3838{
Bram Moolenaar33570922005-01-25 22:26:29 +00003839 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003840 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841
Bram Moolenaar33570922005-01-25 22:26:29 +00003842 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003843 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003844 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003845 {
3846 if (!HASHITEM_EMPTY(hi))
3847 {
3848 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003849 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3850 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003851 }
3852 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003853 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854}
3855#endif
3856
3857#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3858
3859/*
3860 * Local string buffer for the next two functions to store a variable name
3861 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3862 * get_user_var_name().
3863 */
3864
3865static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3866
3867static char_u *varnamebuf = NULL;
3868static int varnamebuflen = 0;
3869
3870/*
3871 * Function to concatenate a prefix and a variable name.
3872 */
3873 static char_u *
3874cat_prefix_varname(prefix, name)
3875 int prefix;
3876 char_u *name;
3877{
3878 int len;
3879
3880 len = (int)STRLEN(name) + 3;
3881 if (len > varnamebuflen)
3882 {
3883 vim_free(varnamebuf);
3884 len += 10; /* some additional space */
3885 varnamebuf = alloc(len);
3886 if (varnamebuf == NULL)
3887 {
3888 varnamebuflen = 0;
3889 return NULL;
3890 }
3891 varnamebuflen = len;
3892 }
3893 *varnamebuf = prefix;
3894 varnamebuf[1] = ':';
3895 STRCPY(varnamebuf + 2, name);
3896 return varnamebuf;
3897}
3898
3899/*
3900 * Function given to ExpandGeneric() to obtain the list of user defined
3901 * (global/buffer/window/built-in) variable names.
3902 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903 char_u *
3904get_user_var_name(xp, idx)
3905 expand_T *xp;
3906 int idx;
3907{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003908 static long_u gdone;
3909 static long_u bdone;
3910 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003911#ifdef FEAT_WINDOWS
3912 static long_u tdone;
3913#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003914 static int vidx;
3915 static hashitem_T *hi;
3916 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917
3918 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003919 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003920 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003921#ifdef FEAT_WINDOWS
3922 tdone = 0;
3923#endif
3924 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003925
3926 /* Global variables */
3927 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003929 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003930 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003931 else
3932 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003933 while (HASHITEM_EMPTY(hi))
3934 ++hi;
3935 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3936 return cat_prefix_varname('g', hi->hi_key);
3937 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003939
3940 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003941 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003942 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003944 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003945 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003946 else
3947 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 while (HASHITEM_EMPTY(hi))
3949 ++hi;
3950 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 return (char_u *)"b:changedtick";
3956 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003957
3958 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003959 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003962 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003964 else
3965 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003966 while (HASHITEM_EMPTY(hi))
3967 ++hi;
3968 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003970
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003971#ifdef FEAT_WINDOWS
3972 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003973 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003974 if (tdone < ht->ht_used)
3975 {
3976 if (tdone++ == 0)
3977 hi = ht->ht_array;
3978 else
3979 ++hi;
3980 while (HASHITEM_EMPTY(hi))
3981 ++hi;
3982 return cat_prefix_varname('t', hi->hi_key);
3983 }
3984#endif
3985
Bram Moolenaar33570922005-01-25 22:26:29 +00003986 /* v: variables */
3987 if (vidx < VV_LEN)
3988 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989
3990 vim_free(varnamebuf);
3991 varnamebuf = NULL;
3992 varnamebuflen = 0;
3993 return NULL;
3994}
3995
3996#endif /* FEAT_CMDL_COMPL */
3997
3998/*
3999 * types for expressions.
4000 */
4001typedef enum
4002{
4003 TYPE_UNKNOWN = 0
4004 , TYPE_EQUAL /* == */
4005 , TYPE_NEQUAL /* != */
4006 , TYPE_GREATER /* > */
4007 , TYPE_GEQUAL /* >= */
4008 , TYPE_SMALLER /* < */
4009 , TYPE_SEQUAL /* <= */
4010 , TYPE_MATCH /* =~ */
4011 , TYPE_NOMATCH /* !~ */
4012} exptype_T;
4013
4014/*
4015 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004016 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4018 */
4019
4020/*
4021 * Handle zero level expression.
4022 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004023 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004024 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004025 * Return OK or FAIL.
4026 */
4027 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004028eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004029 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004030 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 char_u **nextcmd;
4032 int evaluate;
4033{
4034 int ret;
4035 char_u *p;
4036
4037 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004038 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 if (ret == FAIL || !ends_excmd(*p))
4040 {
4041 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004042 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 /*
4044 * Report the invalid expression unless the expression evaluation has
4045 * been cancelled due to an aborting error, an interrupt, or an
4046 * exception.
4047 */
4048 if (!aborting())
4049 EMSG2(_(e_invexpr2), arg);
4050 ret = FAIL;
4051 }
4052 if (nextcmd != NULL)
4053 *nextcmd = check_nextcmd(p);
4054
4055 return ret;
4056}
4057
4058/*
4059 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004060 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 *
4062 * "arg" must point to the first non-white of the expression.
4063 * "arg" is advanced to the next non-white after the recognized expression.
4064 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004065 * Note: "rettv.v_lock" is not set.
4066 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 * Return OK or FAIL.
4068 */
4069 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004070eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004072 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 int evaluate;
4074{
4075 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004076 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077
4078 /*
4079 * Get the first variable.
4080 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004081 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 return FAIL;
4083
4084 if ((*arg)[0] == '?')
4085 {
4086 result = FALSE;
4087 if (evaluate)
4088 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004089 int error = FALSE;
4090
4091 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004093 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004094 if (error)
4095 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 }
4097
4098 /*
4099 * Get the second variable.
4100 */
4101 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004102 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 return FAIL;
4104
4105 /*
4106 * Check for the ":".
4107 */
4108 if ((*arg)[0] != ':')
4109 {
4110 EMSG(_("E109: Missing ':' after '?'"));
4111 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004112 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 return FAIL;
4114 }
4115
4116 /*
4117 * Get the third variable.
4118 */
4119 *arg = skipwhite(*arg + 1);
4120 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4121 {
4122 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004123 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 return FAIL;
4125 }
4126 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128 }
4129
4130 return OK;
4131}
4132
4133/*
4134 * Handle first level expression:
4135 * expr2 || expr2 || expr2 logical OR
4136 *
4137 * "arg" must point to the first non-white of the expression.
4138 * "arg" is advanced to the next non-white after the recognized expression.
4139 *
4140 * Return OK or FAIL.
4141 */
4142 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004143eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004145 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 int evaluate;
4147{
Bram Moolenaar33570922005-01-25 22:26:29 +00004148 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 long result;
4150 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004151 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152
4153 /*
4154 * Get the first variable.
4155 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004156 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 return FAIL;
4158
4159 /*
4160 * Repeat until there is no following "||".
4161 */
4162 first = TRUE;
4163 result = FALSE;
4164 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4165 {
4166 if (evaluate && first)
4167 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004168 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004170 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004171 if (error)
4172 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 first = FALSE;
4174 }
4175
4176 /*
4177 * Get the second variable.
4178 */
4179 *arg = skipwhite(*arg + 2);
4180 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4181 return FAIL;
4182
4183 /*
4184 * Compute the result.
4185 */
4186 if (evaluate && !result)
4187 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004188 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004191 if (error)
4192 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
4194 if (evaluate)
4195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 rettv->v_type = VAR_NUMBER;
4197 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 }
4199 }
4200
4201 return OK;
4202}
4203
4204/*
4205 * Handle second level expression:
4206 * expr3 && expr3 && expr3 logical AND
4207 *
4208 * "arg" must point to the first non-white of the expression.
4209 * "arg" is advanced to the next non-white after the recognized expression.
4210 *
4211 * Return OK or FAIL.
4212 */
4213 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004214eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004216 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 int evaluate;
4218{
Bram Moolenaar33570922005-01-25 22:26:29 +00004219 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 long result;
4221 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004222 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223
4224 /*
4225 * Get the first variable.
4226 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004227 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 return FAIL;
4229
4230 /*
4231 * Repeat until there is no following "&&".
4232 */
4233 first = TRUE;
4234 result = TRUE;
4235 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4236 {
4237 if (evaluate && first)
4238 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004242 if (error)
4243 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244 first = FALSE;
4245 }
4246
4247 /*
4248 * Get the second variable.
4249 */
4250 *arg = skipwhite(*arg + 2);
4251 if (eval4(arg, &var2, evaluate && result) == FAIL)
4252 return FAIL;
4253
4254 /*
4255 * Compute the result.
4256 */
4257 if (evaluate && result)
4258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004259 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004261 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004262 if (error)
4263 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265 if (evaluate)
4266 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004267 rettv->v_type = VAR_NUMBER;
4268 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 }
4271
4272 return OK;
4273}
4274
4275/*
4276 * Handle third level expression:
4277 * var1 == var2
4278 * var1 =~ var2
4279 * var1 != var2
4280 * var1 !~ var2
4281 * var1 > var2
4282 * var1 >= var2
4283 * var1 < var2
4284 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004285 * var1 is var2
4286 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 *
4288 * "arg" must point to the first non-white of the expression.
4289 * "arg" is advanced to the next non-white after the recognized expression.
4290 *
4291 * Return OK or FAIL.
4292 */
4293 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004296 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 int evaluate;
4298{
Bram Moolenaar33570922005-01-25 22:26:29 +00004299 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 char_u *p;
4301 int i;
4302 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004303 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 int len = 2;
4305 long n1, n2;
4306 char_u *s1, *s2;
4307 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4308 regmatch_T regmatch;
4309 int ic;
4310 char_u *save_cpo;
4311
4312 /*
4313 * Get the first variable.
4314 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004315 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 return FAIL;
4317
4318 p = *arg;
4319 switch (p[0])
4320 {
4321 case '=': if (p[1] == '=')
4322 type = TYPE_EQUAL;
4323 else if (p[1] == '~')
4324 type = TYPE_MATCH;
4325 break;
4326 case '!': if (p[1] == '=')
4327 type = TYPE_NEQUAL;
4328 else if (p[1] == '~')
4329 type = TYPE_NOMATCH;
4330 break;
4331 case '>': if (p[1] != '=')
4332 {
4333 type = TYPE_GREATER;
4334 len = 1;
4335 }
4336 else
4337 type = TYPE_GEQUAL;
4338 break;
4339 case '<': if (p[1] != '=')
4340 {
4341 type = TYPE_SMALLER;
4342 len = 1;
4343 }
4344 else
4345 type = TYPE_SEQUAL;
4346 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004347 case 'i': if (p[1] == 's')
4348 {
4349 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4350 len = 5;
4351 if (!vim_isIDc(p[len]))
4352 {
4353 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4354 type_is = TRUE;
4355 }
4356 }
4357 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359
4360 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004361 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 */
4363 if (type != TYPE_UNKNOWN)
4364 {
4365 /* extra question mark appended: ignore case */
4366 if (p[len] == '?')
4367 {
4368 ic = TRUE;
4369 ++len;
4370 }
4371 /* extra '#' appended: match case */
4372 else if (p[len] == '#')
4373 {
4374 ic = FALSE;
4375 ++len;
4376 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004377 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 else
4379 ic = p_ic;
4380
4381 /*
4382 * Get the second variable.
4383 */
4384 *arg = skipwhite(p + len);
4385 if (eval5(arg, &var2, evaluate) == FAIL)
4386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004387 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 return FAIL;
4389 }
4390
4391 if (evaluate)
4392 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004393 if (type_is && rettv->v_type != var2.v_type)
4394 {
4395 /* For "is" a different type always means FALSE, for "notis"
4396 * it means TRUE. */
4397 n1 = (type == TYPE_NEQUAL);
4398 }
4399 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4400 {
4401 if (type_is)
4402 {
4403 n1 = (rettv->v_type == var2.v_type
4404 && rettv->vval.v_list == var2.vval.v_list);
4405 if (type == TYPE_NEQUAL)
4406 n1 = !n1;
4407 }
4408 else if (rettv->v_type != var2.v_type
4409 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4410 {
4411 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004412 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004413 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004414 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004415 clear_tv(rettv);
4416 clear_tv(&var2);
4417 return FAIL;
4418 }
4419 else
4420 {
4421 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004422 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4423 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 if (type == TYPE_NEQUAL)
4425 n1 = !n1;
4426 }
4427 }
4428
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004429 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4430 {
4431 if (type_is)
4432 {
4433 n1 = (rettv->v_type == var2.v_type
4434 && rettv->vval.v_dict == var2.vval.v_dict);
4435 if (type == TYPE_NEQUAL)
4436 n1 = !n1;
4437 }
4438 else if (rettv->v_type != var2.v_type
4439 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4440 {
4441 if (rettv->v_type != var2.v_type)
4442 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4443 else
4444 EMSG(_("E736: Invalid operation for Dictionary"));
4445 clear_tv(rettv);
4446 clear_tv(&var2);
4447 return FAIL;
4448 }
4449 else
4450 {
4451 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004452 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4453 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004454 if (type == TYPE_NEQUAL)
4455 n1 = !n1;
4456 }
4457 }
4458
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004459 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4460 {
4461 if (rettv->v_type != var2.v_type
4462 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4463 {
4464 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004465 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004466 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004467 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004468 clear_tv(rettv);
4469 clear_tv(&var2);
4470 return FAIL;
4471 }
4472 else
4473 {
4474 /* Compare two Funcrefs for being equal or unequal. */
4475 if (rettv->vval.v_string == NULL
4476 || var2.vval.v_string == NULL)
4477 n1 = FALSE;
4478 else
4479 n1 = STRCMP(rettv->vval.v_string,
4480 var2.vval.v_string) == 0;
4481 if (type == TYPE_NEQUAL)
4482 n1 = !n1;
4483 }
4484 }
4485
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004486#ifdef FEAT_FLOAT
4487 /*
4488 * If one of the two variables is a float, compare as a float.
4489 * When using "=~" or "!~", always compare as string.
4490 */
4491 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4492 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4493 {
4494 float_T f1, f2;
4495
4496 if (rettv->v_type == VAR_FLOAT)
4497 f1 = rettv->vval.v_float;
4498 else
4499 f1 = get_tv_number(rettv);
4500 if (var2.v_type == VAR_FLOAT)
4501 f2 = var2.vval.v_float;
4502 else
4503 f2 = get_tv_number(&var2);
4504 n1 = FALSE;
4505 switch (type)
4506 {
4507 case TYPE_EQUAL: n1 = (f1 == f2); break;
4508 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4509 case TYPE_GREATER: n1 = (f1 > f2); break;
4510 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4511 case TYPE_SMALLER: n1 = (f1 < f2); break;
4512 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4513 case TYPE_UNKNOWN:
4514 case TYPE_MATCH:
4515 case TYPE_NOMATCH: break; /* avoid gcc warning */
4516 }
4517 }
4518#endif
4519
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 /*
4521 * If one of the two variables is a number, compare as a number.
4522 * When using "=~" or "!~", always compare as string.
4523 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004524 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4526 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004527 n1 = get_tv_number(rettv);
4528 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 switch (type)
4530 {
4531 case TYPE_EQUAL: n1 = (n1 == n2); break;
4532 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4533 case TYPE_GREATER: n1 = (n1 > n2); break;
4534 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4535 case TYPE_SMALLER: n1 = (n1 < n2); break;
4536 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4537 case TYPE_UNKNOWN:
4538 case TYPE_MATCH:
4539 case TYPE_NOMATCH: break; /* avoid gcc warning */
4540 }
4541 }
4542 else
4543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004544 s1 = get_tv_string_buf(rettv, buf1);
4545 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4547 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4548 else
4549 i = 0;
4550 n1 = FALSE;
4551 switch (type)
4552 {
4553 case TYPE_EQUAL: n1 = (i == 0); break;
4554 case TYPE_NEQUAL: n1 = (i != 0); break;
4555 case TYPE_GREATER: n1 = (i > 0); break;
4556 case TYPE_GEQUAL: n1 = (i >= 0); break;
4557 case TYPE_SMALLER: n1 = (i < 0); break;
4558 case TYPE_SEQUAL: n1 = (i <= 0); break;
4559
4560 case TYPE_MATCH:
4561 case TYPE_NOMATCH:
4562 /* avoid 'l' flag in 'cpoptions' */
4563 save_cpo = p_cpo;
4564 p_cpo = (char_u *)"";
4565 regmatch.regprog = vim_regcomp(s2,
4566 RE_MAGIC + RE_STRING);
4567 regmatch.rm_ic = ic;
4568 if (regmatch.regprog != NULL)
4569 {
4570 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004571 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 if (type == TYPE_NOMATCH)
4573 n1 = !n1;
4574 }
4575 p_cpo = save_cpo;
4576 break;
4577
4578 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4579 }
4580 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004581 clear_tv(rettv);
4582 clear_tv(&var2);
4583 rettv->v_type = VAR_NUMBER;
4584 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 }
4586 }
4587
4588 return OK;
4589}
4590
4591/*
4592 * Handle fourth level expression:
4593 * + number addition
4594 * - number subtraction
4595 * . string concatenation
4596 *
4597 * "arg" must point to the first non-white of the expression.
4598 * "arg" is advanced to the next non-white after the recognized expression.
4599 *
4600 * Return OK or FAIL.
4601 */
4602 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004603eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004605 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 int evaluate;
4607{
Bram Moolenaar33570922005-01-25 22:26:29 +00004608 typval_T var2;
4609 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 int op;
4611 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004612#ifdef FEAT_FLOAT
4613 float_T f1 = 0, f2 = 0;
4614#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 char_u *s1, *s2;
4616 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4617 char_u *p;
4618
4619 /*
4620 * Get the first variable.
4621 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004622 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 return FAIL;
4624
4625 /*
4626 * Repeat computing, until no '+', '-' or '.' is following.
4627 */
4628 for (;;)
4629 {
4630 op = **arg;
4631 if (op != '+' && op != '-' && op != '.')
4632 break;
4633
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004634 if ((op != '+' || rettv->v_type != VAR_LIST)
4635#ifdef FEAT_FLOAT
4636 && (op == '.' || rettv->v_type != VAR_FLOAT)
4637#endif
4638 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004639 {
4640 /* For "list + ...", an illegal use of the first operand as
4641 * a number cannot be determined before evaluating the 2nd
4642 * operand: if this is also a list, all is ok.
4643 * For "something . ...", "something - ..." or "non-list + ...",
4644 * we know that the first operand needs to be a string or number
4645 * without evaluating the 2nd operand. So check before to avoid
4646 * side effects after an error. */
4647 if (evaluate && get_tv_string_chk(rettv) == NULL)
4648 {
4649 clear_tv(rettv);
4650 return FAIL;
4651 }
4652 }
4653
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654 /*
4655 * Get the second variable.
4656 */
4657 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004658 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004660 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 return FAIL;
4662 }
4663
4664 if (evaluate)
4665 {
4666 /*
4667 * Compute the result.
4668 */
4669 if (op == '.')
4670 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004671 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4672 s2 = get_tv_string_buf_chk(&var2, buf2);
4673 if (s2 == NULL) /* type error ? */
4674 {
4675 clear_tv(rettv);
4676 clear_tv(&var2);
4677 return FAIL;
4678 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004679 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004680 clear_tv(rettv);
4681 rettv->v_type = VAR_STRING;
4682 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004684 else if (op == '+' && rettv->v_type == VAR_LIST
4685 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004686 {
4687 /* concatenate Lists */
4688 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4689 &var3) == FAIL)
4690 {
4691 clear_tv(rettv);
4692 clear_tv(&var2);
4693 return FAIL;
4694 }
4695 clear_tv(rettv);
4696 *rettv = var3;
4697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 else
4699 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004700 int error = FALSE;
4701
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004702#ifdef FEAT_FLOAT
4703 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004704 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004705 f1 = rettv->vval.v_float;
4706 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004707 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004708 else
4709#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004710 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004711 n1 = get_tv_number_chk(rettv, &error);
4712 if (error)
4713 {
4714 /* This can only happen for "list + non-list". For
4715 * "non-list + ..." or "something - ...", we returned
4716 * before evaluating the 2nd operand. */
4717 clear_tv(rettv);
4718 return FAIL;
4719 }
4720#ifdef FEAT_FLOAT
4721 if (var2.v_type == VAR_FLOAT)
4722 f1 = n1;
4723#endif
4724 }
4725#ifdef FEAT_FLOAT
4726 if (var2.v_type == VAR_FLOAT)
4727 {
4728 f2 = var2.vval.v_float;
4729 n2 = 0;
4730 }
4731 else
4732#endif
4733 {
4734 n2 = get_tv_number_chk(&var2, &error);
4735 if (error)
4736 {
4737 clear_tv(rettv);
4738 clear_tv(&var2);
4739 return FAIL;
4740 }
4741#ifdef FEAT_FLOAT
4742 if (rettv->v_type == VAR_FLOAT)
4743 f2 = n2;
4744#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004745 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004746 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004747
4748#ifdef FEAT_FLOAT
4749 /* If there is a float on either side the result is a float. */
4750 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4751 {
4752 if (op == '+')
4753 f1 = f1 + f2;
4754 else
4755 f1 = f1 - f2;
4756 rettv->v_type = VAR_FLOAT;
4757 rettv->vval.v_float = f1;
4758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004760#endif
4761 {
4762 if (op == '+')
4763 n1 = n1 + n2;
4764 else
4765 n1 = n1 - n2;
4766 rettv->v_type = VAR_NUMBER;
4767 rettv->vval.v_number = n1;
4768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 }
4772 }
4773 return OK;
4774}
4775
4776/*
4777 * Handle fifth level expression:
4778 * * number multiplication
4779 * / number division
4780 * % number modulo
4781 *
4782 * "arg" must point to the first non-white of the expression.
4783 * "arg" is advanced to the next non-white after the recognized expression.
4784 *
4785 * Return OK or FAIL.
4786 */
4787 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004788eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004790 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004792 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793{
Bram Moolenaar33570922005-01-25 22:26:29 +00004794 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 int op;
4796 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004797#ifdef FEAT_FLOAT
4798 int use_float = FALSE;
4799 float_T f1 = 0, f2;
4800#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004801 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802
4803 /*
4804 * Get the first variable.
4805 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004806 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 return FAIL;
4808
4809 /*
4810 * Repeat computing, until no '*', '/' or '%' is following.
4811 */
4812 for (;;)
4813 {
4814 op = **arg;
4815 if (op != '*' && op != '/' && op != '%')
4816 break;
4817
4818 if (evaluate)
4819 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004820#ifdef FEAT_FLOAT
4821 if (rettv->v_type == VAR_FLOAT)
4822 {
4823 f1 = rettv->vval.v_float;
4824 use_float = TRUE;
4825 n1 = 0;
4826 }
4827 else
4828#endif
4829 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004831 if (error)
4832 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 }
4834 else
4835 n1 = 0;
4836
4837 /*
4838 * Get the second variable.
4839 */
4840 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004841 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 return FAIL;
4843
4844 if (evaluate)
4845 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004846#ifdef FEAT_FLOAT
4847 if (var2.v_type == VAR_FLOAT)
4848 {
4849 if (!use_float)
4850 {
4851 f1 = n1;
4852 use_float = TRUE;
4853 }
4854 f2 = var2.vval.v_float;
4855 n2 = 0;
4856 }
4857 else
4858#endif
4859 {
4860 n2 = get_tv_number_chk(&var2, &error);
4861 clear_tv(&var2);
4862 if (error)
4863 return FAIL;
4864#ifdef FEAT_FLOAT
4865 if (use_float)
4866 f2 = n2;
4867#endif
4868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869
4870 /*
4871 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004872 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004874#ifdef FEAT_FLOAT
4875 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004877 if (op == '*')
4878 f1 = f1 * f2;
4879 else if (op == '/')
4880 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004881# ifdef VMS
4882 /* VMS crashes on divide by zero, work around it */
4883 if (f2 == 0.0)
4884 {
4885 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004886 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004887 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004888 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004889 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004890 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004891 }
4892 else
4893 f1 = f1 / f2;
4894# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004895 /* We rely on the floating point library to handle divide
4896 * by zero to result in "inf" and not a crash. */
4897 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004898# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004901 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004902 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903 return FAIL;
4904 }
4905 rettv->v_type = VAR_FLOAT;
4906 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
4908 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004911 if (op == '*')
4912 n1 = n1 * n2;
4913 else if (op == '/')
4914 {
4915 if (n2 == 0) /* give an error message? */
4916 {
4917 if (n1 == 0)
4918 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4919 else if (n1 < 0)
4920 n1 = -0x7fffffffL;
4921 else
4922 n1 = 0x7fffffffL;
4923 }
4924 else
4925 n1 = n1 / n2;
4926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004928 {
4929 if (n2 == 0) /* give an error message? */
4930 n1 = 0;
4931 else
4932 n1 = n1 % n2;
4933 }
4934 rettv->v_type = VAR_NUMBER;
4935 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938 }
4939
4940 return OK;
4941}
4942
4943/*
4944 * Handle sixth level expression:
4945 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004946 * "string" string constant
4947 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 * &option-name option value
4949 * @r register contents
4950 * identifier variable value
4951 * function() function call
4952 * $VAR environment variable
4953 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004954 * [expr, expr] List
4955 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 *
4957 * Also handle:
4958 * ! in front logical NOT
4959 * - in front unary minus
4960 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004961 * trailing [] subscript in String or List
4962 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 *
4964 * "arg" must point to the first non-white of the expression.
4965 * "arg" is advanced to the next non-white after the recognized expression.
4966 *
4967 * Return OK or FAIL.
4968 */
4969 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004970eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004972 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004974 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 long n;
4977 int len;
4978 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 char_u *start_leader, *end_leader;
4980 int ret = OK;
4981 char_u *alias;
4982
4983 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004984 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004985 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004987 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004988
4989 /*
4990 * Skip '!' and '-' characters. They are handled later.
4991 */
4992 start_leader = *arg;
4993 while (**arg == '!' || **arg == '-' || **arg == '+')
4994 *arg = skipwhite(*arg + 1);
4995 end_leader = *arg;
4996
4997 switch (**arg)
4998 {
4999 /*
5000 * Number constant.
5001 */
5002 case '0':
5003 case '1':
5004 case '2':
5005 case '3':
5006 case '4':
5007 case '5':
5008 case '6':
5009 case '7':
5010 case '8':
5011 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 {
5013#ifdef FEAT_FLOAT
5014 char_u *p = skipdigits(*arg + 1);
5015 int get_float = FALSE;
5016
5017 /* We accept a float when the format matches
5018 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005019 * strict to avoid backwards compatibility problems.
5020 * Don't look for a float after the "." operator, so that
5021 * ":let vers = 1.2.3" doesn't fail. */
5022 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005024 get_float = TRUE;
5025 p = skipdigits(p + 2);
5026 if (*p == 'e' || *p == 'E')
5027 {
5028 ++p;
5029 if (*p == '-' || *p == '+')
5030 ++p;
5031 if (!vim_isdigit(*p))
5032 get_float = FALSE;
5033 else
5034 p = skipdigits(p + 1);
5035 }
5036 if (ASCII_ISALPHA(*p) || *p == '.')
5037 get_float = FALSE;
5038 }
5039 if (get_float)
5040 {
5041 float_T f;
5042
5043 *arg += string2float(*arg, &f);
5044 if (evaluate)
5045 {
5046 rettv->v_type = VAR_FLOAT;
5047 rettv->vval.v_float = f;
5048 }
5049 }
5050 else
5051#endif
5052 {
5053 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5054 *arg += len;
5055 if (evaluate)
5056 {
5057 rettv->v_type = VAR_NUMBER;
5058 rettv->vval.v_number = n;
5059 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 }
5061 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005062 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063
5064 /*
5065 * String constant: "string".
5066 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005067 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 break;
5069
5070 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005071 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005073 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005074 break;
5075
5076 /*
5077 * List: [expr, expr]
5078 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 break;
5081
5082 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005083 * Dictionary: {key: val, key: val}
5084 */
5085 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5086 break;
5087
5088 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005089 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005091 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 break;
5093
5094 /*
5095 * Environment variable: $VAR.
5096 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005097 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 break;
5099
5100 /*
5101 * Register contents: @r.
5102 */
5103 case '@': ++*arg;
5104 if (evaluate)
5105 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005106 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005107 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109 if (**arg != NUL)
5110 ++*arg;
5111 break;
5112
5113 /*
5114 * nested expression: (expression).
5115 */
5116 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005117 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 if (**arg == ')')
5119 ++*arg;
5120 else if (ret == OK)
5121 {
5122 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005123 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 ret = FAIL;
5125 }
5126 break;
5127
Bram Moolenaar8c711452005-01-14 21:53:12 +00005128 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 break;
5130 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005131
5132 if (ret == NOTDONE)
5133 {
5134 /*
5135 * Must be a variable or function name.
5136 * Can also be a curly-braces kind of name: {expr}.
5137 */
5138 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005139 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005140 if (alias != NULL)
5141 s = alias;
5142
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005143 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005144 ret = FAIL;
5145 else
5146 {
5147 if (**arg == '(') /* recursive! */
5148 {
5149 /* If "s" is the name of a variable of type VAR_FUNC
5150 * use its contents. */
5151 s = deref_func_name(s, &len);
5152
5153 /* Invoke the function. */
5154 ret = get_func_tv(s, len, rettv, arg,
5155 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005156 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005157
5158 /* If evaluate is FALSE rettv->v_type was not set in
5159 * get_func_tv, but it's needed in handle_subscript() to parse
5160 * what follows. So set it here. */
5161 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5162 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005163 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005164 rettv->v_type = VAR_FUNC;
5165 }
5166
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 /* Stop the expression evaluation when immediately
5168 * aborting on error, or when an interrupt occurred or
5169 * an exception was thrown but not caught. */
5170 if (aborting())
5171 {
5172 if (ret == OK)
5173 clear_tv(rettv);
5174 ret = FAIL;
5175 }
5176 }
5177 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005178 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005179 else
5180 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005181 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005182 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005183 }
5184
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 *arg = skipwhite(*arg);
5186
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005187 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5188 * expr(expr). */
5189 if (ret == OK)
5190 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191
5192 /*
5193 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5194 */
5195 if (ret == OK && evaluate && end_leader > start_leader)
5196 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005197 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005198 int val = 0;
5199#ifdef FEAT_FLOAT
5200 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005201
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005202 if (rettv->v_type == VAR_FLOAT)
5203 f = rettv->vval.v_float;
5204 else
5205#endif
5206 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005207 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005209 clear_tv(rettv);
5210 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005212 else
5213 {
5214 while (end_leader > start_leader)
5215 {
5216 --end_leader;
5217 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005218 {
5219#ifdef FEAT_FLOAT
5220 if (rettv->v_type == VAR_FLOAT)
5221 f = !f;
5222 else
5223#endif
5224 val = !val;
5225 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005226 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005227 {
5228#ifdef FEAT_FLOAT
5229 if (rettv->v_type == VAR_FLOAT)
5230 f = -f;
5231 else
5232#endif
5233 val = -val;
5234 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005235 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005236#ifdef FEAT_FLOAT
5237 if (rettv->v_type == VAR_FLOAT)
5238 {
5239 clear_tv(rettv);
5240 rettv->vval.v_float = f;
5241 }
5242 else
5243#endif
5244 {
5245 clear_tv(rettv);
5246 rettv->v_type = VAR_NUMBER;
5247 rettv->vval.v_number = val;
5248 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
5251
5252 return ret;
5253}
5254
5255/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005256 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5257 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005258 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5259 */
5260 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005261eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005262 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005263 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005264 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005265 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005266{
5267 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005268 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005269 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 long len = -1;
5271 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005272 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005274
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005275 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005277 if (verbose)
5278 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005279 return FAIL;
5280 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005281#ifdef FEAT_FLOAT
5282 else if (rettv->v_type == VAR_FLOAT)
5283 {
5284 if (verbose)
5285 EMSG(_(e_float_as_string));
5286 return FAIL;
5287 }
5288#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005289
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005291 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 /*
5293 * dict.name
5294 */
5295 key = *arg + 1;
5296 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5297 ;
5298 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005299 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005300 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005301 }
5302 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005303 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005304 /*
5305 * something[idx]
5306 *
5307 * Get the (first) variable from inside the [].
5308 */
5309 *arg = skipwhite(*arg + 1);
5310 if (**arg == ':')
5311 empty1 = TRUE;
5312 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5313 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005314 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5315 {
5316 /* not a number or string */
5317 clear_tv(&var1);
5318 return FAIL;
5319 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005320
5321 /*
5322 * Get the second variable from inside the [:].
5323 */
5324 if (**arg == ':')
5325 {
5326 range = TRUE;
5327 *arg = skipwhite(*arg + 1);
5328 if (**arg == ']')
5329 empty2 = TRUE;
5330 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5331 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005332 if (!empty1)
5333 clear_tv(&var1);
5334 return FAIL;
5335 }
5336 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5337 {
5338 /* not a number or string */
5339 if (!empty1)
5340 clear_tv(&var1);
5341 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005342 return FAIL;
5343 }
5344 }
5345
5346 /* Check for the ']'. */
5347 if (**arg != ']')
5348 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005349 if (verbose)
5350 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005351 clear_tv(&var1);
5352 if (range)
5353 clear_tv(&var2);
5354 return FAIL;
5355 }
5356 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 }
5358
5359 if (evaluate)
5360 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005361 n1 = 0;
5362 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005364 n1 = get_tv_number(&var1);
5365 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005366 }
5367 if (range)
5368 {
5369 if (empty2)
5370 n2 = -1;
5371 else
5372 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005373 n2 = get_tv_number(&var2);
5374 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 }
5376 }
5377
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005378 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005379 {
5380 case VAR_NUMBER:
5381 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005382 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383 len = (long)STRLEN(s);
5384 if (range)
5385 {
5386 /* The resulting variable is a substring. If the indexes
5387 * are out of range the result is empty. */
5388 if (n1 < 0)
5389 {
5390 n1 = len + n1;
5391 if (n1 < 0)
5392 n1 = 0;
5393 }
5394 if (n2 < 0)
5395 n2 = len + n2;
5396 else if (n2 >= len)
5397 n2 = len;
5398 if (n1 >= len || n2 < 0 || n1 > n2)
5399 s = NULL;
5400 else
5401 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5402 }
5403 else
5404 {
5405 /* The resulting variable is a string of a single
5406 * character. If the index is too big or negative the
5407 * result is empty. */
5408 if (n1 >= len || n1 < 0)
5409 s = NULL;
5410 else
5411 s = vim_strnsave(s + n1, 1);
5412 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005413 clear_tv(rettv);
5414 rettv->v_type = VAR_STRING;
5415 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005416 break;
5417
5418 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005419 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005420 if (n1 < 0)
5421 n1 = len + n1;
5422 if (!empty1 && (n1 < 0 || n1 >= len))
5423 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005424 /* For a range we allow invalid values and return an empty
5425 * list. A list index out of range is an error. */
5426 if (!range)
5427 {
5428 if (verbose)
5429 EMSGN(_(e_listidx), n1);
5430 return FAIL;
5431 }
5432 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 }
5434 if (range)
5435 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005436 list_T *l;
5437 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005438
5439 if (n2 < 0)
5440 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005441 else if (n2 >= len)
5442 n2 = len - 1;
5443 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005444 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005445 l = list_alloc();
5446 if (l == NULL)
5447 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005448 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005449 n1 <= n2; ++n1)
5450 {
5451 if (list_append_tv(l, &item->li_tv) == FAIL)
5452 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005453 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005454 return FAIL;
5455 }
5456 item = item->li_next;
5457 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005458 clear_tv(rettv);
5459 rettv->v_type = VAR_LIST;
5460 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005461 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 }
5463 else
5464 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005465 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 clear_tv(rettv);
5467 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005470
5471 case VAR_DICT:
5472 if (range)
5473 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005474 if (verbose)
5475 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005476 if (len == -1)
5477 clear_tv(&var1);
5478 return FAIL;
5479 }
5480 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005481 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005482
5483 if (len == -1)
5484 {
5485 key = get_tv_string(&var1);
5486 if (*key == NUL)
5487 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005488 if (verbose)
5489 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005490 clear_tv(&var1);
5491 return FAIL;
5492 }
5493 }
5494
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005495 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005496
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005497 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005498 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005499 if (len == -1)
5500 clear_tv(&var1);
5501 if (item == NULL)
5502 return FAIL;
5503
5504 copy_tv(&item->di_tv, &var1);
5505 clear_tv(rettv);
5506 *rettv = var1;
5507 }
5508 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 }
5510 }
5511
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005512 return OK;
5513}
5514
5515/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 * Get an option value.
5517 * "arg" points to the '&' or '+' before the option name.
5518 * "arg" is advanced to character after the option name.
5519 * Return OK or FAIL.
5520 */
5521 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005522get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005524 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 int evaluate;
5526{
5527 char_u *option_end;
5528 long numval;
5529 char_u *stringval;
5530 int opt_type;
5531 int c;
5532 int working = (**arg == '+'); /* has("+option") */
5533 int ret = OK;
5534 int opt_flags;
5535
5536 /*
5537 * Isolate the option name and find its value.
5538 */
5539 option_end = find_option_end(arg, &opt_flags);
5540 if (option_end == NULL)
5541 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005542 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005543 EMSG2(_("E112: Option name missing: %s"), *arg);
5544 return FAIL;
5545 }
5546
5547 if (!evaluate)
5548 {
5549 *arg = option_end;
5550 return OK;
5551 }
5552
5553 c = *option_end;
5554 *option_end = NUL;
5555 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005556 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557
5558 if (opt_type == -3) /* invalid name */
5559 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005560 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561 EMSG2(_("E113: Unknown option: %s"), *arg);
5562 ret = FAIL;
5563 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005564 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565 {
5566 if (opt_type == -2) /* hidden string option */
5567 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 rettv->v_type = VAR_STRING;
5569 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 }
5571 else if (opt_type == -1) /* hidden number option */
5572 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005573 rettv->v_type = VAR_NUMBER;
5574 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 }
5576 else if (opt_type == 1) /* number option */
5577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005578 rettv->v_type = VAR_NUMBER;
5579 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 }
5581 else /* string option */
5582 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005583 rettv->v_type = VAR_STRING;
5584 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 }
5586 }
5587 else if (working && (opt_type == -2 || opt_type == -1))
5588 ret = FAIL;
5589
5590 *option_end = c; /* put back for error messages */
5591 *arg = option_end;
5592
5593 return ret;
5594}
5595
5596/*
5597 * Allocate a variable for a string constant.
5598 * Return OK or FAIL.
5599 */
5600 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005601get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005603 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 int evaluate;
5605{
5606 char_u *p;
5607 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 int extra = 0;
5609
5610 /*
5611 * Find the end of the string, skipping backslashed characters.
5612 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005613 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 {
5615 if (*p == '\\' && p[1] != NUL)
5616 {
5617 ++p;
5618 /* A "\<x>" form occupies at least 4 characters, and produces up
5619 * to 6 characters: reserve space for 2 extra */
5620 if (*p == '<')
5621 extra += 2;
5622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 }
5624
5625 if (*p != '"')
5626 {
5627 EMSG2(_("E114: Missing quote: %s"), *arg);
5628 return FAIL;
5629 }
5630
5631 /* If only parsing, set *arg and return here */
5632 if (!evaluate)
5633 {
5634 *arg = p + 1;
5635 return OK;
5636 }
5637
5638 /*
5639 * Copy the string into allocated memory, handling backslashed
5640 * characters.
5641 */
5642 name = alloc((unsigned)(p - *arg + extra));
5643 if (name == NULL)
5644 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005645 rettv->v_type = VAR_STRING;
5646 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005647
Bram Moolenaar8c711452005-01-14 21:53:12 +00005648 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 {
5650 if (*p == '\\')
5651 {
5652 switch (*++p)
5653 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005654 case 'b': *name++ = BS; ++p; break;
5655 case 'e': *name++ = ESC; ++p; break;
5656 case 'f': *name++ = FF; ++p; break;
5657 case 'n': *name++ = NL; ++p; break;
5658 case 'r': *name++ = CAR; ++p; break;
5659 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
5661 case 'X': /* hex: "\x1", "\x12" */
5662 case 'x':
5663 case 'u': /* Unicode: "\u0023" */
5664 case 'U':
5665 if (vim_isxdigit(p[1]))
5666 {
5667 int n, nr;
5668 int c = toupper(*p);
5669
5670 if (c == 'X')
5671 n = 2;
5672 else
5673 n = 4;
5674 nr = 0;
5675 while (--n >= 0 && vim_isxdigit(p[1]))
5676 {
5677 ++p;
5678 nr = (nr << 4) + hex2nr(*p);
5679 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005680 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681#ifdef FEAT_MBYTE
5682 /* For "\u" store the number according to
5683 * 'encoding'. */
5684 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005685 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 else
5687#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005688 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 break;
5691
5692 /* octal: "\1", "\12", "\123" */
5693 case '0':
5694 case '1':
5695 case '2':
5696 case '3':
5697 case '4':
5698 case '5':
5699 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005700 case '7': *name = *p++ - '0';
5701 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005703 *name = (*name << 3) + *p++ - '0';
5704 if (*p >= '0' && *p <= '7')
5705 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005707 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 break;
5709
5710 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005711 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 if (extra != 0)
5713 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005714 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 break;
5716 }
5717 /* FALLTHROUGH */
5718
Bram Moolenaar8c711452005-01-14 21:53:12 +00005719 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720 break;
5721 }
5722 }
5723 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005724 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725
Bram Moolenaar071d4272004-06-13 20:20:40 +00005726 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005727 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 *arg = p + 1;
5729
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 return OK;
5731}
5732
5733/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005734 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735 * Return OK or FAIL.
5736 */
5737 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005738get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005739 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005740 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 int evaluate;
5742{
5743 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005744 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005745 int reduce = 0;
5746
5747 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005748 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005749 */
5750 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5751 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005753 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005754 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005755 break;
5756 ++reduce;
5757 ++p;
5758 }
5759 }
5760
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005762 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005764 return FAIL;
5765 }
5766
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005768 if (!evaluate)
5769 {
5770 *arg = p + 1;
5771 return OK;
5772 }
5773
5774 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005776 */
5777 str = alloc((unsigned)((p - *arg) - reduce));
5778 if (str == NULL)
5779 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005780 rettv->v_type = VAR_STRING;
5781 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005782
Bram Moolenaar8c711452005-01-14 21:53:12 +00005783 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005784 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005785 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005786 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005788 break;
5789 ++p;
5790 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005791 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005792 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005794 *arg = p + 1;
5795
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005796 return OK;
5797}
5798
5799/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005800 * Allocate a variable for a List and fill it from "*arg".
5801 * Return OK or FAIL.
5802 */
5803 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005804get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005805 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005806 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005807 int evaluate;
5808{
Bram Moolenaar33570922005-01-25 22:26:29 +00005809 list_T *l = NULL;
5810 typval_T tv;
5811 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005812
5813 if (evaluate)
5814 {
5815 l = list_alloc();
5816 if (l == NULL)
5817 return FAIL;
5818 }
5819
5820 *arg = skipwhite(*arg + 1);
5821 while (**arg != ']' && **arg != NUL)
5822 {
5823 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5824 goto failret;
5825 if (evaluate)
5826 {
5827 item = listitem_alloc();
5828 if (item != NULL)
5829 {
5830 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005831 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005832 list_append(l, item);
5833 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005834 else
5835 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005836 }
5837
5838 if (**arg == ']')
5839 break;
5840 if (**arg != ',')
5841 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005843 goto failret;
5844 }
5845 *arg = skipwhite(*arg + 1);
5846 }
5847
5848 if (**arg != ']')
5849 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005850 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005851failret:
5852 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005853 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005854 return FAIL;
5855 }
5856
5857 *arg = skipwhite(*arg + 1);
5858 if (evaluate)
5859 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005860 rettv->v_type = VAR_LIST;
5861 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005862 ++l->lv_refcount;
5863 }
5864
5865 return OK;
5866}
5867
5868/*
5869 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005870 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005871 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005872 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005873list_alloc()
5874{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005875 list_T *l;
5876
5877 l = (list_T *)alloc_clear(sizeof(list_T));
5878 if (l != NULL)
5879 {
5880 /* Prepend the list to the list of lists for garbage collection. */
5881 if (first_list != NULL)
5882 first_list->lv_used_prev = l;
5883 l->lv_used_prev = NULL;
5884 l->lv_used_next = first_list;
5885 first_list = l;
5886 }
5887 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005888}
5889
5890/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005891 * Allocate an empty list for a return value.
5892 * Returns OK or FAIL.
5893 */
5894 static int
5895rettv_list_alloc(rettv)
5896 typval_T *rettv;
5897{
5898 list_T *l = list_alloc();
5899
5900 if (l == NULL)
5901 return FAIL;
5902
5903 rettv->vval.v_list = l;
5904 rettv->v_type = VAR_LIST;
5905 ++l->lv_refcount;
5906 return OK;
5907}
5908
5909/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 * Unreference a list: decrement the reference count and free it when it
5911 * becomes zero.
5912 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005913 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005915 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005916{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005917 if (l != NULL && --l->lv_refcount <= 0)
5918 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919}
5920
5921/*
5922 * Free a list, including all items it points to.
5923 * Ignores the reference count.
5924 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005925 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005926list_free(l, recurse)
5927 list_T *l;
5928 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929{
Bram Moolenaar33570922005-01-25 22:26:29 +00005930 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005931
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005932 /* Remove the list from the list of lists for garbage collection. */
5933 if (l->lv_used_prev == NULL)
5934 first_list = l->lv_used_next;
5935 else
5936 l->lv_used_prev->lv_used_next = l->lv_used_next;
5937 if (l->lv_used_next != NULL)
5938 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5939
Bram Moolenaard9fba312005-06-26 22:34:35 +00005940 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005942 /* Remove the item before deleting it. */
5943 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005944 if (recurse || (item->li_tv.v_type != VAR_LIST
5945 && item->li_tv.v_type != VAR_DICT))
5946 clear_tv(&item->li_tv);
5947 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005948 }
5949 vim_free(l);
5950}
5951
5952/*
5953 * Allocate a list item.
5954 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005955 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005956listitem_alloc()
5957{
Bram Moolenaar33570922005-01-25 22:26:29 +00005958 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005959}
5960
5961/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005962 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005964 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005966 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005967{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005968 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 vim_free(item);
5970}
5971
5972/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005973 * Remove a list item from a List and free it. Also clears the value.
5974 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005975 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005976listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005977 list_T *l;
5978 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005979{
5980 list_remove(l, item, item);
5981 listitem_free(item);
5982}
5983
5984/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985 * Get the number of items in a list.
5986 */
5987 static long
5988list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005989 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 if (l == NULL)
5992 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005993 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994}
5995
5996/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005997 * Return TRUE when two lists have exactly the same values.
5998 */
5999 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006000list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006001 list_T *l1;
6002 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006003 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006004 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006005{
Bram Moolenaar33570922005-01-25 22:26:29 +00006006 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006007
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006008 if (l1 == NULL || l2 == NULL)
6009 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006010 if (l1 == l2)
6011 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006012 if (list_len(l1) != list_len(l2))
6013 return FALSE;
6014
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006015 for (item1 = l1->lv_first, item2 = l2->lv_first;
6016 item1 != NULL && item2 != NULL;
6017 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006018 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006019 return FALSE;
6020 return item1 == NULL && item2 == NULL;
6021}
6022
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006023#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6024 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006025/*
6026 * Return the dictitem that an entry in a hashtable points to.
6027 */
6028 dictitem_T *
6029dict_lookup(hi)
6030 hashitem_T *hi;
6031{
6032 return HI2DI(hi);
6033}
6034#endif
6035
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006036/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006037 * Return TRUE when two dictionaries have exactly the same key/values.
6038 */
6039 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006040dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006041 dict_T *d1;
6042 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006043 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006044 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006045{
Bram Moolenaar33570922005-01-25 22:26:29 +00006046 hashitem_T *hi;
6047 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006048 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006049
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006050 if (d1 == NULL || d2 == NULL)
6051 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006052 if (d1 == d2)
6053 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006054 if (dict_len(d1) != dict_len(d2))
6055 return FALSE;
6056
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006057 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006058 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006059 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006060 if (!HASHITEM_EMPTY(hi))
6061 {
6062 item2 = dict_find(d2, hi->hi_key, -1);
6063 if (item2 == NULL)
6064 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006065 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006066 return FALSE;
6067 --todo;
6068 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006069 }
6070 return TRUE;
6071}
6072
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006073static int tv_equal_recurse_limit;
6074
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006075/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006076 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006077 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006078 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006079 */
6080 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006081tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006082 typval_T *tv1;
6083 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006084 int ic; /* ignore case */
6085 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006086{
6087 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006088 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006089 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006090 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006091
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006092 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006093 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006094
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006095 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006096 * recursiveness to a limit. We guess they are equal then.
6097 * A fixed limit has the problem of still taking an awful long time.
6098 * Reduce the limit every time running into it. That should work fine for
6099 * deeply linked structures that are not recursively linked and catch
6100 * recursiveness quickly. */
6101 if (!recursive)
6102 tv_equal_recurse_limit = 1000;
6103 if (recursive_cnt >= tv_equal_recurse_limit)
6104 {
6105 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006106 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006107 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006108
6109 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006110 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006111 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006112 ++recursive_cnt;
6113 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6114 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006115 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006116
6117 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006118 ++recursive_cnt;
6119 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6120 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006121 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006122
6123 case VAR_FUNC:
6124 return (tv1->vval.v_string != NULL
6125 && tv2->vval.v_string != NULL
6126 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6127
6128 case VAR_NUMBER:
6129 return tv1->vval.v_number == tv2->vval.v_number;
6130
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006131#ifdef FEAT_FLOAT
6132 case VAR_FLOAT:
6133 return tv1->vval.v_float == tv2->vval.v_float;
6134#endif
6135
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006136 case VAR_STRING:
6137 s1 = get_tv_string_buf(tv1, buf1);
6138 s2 = get_tv_string_buf(tv2, buf2);
6139 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006140 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006141
6142 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006143 return TRUE;
6144}
6145
6146/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006147 * Locate item with index "n" in list "l" and return it.
6148 * A negative index is counted from the end; -1 is the last item.
6149 * Returns NULL when "n" is out of range.
6150 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006151 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006152list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006153 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006154 long n;
6155{
Bram Moolenaar33570922005-01-25 22:26:29 +00006156 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006157 long idx;
6158
6159 if (l == NULL)
6160 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006161
6162 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006163 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006164 n = l->lv_len + n;
6165
6166 /* Check for index out of range. */
6167 if (n < 0 || n >= l->lv_len)
6168 return NULL;
6169
6170 /* When there is a cached index may start search from there. */
6171 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006172 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006173 if (n < l->lv_idx / 2)
6174 {
6175 /* closest to the start of the list */
6176 item = l->lv_first;
6177 idx = 0;
6178 }
6179 else if (n > (l->lv_idx + l->lv_len) / 2)
6180 {
6181 /* closest to the end of the list */
6182 item = l->lv_last;
6183 idx = l->lv_len - 1;
6184 }
6185 else
6186 {
6187 /* closest to the cached index */
6188 item = l->lv_idx_item;
6189 idx = l->lv_idx;
6190 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006191 }
6192 else
6193 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006194 if (n < l->lv_len / 2)
6195 {
6196 /* closest to the start of the list */
6197 item = l->lv_first;
6198 idx = 0;
6199 }
6200 else
6201 {
6202 /* closest to the end of the list */
6203 item = l->lv_last;
6204 idx = l->lv_len - 1;
6205 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006206 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006207
6208 while (n > idx)
6209 {
6210 /* search forward */
6211 item = item->li_next;
6212 ++idx;
6213 }
6214 while (n < idx)
6215 {
6216 /* search backward */
6217 item = item->li_prev;
6218 --idx;
6219 }
6220
6221 /* cache the used index */
6222 l->lv_idx = idx;
6223 l->lv_idx_item = item;
6224
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006225 return item;
6226}
6227
6228/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006229 * Get list item "l[idx]" as a number.
6230 */
6231 static long
6232list_find_nr(l, idx, errorp)
6233 list_T *l;
6234 long idx;
6235 int *errorp; /* set to TRUE when something wrong */
6236{
6237 listitem_T *li;
6238
6239 li = list_find(l, idx);
6240 if (li == NULL)
6241 {
6242 if (errorp != NULL)
6243 *errorp = TRUE;
6244 return -1L;
6245 }
6246 return get_tv_number_chk(&li->li_tv, errorp);
6247}
6248
6249/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006250 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6251 */
6252 char_u *
6253list_find_str(l, idx)
6254 list_T *l;
6255 long idx;
6256{
6257 listitem_T *li;
6258
6259 li = list_find(l, idx - 1);
6260 if (li == NULL)
6261 {
6262 EMSGN(_(e_listidx), idx);
6263 return NULL;
6264 }
6265 return get_tv_string(&li->li_tv);
6266}
6267
6268/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006269 * Locate "item" list "l" and return its index.
6270 * Returns -1 when "item" is not in the list.
6271 */
6272 static long
6273list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006274 list_T *l;
6275 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006276{
6277 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006278 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006279
6280 if (l == NULL)
6281 return -1;
6282 idx = 0;
6283 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6284 ++idx;
6285 if (li == NULL)
6286 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006287 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006288}
6289
6290/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291 * Append item "item" to the end of list "l".
6292 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006293 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006294list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 list_T *l;
6296 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297{
6298 if (l->lv_last == NULL)
6299 {
6300 /* empty list */
6301 l->lv_first = item;
6302 l->lv_last = item;
6303 item->li_prev = NULL;
6304 }
6305 else
6306 {
6307 l->lv_last->li_next = item;
6308 item->li_prev = l->lv_last;
6309 l->lv_last = item;
6310 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006311 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 item->li_next = NULL;
6313}
6314
6315/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006316 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006317 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006319 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006321 list_T *l;
6322 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006323{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006324 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006325
Bram Moolenaar05159a02005-02-26 23:04:13 +00006326 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006327 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006328 copy_tv(tv, &li->li_tv);
6329 list_append(l, li);
6330 return OK;
6331}
6332
6333/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006334 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006335 * Return FAIL when out of memory.
6336 */
6337 int
6338list_append_dict(list, dict)
6339 list_T *list;
6340 dict_T *dict;
6341{
6342 listitem_T *li = listitem_alloc();
6343
6344 if (li == NULL)
6345 return FAIL;
6346 li->li_tv.v_type = VAR_DICT;
6347 li->li_tv.v_lock = 0;
6348 li->li_tv.vval.v_dict = dict;
6349 list_append(list, li);
6350 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006351 return OK;
6352}
6353
6354/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006355 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006356 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006357 * Returns FAIL when out of memory.
6358 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006359 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006360list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006361 list_T *l;
6362 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006363 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006364{
6365 listitem_T *li = listitem_alloc();
6366
6367 if (li == NULL)
6368 return FAIL;
6369 list_append(l, li);
6370 li->li_tv.v_type = VAR_STRING;
6371 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006372 if (str == NULL)
6373 li->li_tv.vval.v_string = NULL;
6374 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006375 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006376 return FAIL;
6377 return OK;
6378}
6379
6380/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006381 * Append "n" to list "l".
6382 * Returns FAIL when out of memory.
6383 */
6384 static int
6385list_append_number(l, n)
6386 list_T *l;
6387 varnumber_T n;
6388{
6389 listitem_T *li;
6390
6391 li = listitem_alloc();
6392 if (li == NULL)
6393 return FAIL;
6394 li->li_tv.v_type = VAR_NUMBER;
6395 li->li_tv.v_lock = 0;
6396 li->li_tv.vval.v_number = n;
6397 list_append(l, li);
6398 return OK;
6399}
6400
6401/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006402 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006403 * If "item" is NULL append at the end.
6404 * Return FAIL when out of memory.
6405 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006406 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006407list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006408 list_T *l;
6409 typval_T *tv;
6410 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006411{
Bram Moolenaar33570922005-01-25 22:26:29 +00006412 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006413
6414 if (ni == NULL)
6415 return FAIL;
6416 copy_tv(tv, &ni->li_tv);
6417 if (item == NULL)
6418 /* Append new item at end of list. */
6419 list_append(l, ni);
6420 else
6421 {
6422 /* Insert new item before existing item. */
6423 ni->li_prev = item->li_prev;
6424 ni->li_next = item;
6425 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006426 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006427 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006428 ++l->lv_idx;
6429 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006430 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006431 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006432 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006433 l->lv_idx_item = NULL;
6434 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006435 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006436 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006437 }
6438 return OK;
6439}
6440
6441/*
6442 * Extend "l1" with "l2".
6443 * If "bef" is NULL append at the end, otherwise insert before this item.
6444 * Returns FAIL when out of memory.
6445 */
6446 static int
6447list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006448 list_T *l1;
6449 list_T *l2;
6450 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006451{
Bram Moolenaar33570922005-01-25 22:26:29 +00006452 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006453 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006454
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006455 /* We also quit the loop when we have inserted the original item count of
6456 * the list, avoid a hang when we extend a list with itself. */
6457 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006458 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6459 return FAIL;
6460 return OK;
6461}
6462
6463/*
6464 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6465 * Return FAIL when out of memory.
6466 */
6467 static int
6468list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006469 list_T *l1;
6470 list_T *l2;
6471 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006472{
Bram Moolenaar33570922005-01-25 22:26:29 +00006473 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006474
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006475 if (l1 == NULL || l2 == NULL)
6476 return FAIL;
6477
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006478 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006479 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006480 if (l == NULL)
6481 return FAIL;
6482 tv->v_type = VAR_LIST;
6483 tv->vval.v_list = l;
6484
6485 /* append all items from the second list */
6486 return list_extend(l, l2, NULL);
6487}
6488
6489/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006490 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006491 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006492 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006493 * Returns NULL when out of memory.
6494 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006495 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006496list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006497 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006498 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006499 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006500{
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 list_T *copy;
6502 listitem_T *item;
6503 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006504
6505 if (orig == NULL)
6506 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006507
6508 copy = list_alloc();
6509 if (copy != NULL)
6510 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006511 if (copyID != 0)
6512 {
6513 /* Do this before adding the items, because one of the items may
6514 * refer back to this list. */
6515 orig->lv_copyID = copyID;
6516 orig->lv_copylist = copy;
6517 }
6518 for (item = orig->lv_first; item != NULL && !got_int;
6519 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006520 {
6521 ni = listitem_alloc();
6522 if (ni == NULL)
6523 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006524 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006525 {
6526 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6527 {
6528 vim_free(ni);
6529 break;
6530 }
6531 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006532 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006533 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006534 list_append(copy, ni);
6535 }
6536 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006537 if (item != NULL)
6538 {
6539 list_unref(copy);
6540 copy = NULL;
6541 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006542 }
6543
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006544 return copy;
6545}
6546
6547/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006549 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006550 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006551 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006552list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006553 list_T *l;
6554 listitem_T *item;
6555 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006556{
Bram Moolenaar33570922005-01-25 22:26:29 +00006557 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006558
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559 /* notify watchers */
6560 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006561 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006562 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 list_fix_watch(l, ip);
6564 if (ip == item2)
6565 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006566 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567
6568 if (item2->li_next == NULL)
6569 l->lv_last = item->li_prev;
6570 else
6571 item2->li_next->li_prev = item->li_prev;
6572 if (item->li_prev == NULL)
6573 l->lv_first = item2->li_next;
6574 else
6575 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006576 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006577}
6578
6579/*
6580 * Return an allocated string with the string representation of a list.
6581 * May return NULL.
6582 */
6583 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006584list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006585 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006586 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587{
6588 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589
6590 if (tv->vval.v_list == NULL)
6591 return NULL;
6592 ga_init2(&ga, (int)sizeof(char), 80);
6593 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006594 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006595 {
6596 vim_free(ga.ga_data);
6597 return NULL;
6598 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599 ga_append(&ga, ']');
6600 ga_append(&ga, NUL);
6601 return (char_u *)ga.ga_data;
6602}
6603
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006604typedef struct join_S {
6605 char_u *s;
6606 char_u *tofree;
6607} join_T;
6608
6609 static int
6610list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6611 garray_T *gap; /* to store the result in */
6612 list_T *l;
6613 char_u *sep;
6614 int echo_style;
6615 int copyID;
6616 garray_T *join_gap; /* to keep each list item string */
6617{
6618 int i;
6619 join_T *p;
6620 int len;
6621 int sumlen = 0;
6622 int first = TRUE;
6623 char_u *tofree;
6624 char_u numbuf[NUMBUFLEN];
6625 listitem_T *item;
6626 char_u *s;
6627
6628 /* Stringify each item in the list. */
6629 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6630 {
6631 if (echo_style)
6632 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6633 else
6634 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6635 if (s == NULL)
6636 return FAIL;
6637
6638 len = (int)STRLEN(s);
6639 sumlen += len;
6640
6641 ga_grow(join_gap, 1);
6642 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6643 if (tofree != NULL || s != numbuf)
6644 {
6645 p->s = s;
6646 p->tofree = tofree;
6647 }
6648 else
6649 {
6650 p->s = vim_strnsave(s, len);
6651 p->tofree = p->s;
6652 }
6653
6654 line_breakcheck();
6655 }
6656
6657 /* Allocate result buffer with its total size, avoid re-allocation and
6658 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6659 if (join_gap->ga_len >= 2)
6660 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6661 if (ga_grow(gap, sumlen + 2) == FAIL)
6662 return FAIL;
6663
6664 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6665 {
6666 if (first)
6667 first = FALSE;
6668 else
6669 ga_concat(gap, sep);
6670 p = ((join_T *)join_gap->ga_data) + i;
6671
6672 if (p->s != NULL)
6673 ga_concat(gap, p->s);
6674 line_breakcheck();
6675 }
6676
6677 return OK;
6678}
6679
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006681 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006682 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006683 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006684 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006685 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006686list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006687 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006688 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006689 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006690 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006691 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006692{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006693 garray_T join_ga;
6694 int retval;
6695 join_T *p;
6696 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006697
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006698 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6699 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6700
6701 /* Dispose each item in join_ga. */
6702 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006703 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006704 p = (join_T *)join_ga.ga_data;
6705 for (i = 0; i < join_ga.ga_len; ++i)
6706 {
6707 vim_free(p->tofree);
6708 ++p;
6709 }
6710 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006711 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006712
6713 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006714}
6715
6716/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006717 * Garbage collection for lists and dictionaries.
6718 *
6719 * We use reference counts to be able to free most items right away when they
6720 * are no longer used. But for composite items it's possible that it becomes
6721 * unused while the reference count is > 0: When there is a recursive
6722 * reference. Example:
6723 * :let l = [1, 2, 3]
6724 * :let d = {9: l}
6725 * :let l[1] = d
6726 *
6727 * Since this is quite unusual we handle this with garbage collection: every
6728 * once in a while find out which lists and dicts are not referenced from any
6729 * variable.
6730 *
6731 * Here is a good reference text about garbage collection (refers to Python
6732 * but it applies to all reference-counting mechanisms):
6733 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006734 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006735
6736/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006737 * Do garbage collection for lists and dicts.
6738 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006739 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006740 int
6741garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006742{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006743 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006744 buf_T *buf;
6745 win_T *wp;
6746 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006747 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006748 int did_free;
6749 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006750#ifdef FEAT_WINDOWS
6751 tabpage_T *tp;
6752#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006753
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006754 /* Only do this once. */
6755 want_garbage_collect = FALSE;
6756 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006757 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006758
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006759 /* We advance by two because we add one for items referenced through
6760 * previous_funccal. */
6761 current_copyID += COPYID_INC;
6762 copyID = current_copyID;
6763
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006764 /*
6765 * 1. Go through all accessible variables and mark all lists and dicts
6766 * with copyID.
6767 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006768
6769 /* Don't free variables in the previous_funccal list unless they are only
6770 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006771 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006772 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6773 {
6774 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6775 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6776 }
6777
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006778 /* script-local variables */
6779 for (i = 1; i <= ga_scripts.ga_len; ++i)
6780 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6781
6782 /* buffer-local variables */
6783 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006784 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006785
6786 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006787 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006788 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006789#ifdef FEAT_AUTOCMD
6790 if (aucmd_win != NULL)
6791 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6792#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006793
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006794#ifdef FEAT_WINDOWS
6795 /* tabpage-local variables */
6796 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006797 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006798#endif
6799
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006800 /* global variables */
6801 set_ref_in_ht(&globvarht, copyID);
6802
6803 /* function-local variables */
6804 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6805 {
6806 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6807 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6808 }
6809
Bram Moolenaard812df62008-11-09 12:46:09 +00006810 /* v: vars */
6811 set_ref_in_ht(&vimvarht, copyID);
6812
Bram Moolenaar1dced572012-04-05 16:54:08 +02006813#ifdef FEAT_LUA
6814 set_ref_in_lua(copyID);
6815#endif
6816
Bram Moolenaardb913952012-06-29 12:54:53 +02006817#ifdef FEAT_PYTHON
6818 set_ref_in_python(copyID);
6819#endif
6820
6821#ifdef FEAT_PYTHON3
6822 set_ref_in_python3(copyID);
6823#endif
6824
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006825 /*
6826 * 2. Free lists and dictionaries that are not referenced.
6827 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006828 did_free = free_unref_items(copyID);
6829
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006830 /*
6831 * 3. Check if any funccal can be freed now.
6832 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006833 for (pfc = &previous_funccal; *pfc != NULL; )
6834 {
6835 if (can_free_funccal(*pfc, copyID))
6836 {
6837 fc = *pfc;
6838 *pfc = fc->caller;
6839 free_funccal(fc, TRUE);
6840 did_free = TRUE;
6841 did_free_funccal = TRUE;
6842 }
6843 else
6844 pfc = &(*pfc)->caller;
6845 }
6846 if (did_free_funccal)
6847 /* When a funccal was freed some more items might be garbage
6848 * collected, so run again. */
6849 (void)garbage_collect();
6850
6851 return did_free;
6852}
6853
6854/*
6855 * Free lists and dictionaries that are no longer referenced.
6856 */
6857 static int
6858free_unref_items(copyID)
6859 int copyID;
6860{
6861 dict_T *dd;
6862 list_T *ll;
6863 int did_free = FALSE;
6864
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 */
6868 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006870 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006871 /* Free the Dictionary and ordinary items it contains, but don't
6872 * recurse into Lists and Dictionaries, they will be in the list
6873 * of dicts or list of lists. */
6874 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875 did_free = TRUE;
6876
6877 /* restart, next dict may also have been freed */
6878 dd = first_dict;
6879 }
6880 else
6881 dd = dd->dv_used_next;
6882
6883 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006884 * Go through the list of lists and free items without the copyID.
6885 * But don't free a list that has a watcher (used in a for loop), these
6886 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006887 */
6888 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6890 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006892 /* Free the List and ordinary items it contains, but don't recurse
6893 * into Lists and Dictionaries, they will be in the list of dicts
6894 * or list of lists. */
6895 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006896 did_free = TRUE;
6897
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006898 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899 ll = first_list;
6900 }
6901 else
6902 ll = ll->lv_used_next;
6903
6904 return did_free;
6905}
6906
6907/*
6908 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6909 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006910 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911set_ref_in_ht(ht, copyID)
6912 hashtab_T *ht;
6913 int copyID;
6914{
6915 int todo;
6916 hashitem_T *hi;
6917
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006918 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 for (hi = ht->ht_array; todo > 0; ++hi)
6920 if (!HASHITEM_EMPTY(hi))
6921 {
6922 --todo;
6923 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6924 }
6925}
6926
6927/*
6928 * Mark all lists and dicts referenced through list "l" with "copyID".
6929 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006930 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006931set_ref_in_list(l, copyID)
6932 list_T *l;
6933 int copyID;
6934{
6935 listitem_T *li;
6936
6937 for (li = l->lv_first; li != NULL; li = li->li_next)
6938 set_ref_in_item(&li->li_tv, copyID);
6939}
6940
6941/*
6942 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6943 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006944 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945set_ref_in_item(tv, copyID)
6946 typval_T *tv;
6947 int copyID;
6948{
6949 dict_T *dd;
6950 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006951
6952 switch (tv->v_type)
6953 {
6954 case VAR_DICT:
6955 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006956 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006957 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006958 /* Didn't see this dict yet. */
6959 dd->dv_copyID = copyID;
6960 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006961 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006962 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006963
6964 case VAR_LIST:
6965 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006966 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006967 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006968 /* Didn't see this list yet. */
6969 ll->lv_copyID = copyID;
6970 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006971 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006972 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006973 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006974 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006975}
6976
6977/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006978 * Allocate an empty header for a dictionary.
6979 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006980 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006981dict_alloc()
6982{
Bram Moolenaar33570922005-01-25 22:26:29 +00006983 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006984
Bram Moolenaar33570922005-01-25 22:26:29 +00006985 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006986 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006987 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006988 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006989 if (first_dict != NULL)
6990 first_dict->dv_used_prev = d;
6991 d->dv_used_next = first_dict;
6992 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006993 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994
Bram Moolenaar33570922005-01-25 22:26:29 +00006995 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006996 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006997 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006998 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006999 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007000 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007001 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007002}
7003
7004/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007005 * Allocate an empty dict for a return value.
7006 * Returns OK or FAIL.
7007 */
7008 static int
7009rettv_dict_alloc(rettv)
7010 typval_T *rettv;
7011{
7012 dict_T *d = dict_alloc();
7013
7014 if (d == NULL)
7015 return FAIL;
7016
7017 rettv->vval.v_dict = d;
7018 rettv->v_type = VAR_DICT;
7019 ++d->dv_refcount;
7020 return OK;
7021}
7022
7023
7024/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007025 * Unreference a Dictionary: decrement the reference count and free it when it
7026 * becomes zero.
7027 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007028 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007029dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007030 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007031{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007032 if (d != NULL && --d->dv_refcount <= 0)
7033 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007034}
7035
7036/*
7037 * Free a Dictionary, including all items it contains.
7038 * Ignores the reference count.
7039 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007040 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007041dict_free(d, recurse)
7042 dict_T *d;
7043 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007044{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007045 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007046 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007047 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007048
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007049 /* Remove the dict from the list of dicts for garbage collection. */
7050 if (d->dv_used_prev == NULL)
7051 first_dict = d->dv_used_next;
7052 else
7053 d->dv_used_prev->dv_used_next = d->dv_used_next;
7054 if (d->dv_used_next != NULL)
7055 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7056
7057 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007058 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007059 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007060 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007061 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007062 if (!HASHITEM_EMPTY(hi))
7063 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007064 /* Remove the item before deleting it, just in case there is
7065 * something recursive causing trouble. */
7066 di = HI2DI(hi);
7067 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007068 if (recurse || (di->di_tv.v_type != VAR_LIST
7069 && di->di_tv.v_type != VAR_DICT))
7070 clear_tv(&di->di_tv);
7071 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007072 --todo;
7073 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007074 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007075 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007076 vim_free(d);
7077}
7078
7079/*
7080 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007081 * The "key" is copied to the new item.
7082 * Note that the value of the item "di_tv" still needs to be initialized!
7083 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007084 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007085 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007086dictitem_alloc(key)
7087 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007088{
Bram Moolenaar33570922005-01-25 22:26:29 +00007089 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007090
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007091 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007092 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007093 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007094 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007095 di->di_flags = 0;
7096 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007097 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007098}
7099
7100/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007101 * Make a copy of a Dictionary item.
7102 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007103 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007104dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007105 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007106{
Bram Moolenaar33570922005-01-25 22:26:29 +00007107 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007108
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007109 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7110 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007111 if (di != NULL)
7112 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007113 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007114 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007115 copy_tv(&org->di_tv, &di->di_tv);
7116 }
7117 return di;
7118}
7119
7120/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007121 * Remove item "item" from Dictionary "dict" and free it.
7122 */
7123 static void
7124dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007125 dict_T *dict;
7126 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007127{
Bram Moolenaar33570922005-01-25 22:26:29 +00007128 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007129
Bram Moolenaar33570922005-01-25 22:26:29 +00007130 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007131 if (HASHITEM_EMPTY(hi))
7132 EMSG2(_(e_intern2), "dictitem_remove()");
7133 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007134 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007135 dictitem_free(item);
7136}
7137
7138/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007139 * Free a dict item. Also clears the value.
7140 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007141 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007142dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007143 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007144{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007145 clear_tv(&item->di_tv);
7146 vim_free(item);
7147}
7148
7149/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007150 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7151 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007152 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007153 * Returns NULL when out of memory.
7154 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007155 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007156dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007157 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007158 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007159 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007160{
Bram Moolenaar33570922005-01-25 22:26:29 +00007161 dict_T *copy;
7162 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007163 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007164 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007165
7166 if (orig == NULL)
7167 return NULL;
7168
7169 copy = dict_alloc();
7170 if (copy != NULL)
7171 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007172 if (copyID != 0)
7173 {
7174 orig->dv_copyID = copyID;
7175 orig->dv_copydict = copy;
7176 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007177 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007178 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007179 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007180 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007181 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182 --todo;
7183
7184 di = dictitem_alloc(hi->hi_key);
7185 if (di == NULL)
7186 break;
7187 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007188 {
7189 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7190 copyID) == FAIL)
7191 {
7192 vim_free(di);
7193 break;
7194 }
7195 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007196 else
7197 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7198 if (dict_add(copy, di) == FAIL)
7199 {
7200 dictitem_free(di);
7201 break;
7202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007204 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007205
Bram Moolenaare9a41262005-01-15 22:18:47 +00007206 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007207 if (todo > 0)
7208 {
7209 dict_unref(copy);
7210 copy = NULL;
7211 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007212 }
7213
7214 return copy;
7215}
7216
7217/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007218 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007219 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007220 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007221 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 dict_T *d;
7224 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225{
Bram Moolenaar33570922005-01-25 22:26:29 +00007226 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227}
7228
Bram Moolenaar8c711452005-01-14 21:53:12 +00007229/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007230 * Add a number or string entry to dictionary "d".
7231 * When "str" is NULL use number "nr", otherwise use "str".
7232 * Returns FAIL when out of memory and when key already exists.
7233 */
7234 int
7235dict_add_nr_str(d, key, nr, str)
7236 dict_T *d;
7237 char *key;
7238 long nr;
7239 char_u *str;
7240{
7241 dictitem_T *item;
7242
7243 item = dictitem_alloc((char_u *)key);
7244 if (item == NULL)
7245 return FAIL;
7246 item->di_tv.v_lock = 0;
7247 if (str == NULL)
7248 {
7249 item->di_tv.v_type = VAR_NUMBER;
7250 item->di_tv.vval.v_number = nr;
7251 }
7252 else
7253 {
7254 item->di_tv.v_type = VAR_STRING;
7255 item->di_tv.vval.v_string = vim_strsave(str);
7256 }
7257 if (dict_add(d, item) == FAIL)
7258 {
7259 dictitem_free(item);
7260 return FAIL;
7261 }
7262 return OK;
7263}
7264
7265/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007266 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007267 * Returns FAIL when out of memory and when key already exists.
7268 */
7269 int
7270dict_add_list(d, key, list)
7271 dict_T *d;
7272 char *key;
7273 list_T *list;
7274{
7275 dictitem_T *item;
7276
7277 item = dictitem_alloc((char_u *)key);
7278 if (item == NULL)
7279 return FAIL;
7280 item->di_tv.v_lock = 0;
7281 item->di_tv.v_type = VAR_LIST;
7282 item->di_tv.vval.v_list = list;
7283 if (dict_add(d, item) == FAIL)
7284 {
7285 dictitem_free(item);
7286 return FAIL;
7287 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007288 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007289 return OK;
7290}
7291
7292/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007293 * Get the number of items in a Dictionary.
7294 */
7295 static long
7296dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007297 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007299 if (d == NULL)
7300 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007301 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302}
7303
7304/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007305 * Find item "key[len]" in Dictionary "d".
7306 * If "len" is negative use strlen(key).
7307 * Returns NULL when not found.
7308 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007309 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007310dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007311 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312 char_u *key;
7313 int len;
7314{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315#define AKEYLEN 200
7316 char_u buf[AKEYLEN];
7317 char_u *akey;
7318 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007319 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007320
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 if (len < 0)
7322 akey = key;
7323 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007324 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325 tofree = akey = vim_strnsave(key, len);
7326 if (akey == NULL)
7327 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007328 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007329 else
7330 {
7331 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007332 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333 akey = buf;
7334 }
7335
Bram Moolenaar33570922005-01-25 22:26:29 +00007336 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007337 vim_free(tofree);
7338 if (HASHITEM_EMPTY(hi))
7339 return NULL;
7340 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007341}
7342
7343/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007344 * Get a string item from a dictionary.
7345 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007346 * Returns NULL if the entry doesn't exist or out of memory.
7347 */
7348 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007349get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007350 dict_T *d;
7351 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007352 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007353{
7354 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007355 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007356
7357 di = dict_find(d, key, -1);
7358 if (di == NULL)
7359 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007360 s = get_tv_string(&di->di_tv);
7361 if (save && s != NULL)
7362 s = vim_strsave(s);
7363 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007364}
7365
7366/*
7367 * Get a number item from a dictionary.
7368 * Returns 0 if the entry doesn't exist or out of memory.
7369 */
7370 long
7371get_dict_number(d, key)
7372 dict_T *d;
7373 char_u *key;
7374{
7375 dictitem_T *di;
7376
7377 di = dict_find(d, key, -1);
7378 if (di == NULL)
7379 return 0;
7380 return get_tv_number(&di->di_tv);
7381}
7382
7383/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007384 * Return an allocated string with the string representation of a Dictionary.
7385 * May return NULL.
7386 */
7387 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007388dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007389 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007390 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391{
7392 garray_T ga;
7393 int first = TRUE;
7394 char_u *tofree;
7395 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007396 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007397 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007398 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007399 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007400
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007402 return NULL;
7403 ga_init2(&ga, (int)sizeof(char), 80);
7404 ga_append(&ga, '{');
7405
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007406 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007407 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007409 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007411 --todo;
7412
7413 if (first)
7414 first = FALSE;
7415 else
7416 ga_concat(&ga, (char_u *)", ");
7417
7418 tofree = string_quote(hi->hi_key, FALSE);
7419 if (tofree != NULL)
7420 {
7421 ga_concat(&ga, tofree);
7422 vim_free(tofree);
7423 }
7424 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007425 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007426 if (s != NULL)
7427 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007428 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007429 if (s == NULL)
7430 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007432 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007433 if (todo > 0)
7434 {
7435 vim_free(ga.ga_data);
7436 return NULL;
7437 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007438
7439 ga_append(&ga, '}');
7440 ga_append(&ga, NUL);
7441 return (char_u *)ga.ga_data;
7442}
7443
7444/*
7445 * Allocate a variable for a Dictionary and fill it from "*arg".
7446 * Return OK or FAIL. Returns NOTDONE for {expr}.
7447 */
7448 static int
7449get_dict_tv(arg, rettv, evaluate)
7450 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007451 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007452 int evaluate;
7453{
Bram Moolenaar33570922005-01-25 22:26:29 +00007454 dict_T *d = NULL;
7455 typval_T tvkey;
7456 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007457 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007458 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007459 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007460 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007461
7462 /*
7463 * First check if it's not a curly-braces thing: {expr}.
7464 * Must do this without evaluating, otherwise a function may be called
7465 * twice. Unfortunately this means we need to call eval1() twice for the
7466 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007467 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007469 if (*start != '}')
7470 {
7471 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7472 return FAIL;
7473 if (*start == '}')
7474 return NOTDONE;
7475 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007476
7477 if (evaluate)
7478 {
7479 d = dict_alloc();
7480 if (d == NULL)
7481 return FAIL;
7482 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007483 tvkey.v_type = VAR_UNKNOWN;
7484 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007485
7486 *arg = skipwhite(*arg + 1);
7487 while (**arg != '}' && **arg != NUL)
7488 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007489 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 goto failret;
7491 if (**arg != ':')
7492 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007493 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007494 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495 goto failret;
7496 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007497 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007498 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007499 key = get_tv_string_buf_chk(&tvkey, buf);
7500 if (key == NULL || *key == NUL)
7501 {
7502 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7503 if (key != NULL)
7504 EMSG(_(e_emptykey));
7505 clear_tv(&tvkey);
7506 goto failret;
7507 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007509
7510 *arg = skipwhite(*arg + 1);
7511 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7512 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007513 if (evaluate)
7514 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007515 goto failret;
7516 }
7517 if (evaluate)
7518 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007520 if (item != NULL)
7521 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007522 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007524 clear_tv(&tv);
7525 goto failret;
7526 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007527 item = dictitem_alloc(key);
7528 clear_tv(&tvkey);
7529 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007531 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007532 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007533 if (dict_add(d, item) == FAIL)
7534 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007535 }
7536 }
7537
7538 if (**arg == '}')
7539 break;
7540 if (**arg != ',')
7541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007542 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007543 goto failret;
7544 }
7545 *arg = skipwhite(*arg + 1);
7546 }
7547
7548 if (**arg != '}')
7549 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007550 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551failret:
7552 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007553 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007554 return FAIL;
7555 }
7556
7557 *arg = skipwhite(*arg + 1);
7558 if (evaluate)
7559 {
7560 rettv->v_type = VAR_DICT;
7561 rettv->vval.v_dict = d;
7562 ++d->dv_refcount;
7563 }
7564
7565 return OK;
7566}
7567
Bram Moolenaar8c711452005-01-14 21:53:12 +00007568/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007569 * Return a string with the string representation of a variable.
7570 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007571 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007572 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007573 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007574 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007575 */
7576 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007577echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007578 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007579 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007580 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007581 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007582{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007583 static int recurse = 0;
7584 char_u *r = NULL;
7585
Bram Moolenaar33570922005-01-25 22:26:29 +00007586 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007587 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007588 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007589 *tofree = NULL;
7590 return NULL;
7591 }
7592 ++recurse;
7593
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007594 switch (tv->v_type)
7595 {
7596 case VAR_FUNC:
7597 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007598 r = tv->vval.v_string;
7599 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007600
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007601 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007602 if (tv->vval.v_list == NULL)
7603 {
7604 *tofree = NULL;
7605 r = NULL;
7606 }
7607 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7608 {
7609 *tofree = NULL;
7610 r = (char_u *)"[...]";
7611 }
7612 else
7613 {
7614 tv->vval.v_list->lv_copyID = copyID;
7615 *tofree = list2string(tv, copyID);
7616 r = *tofree;
7617 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007618 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007619
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007621 if (tv->vval.v_dict == NULL)
7622 {
7623 *tofree = NULL;
7624 r = NULL;
7625 }
7626 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7627 {
7628 *tofree = NULL;
7629 r = (char_u *)"{...}";
7630 }
7631 else
7632 {
7633 tv->vval.v_dict->dv_copyID = copyID;
7634 *tofree = dict2string(tv, copyID);
7635 r = *tofree;
7636 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007637 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007638
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007639 case VAR_STRING:
7640 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007641 *tofree = NULL;
7642 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007643 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007644
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007645#ifdef FEAT_FLOAT
7646 case VAR_FLOAT:
7647 *tofree = NULL;
7648 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7649 r = numbuf;
7650 break;
7651#endif
7652
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007653 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007654 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007655 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007656 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007657
7658 --recurse;
7659 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007660}
7661
7662/*
7663 * Return a string with the string representation of a variable.
7664 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7665 * "numbuf" is used for a number.
7666 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007667 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007668 */
7669 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007670tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007671 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007672 char_u **tofree;
7673 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007674 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007675{
7676 switch (tv->v_type)
7677 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007678 case VAR_FUNC:
7679 *tofree = string_quote(tv->vval.v_string, TRUE);
7680 return *tofree;
7681 case VAR_STRING:
7682 *tofree = string_quote(tv->vval.v_string, FALSE);
7683 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007684#ifdef FEAT_FLOAT
7685 case VAR_FLOAT:
7686 *tofree = NULL;
7687 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7688 return numbuf;
7689#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007690 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007691 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007693 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007694 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007695 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007696 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007697 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007698}
7699
7700/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 * Return string "str" in ' quotes, doubling ' characters.
7702 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007704 */
7705 static char_u *
7706string_quote(str, function)
7707 char_u *str;
7708 int function;
7709{
Bram Moolenaar33570922005-01-25 22:26:29 +00007710 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007711 char_u *p, *r, *s;
7712
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 len = (function ? 13 : 3);
7714 if (str != NULL)
7715 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007716 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007717 for (p = str; *p != NUL; mb_ptr_adv(p))
7718 if (*p == '\'')
7719 ++len;
7720 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007721 s = r = alloc(len);
7722 if (r != NULL)
7723 {
7724 if (function)
7725 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007727 r += 10;
7728 }
7729 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007730 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007731 if (str != NULL)
7732 for (p = str; *p != NUL; )
7733 {
7734 if (*p == '\'')
7735 *r++ = '\'';
7736 MB_COPY_CHAR(p, r);
7737 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007738 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007739 if (function)
7740 *r++ = ')';
7741 *r++ = NUL;
7742 }
7743 return s;
7744}
7745
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007746#ifdef FEAT_FLOAT
7747/*
7748 * Convert the string "text" to a floating point number.
7749 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7750 * this always uses a decimal point.
7751 * Returns the length of the text that was consumed.
7752 */
7753 static int
7754string2float(text, value)
7755 char_u *text;
7756 float_T *value; /* result stored here */
7757{
7758 char *s = (char *)text;
7759 float_T f;
7760
7761 f = strtod(s, &s);
7762 *value = f;
7763 return (int)((char_u *)s - text);
7764}
7765#endif
7766
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007767/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007768 * Get the value of an environment variable.
7769 * "arg" is pointing to the '$'. It is advanced to after the name.
7770 * If the environment variable was not set, silently assume it is empty.
7771 * Always return OK.
7772 */
7773 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007774get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007776 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 int evaluate;
7778{
7779 char_u *string = NULL;
7780 int len;
7781 int cc;
7782 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007783 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784
7785 ++*arg;
7786 name = *arg;
7787 len = get_env_len(arg);
7788 if (evaluate)
7789 {
7790 if (len != 0)
7791 {
7792 cc = name[len];
7793 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007794 /* first try vim_getenv(), fast for normal environment vars */
7795 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007797 {
7798 if (!mustfree)
7799 string = vim_strsave(string);
7800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 else
7802 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007803 if (mustfree)
7804 vim_free(string);
7805
Bram Moolenaar071d4272004-06-13 20:20:40 +00007806 /* next try expanding things like $VIM and ${HOME} */
7807 string = expand_env_save(name - 1);
7808 if (string != NULL && *string == '$')
7809 {
7810 vim_free(string);
7811 string = NULL;
7812 }
7813 }
7814 name[len] = cc;
7815 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007816 rettv->v_type = VAR_STRING;
7817 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 }
7819
7820 return OK;
7821}
7822
7823/*
7824 * Array with names and number of arguments of all internal functions
7825 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7826 */
7827static struct fst
7828{
7829 char *f_name; /* function name */
7830 char f_min_argc; /* minimal number of arguments */
7831 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007832 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007833 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834} functions[] =
7835{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007836#ifdef FEAT_FLOAT
7837 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007838 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007839#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007840 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007841 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 {"append", 2, 2, f_append},
7843 {"argc", 0, 0, f_argc},
7844 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007845 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007846#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007847 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007848 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007849 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007852 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 {"bufexists", 1, 1, f_bufexists},
7854 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7855 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7856 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7857 {"buflisted", 1, 1, f_buflisted},
7858 {"bufloaded", 1, 1, f_bufloaded},
7859 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007860 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 {"bufwinnr", 1, 1, f_bufwinnr},
7862 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007863 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007865#ifdef FEAT_FLOAT
7866 {"ceil", 1, 1, f_ceil},
7867#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007868 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007869 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007871 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007872 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007873#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007874 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007875 {"complete_add", 1, 1, f_complete_add},
7876 {"complete_check", 0, 0, f_complete_check},
7877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007879 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007880#ifdef FEAT_FLOAT
7881 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007882 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007883#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007884 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007886 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007887 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 {"delete", 1, 1, f_delete},
7889 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007890 {"diff_filler", 1, 1, f_diff_filler},
7891 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007892 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007894 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {"eventhandler", 0, 0, f_eventhandler},
7896 {"executable", 1, 1, f_executable},
7897 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007898#ifdef FEAT_FLOAT
7899 {"exp", 1, 1, f_exp},
7900#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007901 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007902 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007903 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7905 {"filereadable", 1, 1, f_filereadable},
7906 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007907 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007908 {"finddir", 1, 3, f_finddir},
7909 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007910#ifdef FEAT_FLOAT
7911 {"float2nr", 1, 1, f_float2nr},
7912 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007913 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007914#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007915 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {"fnamemodify", 2, 2, f_fnamemodify},
7917 {"foldclosed", 1, 1, f_foldclosed},
7918 {"foldclosedend", 1, 1, f_foldclosedend},
7919 {"foldlevel", 1, 1, f_foldlevel},
7920 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007921 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007923 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007924 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007925 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007926 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007927 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 {"getchar", 0, 1, f_getchar},
7929 {"getcharmod", 0, 0, f_getcharmod},
7930 {"getcmdline", 0, 0, f_getcmdline},
7931 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007932 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007934 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007935 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007936 {"getfsize", 1, 1, f_getfsize},
7937 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007938 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007939 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007940 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007941 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007942 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007943 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007944 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007945 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007947 {"gettabvar", 2, 3, f_gettabvar},
7948 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 {"getwinposx", 0, 0, f_getwinposx},
7950 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007951 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007952 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007953 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007954 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007955 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007956 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007957 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7959 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7960 {"histadd", 2, 2, f_histadd},
7961 {"histdel", 1, 2, f_histdel},
7962 {"histget", 1, 2, f_histget},
7963 {"histnr", 1, 1, f_histnr},
7964 {"hlID", 1, 1, f_hlID},
7965 {"hlexists", 1, 1, f_hlexists},
7966 {"hostname", 0, 0, f_hostname},
7967 {"iconv", 3, 3, f_iconv},
7968 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007969 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007970 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007971 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007972 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 {"inputrestore", 0, 0, f_inputrestore},
7974 {"inputsave", 0, 0, f_inputsave},
7975 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007976 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007977 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007978 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007979 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007980 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007981 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007982 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007984 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007985 {"libcall", 3, 3, f_libcall},
7986 {"libcallnr", 3, 3, f_libcallnr},
7987 {"line", 1, 1, f_line},
7988 {"line2byte", 1, 1, f_line2byte},
7989 {"lispindent", 1, 1, f_lispindent},
7990 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007991#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007992 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007993 {"log10", 1, 1, f_log10},
7994#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007995#ifdef FEAT_LUA
7996 {"luaeval", 1, 2, f_luaeval},
7997#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007998 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007999 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008000 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008001 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008002 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008003 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008004 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008005 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008006 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008007 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008008 {"max", 1, 1, f_max},
8009 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008010#ifdef vim_mkdir
8011 {"mkdir", 1, 3, f_mkdir},
8012#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008013 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008014#ifdef FEAT_MZSCHEME
8015 {"mzeval", 1, 1, f_mzeval},
8016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008018 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008019 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008020 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008021#ifdef FEAT_FLOAT
8022 {"pow", 2, 2, f_pow},
8023#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008025 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008026 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008027#ifdef FEAT_PYTHON3
8028 {"py3eval", 1, 1, f_py3eval},
8029#endif
8030#ifdef FEAT_PYTHON
8031 {"pyeval", 1, 1, f_pyeval},
8032#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008033 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008034 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008035 {"reltime", 0, 2, f_reltime},
8036 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 {"remote_expr", 2, 3, f_remote_expr},
8038 {"remote_foreground", 1, 1, f_remote_foreground},
8039 {"remote_peek", 1, 2, f_remote_peek},
8040 {"remote_read", 1, 1, f_remote_read},
8041 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008042 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008044 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008046 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008047#ifdef FEAT_FLOAT
8048 {"round", 1, 1, f_round},
8049#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008050 {"screenattr", 2, 2, f_screenattr},
8051 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008052 {"screencol", 0, 0, f_screencol},
8053 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008054 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008055 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008056 {"searchpair", 3, 7, f_searchpair},
8057 {"searchpairpos", 3, 7, f_searchpairpos},
8058 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 {"server2client", 2, 2, f_server2client},
8060 {"serverlist", 0, 0, f_serverlist},
8061 {"setbufvar", 3, 3, f_setbufvar},
8062 {"setcmdpos", 1, 1, f_setcmdpos},
8063 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008064 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008065 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008066 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008067 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008068 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008069 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008070 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008071 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008072#ifdef FEAT_CRYPT
8073 {"sha256", 1, 1, f_sha256},
8074#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008075 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008076 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008077 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078#ifdef FEAT_FLOAT
8079 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008080 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008081#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008082 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008083 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008084 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008085 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008086 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008087#ifdef FEAT_FLOAT
8088 {"sqrt", 1, 1, f_sqrt},
8089 {"str2float", 1, 1, f_str2float},
8090#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008091 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008092 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008093 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094#ifdef HAVE_STRFTIME
8095 {"strftime", 1, 2, f_strftime},
8096#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008097 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008098 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"strlen", 1, 1, f_strlen},
8100 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008101 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008103 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 {"submatch", 1, 1, f_submatch},
8105 {"substitute", 4, 4, f_substitute},
8106 {"synID", 3, 3, f_synID},
8107 {"synIDattr", 2, 3, f_synIDattr},
8108 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008109 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008110 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008111 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008112 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008113 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008114 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008115 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008116 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008117#ifdef FEAT_FLOAT
8118 {"tan", 1, 1, f_tan},
8119 {"tanh", 1, 1, f_tanh},
8120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008122 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 {"tolower", 1, 1, f_tolower},
8124 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008125 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008126#ifdef FEAT_FLOAT
8127 {"trunc", 1, 1, f_trunc},
8128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008130 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008131 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008132 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008133 {"virtcol", 1, 1, f_virtcol},
8134 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008135 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 {"winbufnr", 1, 1, f_winbufnr},
8137 {"wincol", 0, 0, f_wincol},
8138 {"winheight", 1, 1, f_winheight},
8139 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008140 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008142 {"winrestview", 1, 1, f_winrestview},
8143 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008145 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008146 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147};
8148
8149#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8150
8151/*
8152 * Function given to ExpandGeneric() to obtain the list of internal
8153 * or user defined function names.
8154 */
8155 char_u *
8156get_function_name(xp, idx)
8157 expand_T *xp;
8158 int idx;
8159{
8160 static int intidx = -1;
8161 char_u *name;
8162
8163 if (idx == 0)
8164 intidx = -1;
8165 if (intidx < 0)
8166 {
8167 name = get_user_func_name(xp, idx);
8168 if (name != NULL)
8169 return name;
8170 }
8171 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8172 {
8173 STRCPY(IObuff, functions[intidx].f_name);
8174 STRCAT(IObuff, "(");
8175 if (functions[intidx].f_max_argc == 0)
8176 STRCAT(IObuff, ")");
8177 return IObuff;
8178 }
8179
8180 return NULL;
8181}
8182
8183/*
8184 * Function given to ExpandGeneric() to obtain the list of internal or
8185 * user defined variable or function names.
8186 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008187 char_u *
8188get_expr_name(xp, idx)
8189 expand_T *xp;
8190 int idx;
8191{
8192 static int intidx = -1;
8193 char_u *name;
8194
8195 if (idx == 0)
8196 intidx = -1;
8197 if (intidx < 0)
8198 {
8199 name = get_function_name(xp, idx);
8200 if (name != NULL)
8201 return name;
8202 }
8203 return get_user_var_name(xp, ++intidx);
8204}
8205
8206#endif /* FEAT_CMDL_COMPL */
8207
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008208#if defined(EBCDIC) || defined(PROTO)
8209/*
8210 * Compare struct fst by function name.
8211 */
8212 static int
8213compare_func_name(s1, s2)
8214 const void *s1;
8215 const void *s2;
8216{
8217 struct fst *p1 = (struct fst *)s1;
8218 struct fst *p2 = (struct fst *)s2;
8219
8220 return STRCMP(p1->f_name, p2->f_name);
8221}
8222
8223/*
8224 * Sort the function table by function name.
8225 * The sorting of the table above is ASCII dependant.
8226 * On machines using EBCDIC we have to sort it.
8227 */
8228 static void
8229sortFunctions()
8230{
8231 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8232
8233 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8234}
8235#endif
8236
8237
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238/*
8239 * Find internal function in table above.
8240 * Return index, or -1 if not found
8241 */
8242 static int
8243find_internal_func(name)
8244 char_u *name; /* name of the function */
8245{
8246 int first = 0;
8247 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8248 int cmp;
8249 int x;
8250
8251 /*
8252 * Find the function name in the table. Binary search.
8253 */
8254 while (first <= last)
8255 {
8256 x = first + ((unsigned)(last - first) >> 1);
8257 cmp = STRCMP(name, functions[x].f_name);
8258 if (cmp < 0)
8259 last = x - 1;
8260 else if (cmp > 0)
8261 first = x + 1;
8262 else
8263 return x;
8264 }
8265 return -1;
8266}
8267
8268/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008269 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8270 * name it contains, otherwise return "name".
8271 */
8272 static char_u *
8273deref_func_name(name, lenp)
8274 char_u *name;
8275 int *lenp;
8276{
Bram Moolenaar33570922005-01-25 22:26:29 +00008277 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008278 int cc;
8279
8280 cc = name[*lenp];
8281 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008282 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008283 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008284 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008285 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008286 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008287 {
8288 *lenp = 0;
8289 return (char_u *)""; /* just in case */
8290 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008291 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008292 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008293 }
8294
8295 return name;
8296}
8297
8298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 * Allocate a variable for the result of a function.
8300 * Return OK or FAIL.
8301 */
8302 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008303get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8304 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 char_u *name; /* name of the function */
8306 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008307 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 char_u **arg; /* argument, pointing to the '(' */
8309 linenr_T firstline; /* first line of range */
8310 linenr_T lastline; /* last line of range */
8311 int *doesrange; /* return: function handled range */
8312 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008313 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314{
8315 char_u *argp;
8316 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008317 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 int argcount = 0; /* number of arguments found */
8319
8320 /*
8321 * Get the arguments.
8322 */
8323 argp = *arg;
8324 while (argcount < MAX_FUNC_ARGS)
8325 {
8326 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8327 if (*argp == ')' || *argp == ',' || *argp == NUL)
8328 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8330 {
8331 ret = FAIL;
8332 break;
8333 }
8334 ++argcount;
8335 if (*argp != ',')
8336 break;
8337 }
8338 if (*argp == ')')
8339 ++argp;
8340 else
8341 ret = FAIL;
8342
8343 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008344 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008345 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008347 {
8348 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008349 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008350 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008351 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353
8354 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008355 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356
8357 *arg = skipwhite(argp);
8358 return ret;
8359}
8360
8361
8362/*
8363 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008364 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008365 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 */
8367 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008368call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008369 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008370 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008372 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008374 typval_T *argvars; /* vars for arguments, must have "argcount"
8375 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008376 linenr_T firstline; /* first line of range */
8377 linenr_T lastline; /* last line of range */
8378 int *doesrange; /* return: function handled range */
8379 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008380 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381{
8382 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383#define ERROR_UNKNOWN 0
8384#define ERROR_TOOMANY 1
8385#define ERROR_TOOFEW 2
8386#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008387#define ERROR_DICT 4
8388#define ERROR_NONE 5
8389#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 int error = ERROR_NONE;
8391 int i;
8392 int llen;
8393 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394#define FLEN_FIXED 40
8395 char_u fname_buf[FLEN_FIXED + 1];
8396 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008397 char_u *name;
8398
8399 /* Make a copy of the name, if it comes from a funcref variable it could
8400 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008401 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008402 if (name == NULL)
8403 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404
8405 /*
8406 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8407 * Change <SNR>123_name() to K_SNR 123_name().
8408 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8409 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410 llen = eval_fname_script(name);
8411 if (llen > 0)
8412 {
8413 fname_buf[0] = K_SPECIAL;
8414 fname_buf[1] = KS_EXTRA;
8415 fname_buf[2] = (int)KE_SNR;
8416 i = 3;
8417 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8418 {
8419 if (current_SID <= 0)
8420 error = ERROR_SCRIPT;
8421 else
8422 {
8423 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8424 i = (int)STRLEN(fname_buf);
8425 }
8426 }
8427 if (i + STRLEN(name + llen) < FLEN_FIXED)
8428 {
8429 STRCPY(fname_buf + i, name + llen);
8430 fname = fname_buf;
8431 }
8432 else
8433 {
8434 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8435 if (fname == NULL)
8436 error = ERROR_OTHER;
8437 else
8438 {
8439 mch_memmove(fname, fname_buf, (size_t)i);
8440 STRCPY(fname + i, name + llen);
8441 }
8442 }
8443 }
8444 else
8445 fname = name;
8446
8447 *doesrange = FALSE;
8448
8449
8450 /* execute the function if no errors detected and executing */
8451 if (evaluate && error == ERROR_NONE)
8452 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008453 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8454 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 error = ERROR_UNKNOWN;
8456
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008457 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {
8459 /*
8460 * User defined function.
8461 */
8462 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008463
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008465 /* Trigger FuncUndefined event, may load the function. */
8466 if (fp == NULL
8467 && apply_autocmds(EVENT_FUNCUNDEFINED,
8468 fname, fname, TRUE, NULL)
8469 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008470 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008471 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472 fp = find_func(fname);
8473 }
8474#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008475 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008476 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008477 {
8478 /* loaded a package, search for the function again */
8479 fp = find_func(fname);
8480 }
8481
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 if (fp != NULL)
8483 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008484 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008485 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008486 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008488 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008490 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008491 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 else
8493 {
8494 /*
8495 * Call the user function.
8496 * Save and restore search patterns, script variables and
8497 * redo buffer.
8498 */
8499 save_search_patterns();
8500 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008501 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008502 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008503 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008504 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8505 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8506 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008507 /* Function was unreferenced while being used, free it
8508 * now. */
8509 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 restoreRedobuff();
8511 restore_search_patterns();
8512 error = ERROR_NONE;
8513 }
8514 }
8515 }
8516 else
8517 {
8518 /*
8519 * Find the function name in the table, call its implementation.
8520 */
8521 i = find_internal_func(fname);
8522 if (i >= 0)
8523 {
8524 if (argcount < functions[i].f_min_argc)
8525 error = ERROR_TOOFEW;
8526 else if (argcount > functions[i].f_max_argc)
8527 error = ERROR_TOOMANY;
8528 else
8529 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008530 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008531 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 error = ERROR_NONE;
8533 }
8534 }
8535 }
8536 /*
8537 * The function call (or "FuncUndefined" autocommand sequence) might
8538 * have been aborted by an error, an interrupt, or an explicitly thrown
8539 * exception that has not been caught so far. This situation can be
8540 * tested for by calling aborting(). For an error in an internal
8541 * function or for the "E132" error in call_user_func(), however, the
8542 * throw point at which the "force_abort" flag (temporarily reset by
8543 * emsg()) is normally updated has not been reached yet. We need to
8544 * update that flag first to make aborting() reliable.
8545 */
8546 update_force_abort();
8547 }
8548 if (error == ERROR_NONE)
8549 ret = OK;
8550
8551 /*
8552 * Report an error unless the argument evaluation or function call has been
8553 * cancelled due to an aborting error, an interrupt, or an exception.
8554 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008555 if (!aborting())
8556 {
8557 switch (error)
8558 {
8559 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008560 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008561 break;
8562 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008563 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008564 break;
8565 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008566 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008567 name);
8568 break;
8569 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008570 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008571 name);
8572 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008573 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008574 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008575 name);
8576 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008577 }
8578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 if (fname != name && fname != fname_buf)
8581 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008582 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583
8584 return ret;
8585}
8586
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008587/*
8588 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008589 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008590 */
8591 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008592emsg_funcname(ermsg, name)
8593 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008594 char_u *name;
8595{
8596 char_u *p;
8597
8598 if (*name == K_SPECIAL)
8599 p = concat_str((char_u *)"<SNR>", name + 3);
8600 else
8601 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008602 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008603 if (p != name)
8604 vim_free(p);
8605}
8606
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008607/*
8608 * Return TRUE for a non-zero Number and a non-empty String.
8609 */
8610 static int
8611non_zero_arg(argvars)
8612 typval_T *argvars;
8613{
8614 return ((argvars[0].v_type == VAR_NUMBER
8615 && argvars[0].vval.v_number != 0)
8616 || (argvars[0].v_type == VAR_STRING
8617 && argvars[0].vval.v_string != NULL
8618 && *argvars[0].vval.v_string != NUL));
8619}
8620
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621/*********************************************
8622 * Implementation of the built-in functions
8623 */
8624
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008625#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008626static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8627
8628/*
8629 * Get the float value of "argvars[0]" into "f".
8630 * Returns FAIL when the argument is not a Number or Float.
8631 */
8632 static int
8633get_float_arg(argvars, f)
8634 typval_T *argvars;
8635 float_T *f;
8636{
8637 if (argvars[0].v_type == VAR_FLOAT)
8638 {
8639 *f = argvars[0].vval.v_float;
8640 return OK;
8641 }
8642 if (argvars[0].v_type == VAR_NUMBER)
8643 {
8644 *f = (float_T)argvars[0].vval.v_number;
8645 return OK;
8646 }
8647 EMSG(_("E808: Number or Float required"));
8648 return FAIL;
8649}
8650
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008651/*
8652 * "abs(expr)" function
8653 */
8654 static void
8655f_abs(argvars, rettv)
8656 typval_T *argvars;
8657 typval_T *rettv;
8658{
8659 if (argvars[0].v_type == VAR_FLOAT)
8660 {
8661 rettv->v_type = VAR_FLOAT;
8662 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8663 }
8664 else
8665 {
8666 varnumber_T n;
8667 int error = FALSE;
8668
8669 n = get_tv_number_chk(&argvars[0], &error);
8670 if (error)
8671 rettv->vval.v_number = -1;
8672 else if (n > 0)
8673 rettv->vval.v_number = n;
8674 else
8675 rettv->vval.v_number = -n;
8676 }
8677}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008678
8679/*
8680 * "acos()" function
8681 */
8682 static void
8683f_acos(argvars, rettv)
8684 typval_T *argvars;
8685 typval_T *rettv;
8686{
8687 float_T f;
8688
8689 rettv->v_type = VAR_FLOAT;
8690 if (get_float_arg(argvars, &f) == OK)
8691 rettv->vval.v_float = acos(f);
8692 else
8693 rettv->vval.v_float = 0.0;
8694}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008695#endif
8696
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008698 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008699 */
8700 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008701f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008702 typval_T *argvars;
8703 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704{
Bram Moolenaar33570922005-01-25 22:26:29 +00008705 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008707 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008708 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008709 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008710 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008711 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008712 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008713 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008714 }
8715 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008716 EMSG(_(e_listreq));
8717}
8718
8719/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008720 * "and(expr, expr)" function
8721 */
8722 static void
8723f_and(argvars, rettv)
8724 typval_T *argvars;
8725 typval_T *rettv;
8726{
8727 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8728 & get_tv_number_chk(&argvars[1], NULL);
8729}
8730
8731/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008732 * "append(lnum, string/list)" function
8733 */
8734 static void
8735f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008736 typval_T *argvars;
8737 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008738{
8739 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008740 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008741 list_T *l = NULL;
8742 listitem_T *li = NULL;
8743 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008744 long added = 0;
8745
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008746 /* When coming here from Insert mode, sync undo, so that this can be
8747 * undone separately from what was previously inserted. */
8748 if (u_sync_once == 2)
8749 {
8750 u_sync_once = 1; /* notify that u_sync() was called */
8751 u_sync(TRUE);
8752 }
8753
Bram Moolenaar0d660222005-01-07 21:51:51 +00008754 lnum = get_tv_lnum(argvars);
8755 if (lnum >= 0
8756 && lnum <= curbuf->b_ml.ml_line_count
8757 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008758 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008759 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008760 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008761 l = argvars[1].vval.v_list;
8762 if (l == NULL)
8763 return;
8764 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008765 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008766 for (;;)
8767 {
8768 if (l == NULL)
8769 tv = &argvars[1]; /* append a string */
8770 else if (li == NULL)
8771 break; /* end of list */
8772 else
8773 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008774 line = get_tv_string_chk(tv);
8775 if (line == NULL) /* type error */
8776 {
8777 rettv->vval.v_number = 1; /* Failed */
8778 break;
8779 }
8780 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008781 ++added;
8782 if (l == NULL)
8783 break;
8784 li = li->li_next;
8785 }
8786
8787 appended_lines_mark(lnum, added);
8788 if (curwin->w_cursor.lnum > lnum)
8789 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008791 else
8792 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008793}
8794
8795/*
8796 * "argc()" function
8797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008799f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008800 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008802{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008803 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008804}
8805
8806/*
8807 * "argidx()" function
8808 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008810f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008811 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008812 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008814 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008815}
8816
8817/*
8818 * "argv(nr)" function
8819 */
8820 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008821f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008822 typval_T *argvars;
8823 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824{
8825 int idx;
8826
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008827 if (argvars[0].v_type != VAR_UNKNOWN)
8828 {
8829 idx = get_tv_number_chk(&argvars[0], NULL);
8830 if (idx >= 0 && idx < ARGCOUNT)
8831 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8832 else
8833 rettv->vval.v_string = NULL;
8834 rettv->v_type = VAR_STRING;
8835 }
8836 else if (rettv_list_alloc(rettv) == OK)
8837 for (idx = 0; idx < ARGCOUNT; ++idx)
8838 list_append_string(rettv->vval.v_list,
8839 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840}
8841
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008842#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008843/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008844 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008845 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008846 static void
8847f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008848 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008849 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008850{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008851 float_T f;
8852
8853 rettv->v_type = VAR_FLOAT;
8854 if (get_float_arg(argvars, &f) == OK)
8855 rettv->vval.v_float = asin(f);
8856 else
8857 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008858}
8859
8860/*
8861 * "atan()" function
8862 */
8863 static void
8864f_atan(argvars, rettv)
8865 typval_T *argvars;
8866 typval_T *rettv;
8867{
8868 float_T f;
8869
8870 rettv->v_type = VAR_FLOAT;
8871 if (get_float_arg(argvars, &f) == OK)
8872 rettv->vval.v_float = atan(f);
8873 else
8874 rettv->vval.v_float = 0.0;
8875}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008876
8877/*
8878 * "atan2()" function
8879 */
8880 static void
8881f_atan2(argvars, rettv)
8882 typval_T *argvars;
8883 typval_T *rettv;
8884{
8885 float_T fx, fy;
8886
8887 rettv->v_type = VAR_FLOAT;
8888 if (get_float_arg(argvars, &fx) == OK
8889 && get_float_arg(&argvars[1], &fy) == OK)
8890 rettv->vval.v_float = atan2(fx, fy);
8891 else
8892 rettv->vval.v_float = 0.0;
8893}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008894#endif
8895
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896/*
8897 * "browse(save, title, initdir, default)" function
8898 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008900f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008901 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008902 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903{
8904#ifdef FEAT_BROWSE
8905 int save;
8906 char_u *title;
8907 char_u *initdir;
8908 char_u *defname;
8909 char_u buf[NUMBUFLEN];
8910 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008911 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008913 save = get_tv_number_chk(&argvars[0], &error);
8914 title = get_tv_string_chk(&argvars[1]);
8915 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8916 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008918 if (error || title == NULL || initdir == NULL || defname == NULL)
8919 rettv->vval.v_string = NULL;
8920 else
8921 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008922 do_browse(save ? BROWSE_SAVE : 0,
8923 title, defname, NULL, initdir, NULL, curbuf);
8924#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008925 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008926#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008927 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008928}
8929
8930/*
8931 * "browsedir(title, initdir)" function
8932 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008933 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008934f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008935 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008936 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008937{
8938#ifdef FEAT_BROWSE
8939 char_u *title;
8940 char_u *initdir;
8941 char_u buf[NUMBUFLEN];
8942
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008943 title = get_tv_string_chk(&argvars[0]);
8944 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008945
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008946 if (title == NULL || initdir == NULL)
8947 rettv->vval.v_string = NULL;
8948 else
8949 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008950 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008952 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008954 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955}
8956
Bram Moolenaar33570922005-01-25 22:26:29 +00008957static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008958
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959/*
8960 * Find a buffer by number or exact name.
8961 */
8962 static buf_T *
8963find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008964 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965{
8966 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008967
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008968 if (avar->v_type == VAR_NUMBER)
8969 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008970 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008972 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008973 if (buf == NULL)
8974 {
8975 /* No full path name match, try a match with a URL or a "nofile"
8976 * buffer, these don't use the full path. */
8977 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8978 if (buf->b_fname != NULL
8979 && (path_with_url(buf->b_fname)
8980#ifdef FEAT_QUICKFIX
8981 || bt_nofile(buf)
8982#endif
8983 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008984 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008985 break;
8986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008987 }
8988 return buf;
8989}
8990
8991/*
8992 * "bufexists(expr)" function
8993 */
8994 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008995f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008996 typval_T *argvars;
8997 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008999 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000}
9001
9002/*
9003 * "buflisted(expr)" function
9004 */
9005 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009006f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009007 typval_T *argvars;
9008 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009{
9010 buf_T *buf;
9011
9012 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009013 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014}
9015
9016/*
9017 * "bufloaded(expr)" function
9018 */
9019 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009020f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009021 typval_T *argvars;
9022 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009023{
9024 buf_T *buf;
9025
9026 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009027 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028}
9029
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009030static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009031
Bram Moolenaar071d4272004-06-13 20:20:40 +00009032/*
9033 * Get buffer by number or pattern.
9034 */
9035 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009036get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009037 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009038 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009040 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 int save_magic;
9042 char_u *save_cpo;
9043 buf_T *buf;
9044
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009045 if (tv->v_type == VAR_NUMBER)
9046 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009047 if (tv->v_type != VAR_STRING)
9048 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009049 if (name == NULL || *name == NUL)
9050 return curbuf;
9051 if (name[0] == '$' && name[1] == NUL)
9052 return lastbuf;
9053
9054 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9055 save_magic = p_magic;
9056 p_magic = TRUE;
9057 save_cpo = p_cpo;
9058 p_cpo = (char_u *)"";
9059
9060 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009061 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062
9063 p_magic = save_magic;
9064 p_cpo = save_cpo;
9065
9066 /* If not found, try expanding the name, like done for bufexists(). */
9067 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009068 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069
9070 return buf;
9071}
9072
9073/*
9074 * "bufname(expr)" function
9075 */
9076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009078 typval_T *argvars;
9079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080{
9081 buf_T *buf;
9082
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009083 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009085 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009086 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009090 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009091 --emsg_off;
9092}
9093
9094/*
9095 * "bufnr(expr)" function
9096 */
9097 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009098f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009099 typval_T *argvars;
9100 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101{
9102 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009103 int error = FALSE;
9104 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009106 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009108 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009109 --emsg_off;
9110
9111 /* If the buffer isn't found and the second argument is not zero create a
9112 * new buffer. */
9113 if (buf == NULL
9114 && argvars[1].v_type != VAR_UNKNOWN
9115 && get_tv_number_chk(&argvars[1], &error) != 0
9116 && !error
9117 && (name = get_tv_string_chk(&argvars[0])) != NULL
9118 && !error)
9119 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9120
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009122 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009124 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009125}
9126
9127/*
9128 * "bufwinnr(nr)" function
9129 */
9130 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009131f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009132 typval_T *argvars;
9133 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
9135#ifdef FEAT_WINDOWS
9136 win_T *wp;
9137 int winnr = 0;
9138#endif
9139 buf_T *buf;
9140
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009141 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009143 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144#ifdef FEAT_WINDOWS
9145 for (wp = firstwin; wp; wp = wp->w_next)
9146 {
9147 ++winnr;
9148 if (wp->w_buffer == buf)
9149 break;
9150 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009151 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009153 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154#endif
9155 --emsg_off;
9156}
9157
9158/*
9159 * "byte2line(byte)" function
9160 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009162f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009163 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009164 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165{
9166#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009167 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168#else
9169 long boff = 0;
9170
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009171 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009173 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009175 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 (linenr_T)0, &boff);
9177#endif
9178}
9179
9180/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009181 * "byteidx()" function
9182 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009183 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009185 typval_T *argvars;
9186 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009187{
9188#ifdef FEAT_MBYTE
9189 char_u *t;
9190#endif
9191 char_u *str;
9192 long idx;
9193
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009194 str = get_tv_string_chk(&argvars[0]);
9195 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009196 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009197 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009198 return;
9199
9200#ifdef FEAT_MBYTE
9201 t = str;
9202 for ( ; idx > 0; idx--)
9203 {
9204 if (*t == NUL) /* EOL reached */
9205 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009206 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009207 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009208 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009209#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009210 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009211 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009212#endif
9213}
9214
Bram Moolenaardb913952012-06-29 12:54:53 +02009215 int
9216func_call(name, args, selfdict, rettv)
9217 char_u *name;
9218 typval_T *args;
9219 dict_T *selfdict;
9220 typval_T *rettv;
9221{
9222 listitem_T *item;
9223 typval_T argv[MAX_FUNC_ARGS + 1];
9224 int argc = 0;
9225 int dummy;
9226 int r = 0;
9227
9228 for (item = args->vval.v_list->lv_first; item != NULL;
9229 item = item->li_next)
9230 {
9231 if (argc == MAX_FUNC_ARGS)
9232 {
9233 EMSG(_("E699: Too many arguments"));
9234 break;
9235 }
9236 /* Make a copy of each argument. This is needed to be able to set
9237 * v_lock to VAR_FIXED in the copy without changing the original list.
9238 */
9239 copy_tv(&item->li_tv, &argv[argc++]);
9240 }
9241
9242 if (item == NULL)
9243 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9244 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9245 &dummy, TRUE, selfdict);
9246
9247 /* Free the arguments. */
9248 while (argc > 0)
9249 clear_tv(&argv[--argc]);
9250
9251 return r;
9252}
9253
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009254/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009255 * "call(func, arglist)" function
9256 */
9257 static void
9258f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009259 typval_T *argvars;
9260 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009261{
9262 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009263 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009264
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009265 if (argvars[1].v_type != VAR_LIST)
9266 {
9267 EMSG(_(e_listreq));
9268 return;
9269 }
9270 if (argvars[1].vval.v_list == NULL)
9271 return;
9272
9273 if (argvars[0].v_type == VAR_FUNC)
9274 func = argvars[0].vval.v_string;
9275 else
9276 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009277 if (*func == NUL)
9278 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009279
Bram Moolenaare9a41262005-01-15 22:18:47 +00009280 if (argvars[2].v_type != VAR_UNKNOWN)
9281 {
9282 if (argvars[2].v_type != VAR_DICT)
9283 {
9284 EMSG(_(e_dictreq));
9285 return;
9286 }
9287 selfdict = argvars[2].vval.v_dict;
9288 }
9289
Bram Moolenaardb913952012-06-29 12:54:53 +02009290 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009291}
9292
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009293#ifdef FEAT_FLOAT
9294/*
9295 * "ceil({float})" function
9296 */
9297 static void
9298f_ceil(argvars, rettv)
9299 typval_T *argvars;
9300 typval_T *rettv;
9301{
9302 float_T f;
9303
9304 rettv->v_type = VAR_FLOAT;
9305 if (get_float_arg(argvars, &f) == OK)
9306 rettv->vval.v_float = ceil(f);
9307 else
9308 rettv->vval.v_float = 0.0;
9309}
9310#endif
9311
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009312/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009313 * "changenr()" function
9314 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009315 static void
9316f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009317 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009318 typval_T *rettv;
9319{
9320 rettv->vval.v_number = curbuf->b_u_seq_cur;
9321}
9322
9323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324 * "char2nr(string)" function
9325 */
9326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009327f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009328 typval_T *argvars;
9329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009330{
9331#ifdef FEAT_MBYTE
9332 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009333 {
9334 int utf8 = 0;
9335
9336 if (argvars[1].v_type != VAR_UNKNOWN)
9337 utf8 = get_tv_number_chk(&argvars[1], NULL);
9338
9339 if (utf8)
9340 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9341 else
9342 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344 else
9345#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009346 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347}
9348
9349/*
9350 * "cindent(lnum)" function
9351 */
9352 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009353f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009354 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009355 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009356{
9357#ifdef FEAT_CINDENT
9358 pos_T pos;
9359 linenr_T lnum;
9360
9361 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009362 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009363 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9364 {
9365 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009366 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009367 curwin->w_cursor = pos;
9368 }
9369 else
9370#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009371 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372}
9373
9374/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009375 * "clearmatches()" function
9376 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009377 static void
9378f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009379 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009380 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009381{
9382#ifdef FEAT_SEARCH_EXTRA
9383 clear_matches(curwin);
9384#endif
9385}
9386
9387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009388 * "col(string)" function
9389 */
9390 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009391f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009392 typval_T *argvars;
9393 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009394{
9395 colnr_T col = 0;
9396 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009397 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009399 fp = var2fpos(&argvars[0], FALSE, &fnum);
9400 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009401 {
9402 if (fp->col == MAXCOL)
9403 {
9404 /* '> can be MAXCOL, get the length of the line then */
9405 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009406 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009407 else
9408 col = MAXCOL;
9409 }
9410 else
9411 {
9412 col = fp->col + 1;
9413#ifdef FEAT_VIRTUALEDIT
9414 /* col(".") when the cursor is on the NUL at the end of the line
9415 * because of "coladd" can be seen as an extra column. */
9416 if (virtual_active() && fp == &curwin->w_cursor)
9417 {
9418 char_u *p = ml_get_cursor();
9419
9420 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9421 curwin->w_virtcol - curwin->w_cursor.coladd))
9422 {
9423# ifdef FEAT_MBYTE
9424 int l;
9425
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009426 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009427 col += l;
9428# else
9429 if (*p != NUL && p[1] == NUL)
9430 ++col;
9431# endif
9432 }
9433 }
9434#endif
9435 }
9436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009437 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438}
9439
Bram Moolenaar572cb562005-08-05 21:35:02 +00009440#if defined(FEAT_INS_EXPAND)
9441/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009442 * "complete()" function
9443 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009444 static void
9445f_complete(argvars, rettv)
9446 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009447 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009448{
9449 int startcol;
9450
9451 if ((State & INSERT) == 0)
9452 {
9453 EMSG(_("E785: complete() can only be used in Insert mode"));
9454 return;
9455 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009456
9457 /* Check for undo allowed here, because if something was already inserted
9458 * the line was already saved for undo and this check isn't done. */
9459 if (!undo_allowed())
9460 return;
9461
Bram Moolenaarade00832006-03-10 21:46:58 +00009462 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9463 {
9464 EMSG(_(e_invarg));
9465 return;
9466 }
9467
9468 startcol = get_tv_number_chk(&argvars[0], NULL);
9469 if (startcol <= 0)
9470 return;
9471
9472 set_completion(startcol - 1, argvars[1].vval.v_list);
9473}
9474
9475/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009476 * "complete_add()" function
9477 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009478 static void
9479f_complete_add(argvars, rettv)
9480 typval_T *argvars;
9481 typval_T *rettv;
9482{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009483 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009484}
9485
9486/*
9487 * "complete_check()" function
9488 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009489 static void
9490f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009491 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009492 typval_T *rettv;
9493{
9494 int saved = RedrawingDisabled;
9495
9496 RedrawingDisabled = 0;
9497 ins_compl_check_keys(0);
9498 rettv->vval.v_number = compl_interrupted;
9499 RedrawingDisabled = saved;
9500}
9501#endif
9502
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503/*
9504 * "confirm(message, buttons[, default [, type]])" function
9505 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009507f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009508 typval_T *argvars UNUSED;
9509 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510{
9511#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9512 char_u *message;
9513 char_u *buttons = NULL;
9514 char_u buf[NUMBUFLEN];
9515 char_u buf2[NUMBUFLEN];
9516 int def = 1;
9517 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009518 char_u *typestr;
9519 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009521 message = get_tv_string_chk(&argvars[0]);
9522 if (message == NULL)
9523 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009524 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009525 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009526 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9527 if (buttons == NULL)
9528 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009529 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009530 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009531 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009532 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009534 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9535 if (typestr == NULL)
9536 error = TRUE;
9537 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009539 switch (TOUPPER_ASC(*typestr))
9540 {
9541 case 'E': type = VIM_ERROR; break;
9542 case 'Q': type = VIM_QUESTION; break;
9543 case 'I': type = VIM_INFO; break;
9544 case 'W': type = VIM_WARNING; break;
9545 case 'G': type = VIM_GENERIC; break;
9546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 }
9548 }
9549 }
9550 }
9551
9552 if (buttons == NULL || *buttons == NUL)
9553 buttons = (char_u *)_("&Ok");
9554
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009555 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009556 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009557 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558#endif
9559}
9560
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009561/*
9562 * "copy()" function
9563 */
9564 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009565f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009566 typval_T *argvars;
9567 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009568{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009569 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009570}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009571
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009572#ifdef FEAT_FLOAT
9573/*
9574 * "cos()" function
9575 */
9576 static void
9577f_cos(argvars, rettv)
9578 typval_T *argvars;
9579 typval_T *rettv;
9580{
9581 float_T f;
9582
9583 rettv->v_type = VAR_FLOAT;
9584 if (get_float_arg(argvars, &f) == OK)
9585 rettv->vval.v_float = cos(f);
9586 else
9587 rettv->vval.v_float = 0.0;
9588}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009589
9590/*
9591 * "cosh()" function
9592 */
9593 static void
9594f_cosh(argvars, rettv)
9595 typval_T *argvars;
9596 typval_T *rettv;
9597{
9598 float_T f;
9599
9600 rettv->v_type = VAR_FLOAT;
9601 if (get_float_arg(argvars, &f) == OK)
9602 rettv->vval.v_float = cosh(f);
9603 else
9604 rettv->vval.v_float = 0.0;
9605}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009606#endif
9607
Bram Moolenaar071d4272004-06-13 20:20:40 +00009608/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009609 * "count()" function
9610 */
9611 static void
9612f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009613 typval_T *argvars;
9614 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009615{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009616 long n = 0;
9617 int ic = FALSE;
9618
Bram Moolenaare9a41262005-01-15 22:18:47 +00009619 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009620 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009621 listitem_T *li;
9622 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009623 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009624
Bram Moolenaare9a41262005-01-15 22:18:47 +00009625 if ((l = argvars[0].vval.v_list) != NULL)
9626 {
9627 li = l->lv_first;
9628 if (argvars[2].v_type != VAR_UNKNOWN)
9629 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009630 int error = FALSE;
9631
9632 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009633 if (argvars[3].v_type != VAR_UNKNOWN)
9634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009635 idx = get_tv_number_chk(&argvars[3], &error);
9636 if (!error)
9637 {
9638 li = list_find(l, idx);
9639 if (li == NULL)
9640 EMSGN(_(e_listidx), idx);
9641 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009642 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009643 if (error)
9644 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009645 }
9646
9647 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009648 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009649 ++n;
9650 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009651 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009652 else if (argvars[0].v_type == VAR_DICT)
9653 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009654 int todo;
9655 dict_T *d;
9656 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009657
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009658 if ((d = argvars[0].vval.v_dict) != NULL)
9659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009660 int error = FALSE;
9661
Bram Moolenaare9a41262005-01-15 22:18:47 +00009662 if (argvars[2].v_type != VAR_UNKNOWN)
9663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009664 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009665 if (argvars[3].v_type != VAR_UNKNOWN)
9666 EMSG(_(e_invarg));
9667 }
9668
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009669 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009670 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009671 {
9672 if (!HASHITEM_EMPTY(hi))
9673 {
9674 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009675 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009676 ++n;
9677 }
9678 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009679 }
9680 }
9681 else
9682 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009683 rettv->vval.v_number = n;
9684}
9685
9686/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009687 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9688 *
9689 * Checks the existence of a cscope connection.
9690 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009693 typval_T *argvars UNUSED;
9694 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695{
9696#ifdef FEAT_CSCOPE
9697 int num = 0;
9698 char_u *dbpath = NULL;
9699 char_u *prepend = NULL;
9700 char_u buf[NUMBUFLEN];
9701
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009702 if (argvars[0].v_type != VAR_UNKNOWN
9703 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009705 num = (int)get_tv_number(&argvars[0]);
9706 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009707 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 }
9710
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712#endif
9713}
9714
9715/*
9716 * "cursor(lnum, col)" function
9717 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009718 * Moves the cursor to the specified line and column.
9719 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009722f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009723 typval_T *argvars;
9724 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725{
9726 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009727#ifdef FEAT_VIRTUALEDIT
9728 long coladd = 0;
9729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009731 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009732 if (argvars[1].v_type == VAR_UNKNOWN)
9733 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009734 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009735
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009736 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009737 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009738 line = pos.lnum;
9739 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009740#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009741 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009742#endif
9743 }
9744 else
9745 {
9746 line = get_tv_lnum(argvars);
9747 col = get_tv_number_chk(&argvars[1], NULL);
9748#ifdef FEAT_VIRTUALEDIT
9749 if (argvars[2].v_type != VAR_UNKNOWN)
9750 coladd = get_tv_number_chk(&argvars[2], NULL);
9751#endif
9752 }
9753 if (line < 0 || col < 0
9754#ifdef FEAT_VIRTUALEDIT
9755 || coladd < 0
9756#endif
9757 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009758 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759 if (line > 0)
9760 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761 if (col > 0)
9762 curwin->w_cursor.col = col - 1;
9763#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009764 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765#endif
9766
9767 /* Make sure the cursor is in a valid position. */
9768 check_cursor();
9769#ifdef FEAT_MBYTE
9770 /* Correct cursor for multi-byte character. */
9771 if (has_mbyte)
9772 mb_adjust_cursor();
9773#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009774
9775 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009776 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777}
9778
9779/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009780 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 */
9782 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009784 typval_T *argvars;
9785 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009787 int noref = 0;
9788
9789 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009790 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009791 if (noref < 0 || noref > 1)
9792 EMSG(_(e_invarg));
9793 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009794 {
9795 current_copyID += COPYID_INC;
9796 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9797 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009798}
9799
9800/*
9801 * "delete()" function
9802 */
9803 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009804f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009805 typval_T *argvars;
9806 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009807{
9808 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812}
9813
9814/*
9815 * "did_filetype()" function
9816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009818f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009819 typval_T *argvars UNUSED;
9820 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
9822#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009823 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824#endif
9825}
9826
9827/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009828 * "diff_filler()" function
9829 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009830 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009831f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009832 typval_T *argvars UNUSED;
9833 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009834{
9835#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009837#endif
9838}
9839
9840/*
9841 * "diff_hlID()" function
9842 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009843 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009844f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009845 typval_T *argvars UNUSED;
9846 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009847{
9848#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009849 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009850 static linenr_T prev_lnum = 0;
9851 static int changedtick = 0;
9852 static int fnum = 0;
9853 static int change_start = 0;
9854 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009855 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009856 int filler_lines;
9857 int col;
9858
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009859 if (lnum < 0) /* ignore type error in {lnum} arg */
9860 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009861 if (lnum != prev_lnum
9862 || changedtick != curbuf->b_changedtick
9863 || fnum != curbuf->b_fnum)
9864 {
9865 /* New line, buffer, change: need to get the values. */
9866 filler_lines = diff_check(curwin, lnum);
9867 if (filler_lines < 0)
9868 {
9869 if (filler_lines == -1)
9870 {
9871 change_start = MAXCOL;
9872 change_end = -1;
9873 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9874 hlID = HLF_ADD; /* added line */
9875 else
9876 hlID = HLF_CHD; /* changed line */
9877 }
9878 else
9879 hlID = HLF_ADD; /* added line */
9880 }
9881 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009882 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009883 prev_lnum = lnum;
9884 changedtick = curbuf->b_changedtick;
9885 fnum = curbuf->b_fnum;
9886 }
9887
9888 if (hlID == HLF_CHD || hlID == HLF_TXD)
9889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009890 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009891 if (col >= change_start && col <= change_end)
9892 hlID = HLF_TXD; /* changed text */
9893 else
9894 hlID = HLF_CHD; /* changed line */
9895 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009896 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009897#endif
9898}
9899
9900/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009901 * "empty({expr})" function
9902 */
9903 static void
9904f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009905 typval_T *argvars;
9906 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009907{
9908 int n;
9909
9910 switch (argvars[0].v_type)
9911 {
9912 case VAR_STRING:
9913 case VAR_FUNC:
9914 n = argvars[0].vval.v_string == NULL
9915 || *argvars[0].vval.v_string == NUL;
9916 break;
9917 case VAR_NUMBER:
9918 n = argvars[0].vval.v_number == 0;
9919 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009920#ifdef FEAT_FLOAT
9921 case VAR_FLOAT:
9922 n = argvars[0].vval.v_float == 0.0;
9923 break;
9924#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009925 case VAR_LIST:
9926 n = argvars[0].vval.v_list == NULL
9927 || argvars[0].vval.v_list->lv_first == NULL;
9928 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009929 case VAR_DICT:
9930 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009931 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009932 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009933 default:
9934 EMSG2(_(e_intern2), "f_empty()");
9935 n = 0;
9936 }
9937
9938 rettv->vval.v_number = n;
9939}
9940
9941/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009942 * "escape({string}, {chars})" function
9943 */
9944 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009945f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009946 typval_T *argvars;
9947 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948{
9949 char_u buf[NUMBUFLEN];
9950
Bram Moolenaar758711c2005-02-02 23:11:38 +00009951 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9952 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009953 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954}
9955
9956/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009957 * "eval()" function
9958 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009959 static void
9960f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009961 typval_T *argvars;
9962 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009963{
9964 char_u *s;
9965
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009966 s = get_tv_string_chk(&argvars[0]);
9967 if (s != NULL)
9968 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009969
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009970 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9971 {
9972 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009973 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009974 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009975 else if (*s != NUL)
9976 EMSG(_(e_trailing));
9977}
9978
9979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 * "eventhandler()" function
9981 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009983f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009984 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009985 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009987 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988}
9989
9990/*
9991 * "executable()" function
9992 */
9993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009994f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009995 typval_T *argvars;
9996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009997{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009998 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999}
10000
10001/*
10002 * "exists()" function
10003 */
10004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010005f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010006 typval_T *argvars;
10007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010008{
10009 char_u *p;
10010 char_u *name;
10011 int n = FALSE;
10012 int len = 0;
10013
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010014 no_autoload = TRUE;
10015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010016 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010017 if (*p == '$') /* environment variable */
10018 {
10019 /* first try "normal" environment variables (fast) */
10020 if (mch_getenv(p + 1) != NULL)
10021 n = TRUE;
10022 else
10023 {
10024 /* try expanding things like $VIM and ${HOME} */
10025 p = expand_env_save(p);
10026 if (p != NULL && *p != '$')
10027 n = TRUE;
10028 vim_free(p);
10029 }
10030 }
10031 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010033 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010034 if (*skipwhite(p) != NUL)
10035 n = FALSE; /* trailing garbage */
10036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037 else if (*p == '*') /* internal or user defined function */
10038 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010039 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010040 }
10041 else if (*p == ':')
10042 {
10043 n = cmd_exists(p + 1);
10044 }
10045 else if (*p == '#')
10046 {
10047#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010048 if (p[1] == '#')
10049 n = autocmd_supported(p + 2);
10050 else
10051 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010052#endif
10053 }
10054 else /* internal variable */
10055 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010056 char_u *tofree;
10057 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010058
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010059 /* get_name_len() takes care of expanding curly braces */
10060 name = p;
10061 len = get_name_len(&p, &tofree, TRUE, FALSE);
10062 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010064 if (tofree != NULL)
10065 name = tofree;
10066 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10067 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010069 /* handle d.key, l[idx], f(expr) */
10070 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10071 if (n)
10072 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073 }
10074 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010075 if (*p != NUL)
10076 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010078 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010079 }
10080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010081 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010082
10083 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010084}
10085
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010086#ifdef FEAT_FLOAT
10087/*
10088 * "exp()" function
10089 */
10090 static void
10091f_exp(argvars, rettv)
10092 typval_T *argvars;
10093 typval_T *rettv;
10094{
10095 float_T f;
10096
10097 rettv->v_type = VAR_FLOAT;
10098 if (get_float_arg(argvars, &f) == OK)
10099 rettv->vval.v_float = exp(f);
10100 else
10101 rettv->vval.v_float = 0.0;
10102}
10103#endif
10104
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105/*
10106 * "expand()" function
10107 */
10108 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010109f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010110 typval_T *argvars;
10111 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010112{
10113 char_u *s;
10114 int len;
10115 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010116 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010118 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010119 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010121 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010122 if (argvars[1].v_type != VAR_UNKNOWN
10123 && argvars[2].v_type != VAR_UNKNOWN
10124 && get_tv_number_chk(&argvars[2], &error)
10125 && !error)
10126 {
10127 rettv->v_type = VAR_LIST;
10128 rettv->vval.v_list = NULL;
10129 }
10130
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010131 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132 if (*s == '%' || *s == '#' || *s == '<')
10133 {
10134 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010135 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010137 if (rettv->v_type == VAR_LIST)
10138 {
10139 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10140 list_append_string(rettv->vval.v_list, result, -1);
10141 else
10142 vim_free(result);
10143 }
10144 else
10145 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 }
10147 else
10148 {
10149 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010150 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010151 if (argvars[1].v_type != VAR_UNKNOWN
10152 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010153 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010154 if (!error)
10155 {
10156 ExpandInit(&xpc);
10157 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010158 if (p_wic)
10159 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010160 if (rettv->v_type == VAR_STRING)
10161 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10162 options, WILD_ALL);
10163 else if (rettv_list_alloc(rettv) != FAIL)
10164 {
10165 int i;
10166
10167 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10168 for (i = 0; i < xpc.xp_numfiles; i++)
10169 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10170 ExpandCleanup(&xpc);
10171 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010172 }
10173 else
10174 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 }
10176}
10177
10178/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010179 * Go over all entries in "d2" and add them to "d1".
10180 * When "action" is "error" then a duplicate key is an error.
10181 * When "action" is "force" then a duplicate key is overwritten.
10182 * Otherwise duplicate keys are ignored ("action" is "keep").
10183 */
10184 void
10185dict_extend(d1, d2, action)
10186 dict_T *d1;
10187 dict_T *d2;
10188 char_u *action;
10189{
10190 dictitem_T *di1;
10191 hashitem_T *hi2;
10192 int todo;
10193
10194 todo = (int)d2->dv_hashtab.ht_used;
10195 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10196 {
10197 if (!HASHITEM_EMPTY(hi2))
10198 {
10199 --todo;
10200 di1 = dict_find(d1, hi2->hi_key, -1);
10201 if (d1->dv_scope != 0)
10202 {
10203 /* Disallow replacing a builtin function in l: and g:.
10204 * Check the key to be valid when adding to any
10205 * scope. */
10206 if (d1->dv_scope == VAR_DEF_SCOPE
10207 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10208 && var_check_func_name(hi2->hi_key,
10209 di1 == NULL))
10210 break;
10211 if (!valid_varname(hi2->hi_key))
10212 break;
10213 }
10214 if (di1 == NULL)
10215 {
10216 di1 = dictitem_copy(HI2DI(hi2));
10217 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10218 dictitem_free(di1);
10219 }
10220 else if (*action == 'e')
10221 {
10222 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10223 break;
10224 }
10225 else if (*action == 'f' && HI2DI(hi2) != di1)
10226 {
10227 clear_tv(&di1->di_tv);
10228 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10229 }
10230 }
10231 }
10232}
10233
10234/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010235 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010236 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010237 */
10238 static void
10239f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010240 typval_T *argvars;
10241 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010242{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010243 char *arg_errmsg = N_("extend() argument");
10244
Bram Moolenaare9a41262005-01-15 22:18:47 +000010245 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010246 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010247 list_T *l1, *l2;
10248 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010249 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010250 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010251
Bram Moolenaare9a41262005-01-15 22:18:47 +000010252 l1 = argvars[0].vval.v_list;
10253 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010254 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010255 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010256 {
10257 if (argvars[2].v_type != VAR_UNKNOWN)
10258 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010259 before = get_tv_number_chk(&argvars[2], &error);
10260 if (error)
10261 return; /* type error; errmsg already given */
10262
Bram Moolenaar758711c2005-02-02 23:11:38 +000010263 if (before == l1->lv_len)
10264 item = NULL;
10265 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010266 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010267 item = list_find(l1, before);
10268 if (item == NULL)
10269 {
10270 EMSGN(_(e_listidx), before);
10271 return;
10272 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010273 }
10274 }
10275 else
10276 item = NULL;
10277 list_extend(l1, l2, item);
10278
Bram Moolenaare9a41262005-01-15 22:18:47 +000010279 copy_tv(&argvars[0], rettv);
10280 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010281 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010282 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10283 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010284 dict_T *d1, *d2;
10285 char_u *action;
10286 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010287
10288 d1 = argvars[0].vval.v_dict;
10289 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010290 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010291 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010292 {
10293 /* Check the third argument. */
10294 if (argvars[2].v_type != VAR_UNKNOWN)
10295 {
10296 static char *(av[]) = {"keep", "force", "error"};
10297
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010298 action = get_tv_string_chk(&argvars[2]);
10299 if (action == NULL)
10300 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010301 for (i = 0; i < 3; ++i)
10302 if (STRCMP(action, av[i]) == 0)
10303 break;
10304 if (i == 3)
10305 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010306 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010307 return;
10308 }
10309 }
10310 else
10311 action = (char_u *)"force";
10312
Bram Moolenaara9922d62013-05-30 13:01:18 +020010313 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314
Bram Moolenaare9a41262005-01-15 22:18:47 +000010315 copy_tv(&argvars[0], rettv);
10316 }
10317 }
10318 else
10319 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010320}
10321
10322/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010323 * "feedkeys()" function
10324 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010325 static void
10326f_feedkeys(argvars, rettv)
10327 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010328 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010329{
10330 int remap = TRUE;
10331 char_u *keys, *flags;
10332 char_u nbuf[NUMBUFLEN];
10333 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010334 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010335
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010336 /* This is not allowed in the sandbox. If the commands would still be
10337 * executed in the sandbox it would be OK, but it probably happens later,
10338 * when "sandbox" is no longer set. */
10339 if (check_secure())
10340 return;
10341
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010342 keys = get_tv_string(&argvars[0]);
10343 if (*keys != NUL)
10344 {
10345 if (argvars[1].v_type != VAR_UNKNOWN)
10346 {
10347 flags = get_tv_string_buf(&argvars[1], nbuf);
10348 for ( ; *flags != NUL; ++flags)
10349 {
10350 switch (*flags)
10351 {
10352 case 'n': remap = FALSE; break;
10353 case 'm': remap = TRUE; break;
10354 case 't': typed = TRUE; break;
10355 }
10356 }
10357 }
10358
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010359 /* Need to escape K_SPECIAL and CSI before putting the string in the
10360 * typeahead buffer. */
10361 keys_esc = vim_strsave_escape_csi(keys);
10362 if (keys_esc != NULL)
10363 {
10364 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010365 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010366 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010367 if (vgetc_busy)
10368 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010369 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010370 }
10371}
10372
10373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 * "filereadable()" function
10375 */
10376 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010377f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010378 typval_T *argvars;
10379 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010381 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010382 char_u *p;
10383 int n;
10384
Bram Moolenaarc236c162008-07-13 17:41:49 +000010385#ifndef O_NONBLOCK
10386# define O_NONBLOCK 0
10387#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010388 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010389 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10390 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010391 {
10392 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010393 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394 }
10395 else
10396 n = FALSE;
10397
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010398 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010399}
10400
10401/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010402 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010403 * rights to write into.
10404 */
10405 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010406f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010407 typval_T *argvars;
10408 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010409{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010410 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010411}
10412
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010413static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010414
10415 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010416findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010417 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010418 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010419 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010420{
10421#ifdef FEAT_SEARCHPATH
10422 char_u *fname;
10423 char_u *fresult = NULL;
10424 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10425 char_u *p;
10426 char_u pathbuf[NUMBUFLEN];
10427 int count = 1;
10428 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010429 int error = FALSE;
10430#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010431
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010432 rettv->vval.v_string = NULL;
10433 rettv->v_type = VAR_STRING;
10434
10435#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010436 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010437
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010438 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010440 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10441 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010442 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010443 else
10444 {
10445 if (*p != NUL)
10446 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010448 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010449 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010450 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010451 }
10452
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010453 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10454 error = TRUE;
10455
10456 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010457 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010458 do
10459 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010460 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010461 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010462 fresult = find_file_in_path_option(first ? fname : NULL,
10463 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010464 0, first, path,
10465 find_what,
10466 curbuf->b_ffname,
10467 find_what == FINDFILE_DIR
10468 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010469 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010470
10471 if (fresult != NULL && rettv->v_type == VAR_LIST)
10472 list_append_string(rettv->vval.v_list, fresult, -1);
10473
10474 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010475 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010476
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010477 if (rettv->v_type == VAR_STRING)
10478 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010479#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010480}
10481
Bram Moolenaar33570922005-01-25 22:26:29 +000010482static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10483static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010484
10485/*
10486 * Implementation of map() and filter().
10487 */
10488 static void
10489filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010490 typval_T *argvars;
10491 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010492 int map;
10493{
10494 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010495 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010496 listitem_T *li, *nli;
10497 list_T *l = NULL;
10498 dictitem_T *di;
10499 hashtab_T *ht;
10500 hashitem_T *hi;
10501 dict_T *d = NULL;
10502 typval_T save_val;
10503 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010504 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010505 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010506 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10507 char *arg_errmsg = (map ? N_("map() argument")
10508 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010509 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010510 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010511
Bram Moolenaare9a41262005-01-15 22:18:47 +000010512 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010513 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010514 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010515 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010516 return;
10517 }
10518 else if (argvars[0].v_type == VAR_DICT)
10519 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010520 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010521 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010522 return;
10523 }
10524 else
10525 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010526 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010527 return;
10528 }
10529
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010530 expr = get_tv_string_buf_chk(&argvars[1], buf);
10531 /* On type errors, the preceding call has already displayed an error
10532 * message. Avoid a misleading error message for an empty string that
10533 * was not passed as argument. */
10534 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010535 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010536 prepare_vimvar(VV_VAL, &save_val);
10537 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010538
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010539 /* We reset "did_emsg" to be able to detect whether an error
10540 * occurred during evaluation of the expression. */
10541 save_did_emsg = did_emsg;
10542 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010543
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010544 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010545 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010546 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010547 vimvars[VV_KEY].vv_type = VAR_STRING;
10548
10549 ht = &d->dv_hashtab;
10550 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010551 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010552 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010553 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010554 if (!HASHITEM_EMPTY(hi))
10555 {
10556 --todo;
10557 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010558 if (tv_check_lock(di->di_tv.v_lock,
10559 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010560 break;
10561 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010562 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010563 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010564 break;
10565 if (!map && rem)
10566 dictitem_remove(d, di);
10567 clear_tv(&vimvars[VV_KEY].vv_tv);
10568 }
10569 }
10570 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010571 }
10572 else
10573 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010574 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010576 for (li = l->lv_first; li != NULL; li = nli)
10577 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010578 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010579 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010580 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010581 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010582 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010583 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010584 break;
10585 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010586 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010587 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010588 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010589 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010590
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010591 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010592 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010593
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010594 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010595 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010596
10597 copy_tv(&argvars[0], rettv);
10598}
10599
10600 static int
10601filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010602 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010603 char_u *expr;
10604 int map;
10605 int *remp;
10606{
Bram Moolenaar33570922005-01-25 22:26:29 +000010607 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010608 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010609 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010610
Bram Moolenaar33570922005-01-25 22:26:29 +000010611 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010612 s = expr;
10613 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010614 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010615 if (*s != NUL) /* check for trailing chars after expr */
10616 {
10617 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010618 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010619 }
10620 if (map)
10621 {
10622 /* map(): replace the list item value */
10623 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010624 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010625 *tv = rettv;
10626 }
10627 else
10628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010629 int error = FALSE;
10630
Bram Moolenaare9a41262005-01-15 22:18:47 +000010631 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010632 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010633 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010634 /* On type error, nothing has been removed; return FAIL to stop the
10635 * loop. The error message was given by get_tv_number_chk(). */
10636 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010637 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010638 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010639 retval = OK;
10640theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010641 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010642 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010643}
10644
10645/*
10646 * "filter()" function
10647 */
10648 static void
10649f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010650 typval_T *argvars;
10651 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010652{
10653 filter_map(argvars, rettv, FALSE);
10654}
10655
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010656/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010657 * "finddir({fname}[, {path}[, {count}]])" function
10658 */
10659 static void
10660f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010661 typval_T *argvars;
10662 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010663{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010664 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010665}
10666
10667/*
10668 * "findfile({fname}[, {path}[, {count}]])" function
10669 */
10670 static void
10671f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010672 typval_T *argvars;
10673 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010674{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010675 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010676}
10677
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010678#ifdef FEAT_FLOAT
10679/*
10680 * "float2nr({float})" function
10681 */
10682 static void
10683f_float2nr(argvars, rettv)
10684 typval_T *argvars;
10685 typval_T *rettv;
10686{
10687 float_T f;
10688
10689 if (get_float_arg(argvars, &f) == OK)
10690 {
10691 if (f < -0x7fffffff)
10692 rettv->vval.v_number = -0x7fffffff;
10693 else if (f > 0x7fffffff)
10694 rettv->vval.v_number = 0x7fffffff;
10695 else
10696 rettv->vval.v_number = (varnumber_T)f;
10697 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010698}
10699
10700/*
10701 * "floor({float})" function
10702 */
10703 static void
10704f_floor(argvars, rettv)
10705 typval_T *argvars;
10706 typval_T *rettv;
10707{
10708 float_T f;
10709
10710 rettv->v_type = VAR_FLOAT;
10711 if (get_float_arg(argvars, &f) == OK)
10712 rettv->vval.v_float = floor(f);
10713 else
10714 rettv->vval.v_float = 0.0;
10715}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010716
10717/*
10718 * "fmod()" function
10719 */
10720 static void
10721f_fmod(argvars, rettv)
10722 typval_T *argvars;
10723 typval_T *rettv;
10724{
10725 float_T fx, fy;
10726
10727 rettv->v_type = VAR_FLOAT;
10728 if (get_float_arg(argvars, &fx) == OK
10729 && get_float_arg(&argvars[1], &fy) == OK)
10730 rettv->vval.v_float = fmod(fx, fy);
10731 else
10732 rettv->vval.v_float = 0.0;
10733}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010734#endif
10735
Bram Moolenaar0d660222005-01-07 21:51:51 +000010736/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010737 * "fnameescape({string})" function
10738 */
10739 static void
10740f_fnameescape(argvars, rettv)
10741 typval_T *argvars;
10742 typval_T *rettv;
10743{
10744 rettv->vval.v_string = vim_strsave_fnameescape(
10745 get_tv_string(&argvars[0]), FALSE);
10746 rettv->v_type = VAR_STRING;
10747}
10748
10749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750 * "fnamemodify({fname}, {mods})" function
10751 */
10752 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010753f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010754 typval_T *argvars;
10755 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756{
10757 char_u *fname;
10758 char_u *mods;
10759 int usedlen = 0;
10760 int len;
10761 char_u *fbuf = NULL;
10762 char_u buf[NUMBUFLEN];
10763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010764 fname = get_tv_string_chk(&argvars[0]);
10765 mods = get_tv_string_buf_chk(&argvars[1], buf);
10766 if (fname == NULL || mods == NULL)
10767 fname = NULL;
10768 else
10769 {
10770 len = (int)STRLEN(fname);
10771 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010774 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010775 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010776 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010777 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010778 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010779 vim_free(fbuf);
10780}
10781
Bram Moolenaar33570922005-01-25 22:26:29 +000010782static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783
10784/*
10785 * "foldclosed()" function
10786 */
10787 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010788foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010789 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010790 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010791 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792{
10793#ifdef FEAT_FOLDING
10794 linenr_T lnum;
10795 linenr_T first, last;
10796
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010797 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10799 {
10800 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10801 {
10802 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010803 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806 return;
10807 }
10808 }
10809#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010810 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010811}
10812
10813/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010814 * "foldclosed()" function
10815 */
10816 static void
10817f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010818 typval_T *argvars;
10819 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010820{
10821 foldclosed_both(argvars, rettv, FALSE);
10822}
10823
10824/*
10825 * "foldclosedend()" function
10826 */
10827 static void
10828f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010829 typval_T *argvars;
10830 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010831{
10832 foldclosed_both(argvars, rettv, TRUE);
10833}
10834
10835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 * "foldlevel()" function
10837 */
10838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010839f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010840 typval_T *argvars UNUSED;
10841 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010842{
10843#ifdef FEAT_FOLDING
10844 linenr_T lnum;
10845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010846 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010848 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010850}
10851
10852/*
10853 * "foldtext()" function
10854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010855 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010856f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010857 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010858 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010859{
10860#ifdef FEAT_FOLDING
10861 linenr_T lnum;
10862 char_u *s;
10863 char_u *r;
10864 int len;
10865 char *txt;
10866#endif
10867
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010868 rettv->v_type = VAR_STRING;
10869 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010870#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010871 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10872 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10873 <= curbuf->b_ml.ml_line_count
10874 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 {
10876 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010877 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10878 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879 {
10880 if (!linewhite(lnum))
10881 break;
10882 ++lnum;
10883 }
10884
10885 /* Find interesting text in this line. */
10886 s = skipwhite(ml_get(lnum));
10887 /* skip C comment-start */
10888 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010889 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010890 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010891 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010892 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010893 {
10894 s = skipwhite(ml_get(lnum + 1));
10895 if (*s == '*')
10896 s = skipwhite(s + 1);
10897 }
10898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899 txt = _("+-%s%3ld lines: ");
10900 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010901 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010902 + 20 /* for %3ld */
10903 + STRLEN(s))); /* concatenated */
10904 if (r != NULL)
10905 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010906 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10907 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10908 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909 len = (int)STRLEN(r);
10910 STRCAT(r, s);
10911 /* remove 'foldmarker' and 'commentstring' */
10912 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010913 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 }
10915 }
10916#endif
10917}
10918
10919/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010920 * "foldtextresult(lnum)" function
10921 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010922 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010923f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010924 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010925 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010926{
10927#ifdef FEAT_FOLDING
10928 linenr_T lnum;
10929 char_u *text;
10930 char_u buf[51];
10931 foldinfo_T foldinfo;
10932 int fold_count;
10933#endif
10934
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010935 rettv->v_type = VAR_STRING;
10936 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010937#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010938 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010939 /* treat illegal types and illegal string values for {lnum} the same */
10940 if (lnum < 0)
10941 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010942 fold_count = foldedCount(curwin, lnum, &foldinfo);
10943 if (fold_count > 0)
10944 {
10945 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10946 &foldinfo, buf);
10947 if (text == buf)
10948 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010949 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010950 }
10951#endif
10952}
10953
10954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010955 * "foreground()" function
10956 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010957 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010958f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010959 typval_T *argvars UNUSED;
10960 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962#ifdef FEAT_GUI
10963 if (gui.in_use)
10964 gui_mch_set_foreground();
10965#else
10966# ifdef WIN32
10967 win32_set_foreground();
10968# endif
10969#endif
10970}
10971
10972/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010973 * "function()" function
10974 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010975 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010976f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010977 typval_T *argvars;
10978 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010979{
10980 char_u *s;
10981
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010982 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010983 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010984 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010985 /* Don't check an autoload name for existence here. */
10986 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010987 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010988 else
10989 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010990 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010991 {
10992 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010993 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010994
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010995 /* Expand s: and <SID> into <SNR>nr_, so that the function can
10996 * also be called from another script. Using trans_function_name()
10997 * would also work, but some plugins depend on the name being
10998 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010999 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020011000 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011001 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011002 if (rettv->vval.v_string != NULL)
11003 {
11004 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011005 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011006 }
11007 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011008 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011009 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011010 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011011 }
11012}
11013
11014/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011015 * "garbagecollect()" function
11016 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011017 static void
11018f_garbagecollect(argvars, rettv)
11019 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011020 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011021{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011022 /* This is postponed until we are back at the toplevel, because we may be
11023 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11024 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011025
11026 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11027 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011028}
11029
11030/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011031 * "get()" function
11032 */
11033 static void
11034f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011035 typval_T *argvars;
11036 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011037{
Bram Moolenaar33570922005-01-25 22:26:29 +000011038 listitem_T *li;
11039 list_T *l;
11040 dictitem_T *di;
11041 dict_T *d;
11042 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011043
Bram Moolenaare9a41262005-01-15 22:18:47 +000011044 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011045 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011046 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011047 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011048 int error = FALSE;
11049
11050 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11051 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011052 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011053 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011054 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011055 else if (argvars[0].v_type == VAR_DICT)
11056 {
11057 if ((d = argvars[0].vval.v_dict) != NULL)
11058 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011059 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011060 if (di != NULL)
11061 tv = &di->di_tv;
11062 }
11063 }
11064 else
11065 EMSG2(_(e_listdictarg), "get()");
11066
11067 if (tv == NULL)
11068 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011069 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011070 copy_tv(&argvars[2], rettv);
11071 }
11072 else
11073 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011074}
11075
Bram Moolenaar342337a2005-07-21 21:11:17 +000011076static 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 +000011077
11078/*
11079 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011080 * Return a range (from start to end) of lines in rettv from the specified
11081 * buffer.
11082 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011083 */
11084 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011085get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011086 buf_T *buf;
11087 linenr_T start;
11088 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011089 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011090 typval_T *rettv;
11091{
11092 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011093
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011094 if (retlist && rettv_list_alloc(rettv) == FAIL)
11095 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011096
11097 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11098 return;
11099
11100 if (!retlist)
11101 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011102 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11103 p = ml_get_buf(buf, start, FALSE);
11104 else
11105 p = (char_u *)"";
11106
11107 rettv->v_type = VAR_STRING;
11108 rettv->vval.v_string = vim_strsave(p);
11109 }
11110 else
11111 {
11112 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011113 return;
11114
11115 if (start < 1)
11116 start = 1;
11117 if (end > buf->b_ml.ml_line_count)
11118 end = buf->b_ml.ml_line_count;
11119 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011120 if (list_append_string(rettv->vval.v_list,
11121 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011122 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011123 }
11124}
11125
11126/*
11127 * "getbufline()" function
11128 */
11129 static void
11130f_getbufline(argvars, rettv)
11131 typval_T *argvars;
11132 typval_T *rettv;
11133{
11134 linenr_T lnum;
11135 linenr_T end;
11136 buf_T *buf;
11137
11138 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11139 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011140 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011141 --emsg_off;
11142
Bram Moolenaar661b1822005-07-28 22:36:45 +000011143 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011144 if (argvars[2].v_type == VAR_UNKNOWN)
11145 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011146 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011147 end = get_tv_lnum_buf(&argvars[2], buf);
11148
Bram Moolenaar342337a2005-07-21 21:11:17 +000011149 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011150}
11151
Bram Moolenaar0d660222005-01-07 21:51:51 +000011152/*
11153 * "getbufvar()" function
11154 */
11155 static void
11156f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011157 typval_T *argvars;
11158 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011159{
11160 buf_T *buf;
11161 buf_T *save_curbuf;
11162 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011163 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011164 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11167 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011168 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011169 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011170
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011171 rettv->v_type = VAR_STRING;
11172 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011173
11174 if (buf != NULL && varname != NULL)
11175 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011176 /* set curbuf to be our buf, temporarily */
11177 save_curbuf = curbuf;
11178 curbuf = buf;
11179
Bram Moolenaar0d660222005-01-07 21:51:51 +000011180 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011181 {
11182 if (get_option_tv(&varname, rettv, TRUE) == OK)
11183 done = TRUE;
11184 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011185 else if (STRCMP(varname, "changedtick") == 0)
11186 {
11187 rettv->v_type = VAR_NUMBER;
11188 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011189 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011190 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011191 else
11192 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011193 /* Look up the variable. */
11194 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11195 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11196 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011197 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011198 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011199 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011200 done = TRUE;
11201 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011202 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011203
11204 /* restore previous notion of curbuf */
11205 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011206 }
11207
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011208 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11209 /* use the default value */
11210 copy_tv(&argvars[2], rettv);
11211
Bram Moolenaar0d660222005-01-07 21:51:51 +000011212 --emsg_off;
11213}
11214
11215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011216 * "getchar()" function
11217 */
11218 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011219f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011220 typval_T *argvars;
11221 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011222{
11223 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011224 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011225
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011226 /* Position the cursor. Needed after a message that ends in a space. */
11227 windgoto(msg_row, msg_col);
11228
Bram Moolenaar071d4272004-06-13 20:20:40 +000011229 ++no_mapping;
11230 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011231 for (;;)
11232 {
11233 if (argvars[0].v_type == VAR_UNKNOWN)
11234 /* getchar(): blocking wait. */
11235 n = safe_vgetc();
11236 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11237 /* getchar(1): only check if char avail */
11238 n = vpeekc();
11239 else if (error || vpeekc() == NUL)
11240 /* illegal argument or getchar(0) and no char avail: return zero */
11241 n = 0;
11242 else
11243 /* getchar(0) and char avail: return char */
11244 n = safe_vgetc();
11245 if (n == K_IGNORE)
11246 continue;
11247 break;
11248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011249 --no_mapping;
11250 --allow_keys;
11251
Bram Moolenaar219b8702006-11-01 14:32:36 +000011252 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11253 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11254 vimvars[VV_MOUSE_COL].vv_nr = 0;
11255
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011256 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 if (IS_SPECIAL(n) || mod_mask != 0)
11258 {
11259 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11260 int i = 0;
11261
11262 /* Turn a special key into three bytes, plus modifier. */
11263 if (mod_mask != 0)
11264 {
11265 temp[i++] = K_SPECIAL;
11266 temp[i++] = KS_MODIFIER;
11267 temp[i++] = mod_mask;
11268 }
11269 if (IS_SPECIAL(n))
11270 {
11271 temp[i++] = K_SPECIAL;
11272 temp[i++] = K_SECOND(n);
11273 temp[i++] = K_THIRD(n);
11274 }
11275#ifdef FEAT_MBYTE
11276 else if (has_mbyte)
11277 i += (*mb_char2bytes)(n, temp + i);
11278#endif
11279 else
11280 temp[i++] = n;
11281 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011282 rettv->v_type = VAR_STRING;
11283 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011284
11285#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011286 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011287 {
11288 int row = mouse_row;
11289 int col = mouse_col;
11290 win_T *win;
11291 linenr_T lnum;
11292# ifdef FEAT_WINDOWS
11293 win_T *wp;
11294# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011295 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011296
11297 if (row >= 0 && col >= 0)
11298 {
11299 /* Find the window at the mouse coordinates and compute the
11300 * text position. */
11301 win = mouse_find_win(&row, &col);
11302 (void)mouse_comp_pos(win, &row, &col, &lnum);
11303# ifdef FEAT_WINDOWS
11304 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011305 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011306# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011307 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011308 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11309 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11310 }
11311 }
11312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011313 }
11314}
11315
11316/*
11317 * "getcharmod()" function
11318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011319 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011320f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011321 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011322 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011323{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011324 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325}
11326
11327/*
11328 * "getcmdline()" function
11329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011330 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011331f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011332 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011333 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011334{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011335 rettv->v_type = VAR_STRING;
11336 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011337}
11338
11339/*
11340 * "getcmdpos()" function
11341 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011342 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011343f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011344 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011345 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011346{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011347 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348}
11349
11350/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011351 * "getcmdtype()" function
11352 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011353 static void
11354f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011355 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011356 typval_T *rettv;
11357{
11358 rettv->v_type = VAR_STRING;
11359 rettv->vval.v_string = alloc(2);
11360 if (rettv->vval.v_string != NULL)
11361 {
11362 rettv->vval.v_string[0] = get_cmdline_type();
11363 rettv->vval.v_string[1] = NUL;
11364 }
11365}
11366
11367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 * "getcwd()" function
11369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011371f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011372 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011373 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011375 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011378 rettv->vval.v_string = NULL;
11379 cwd = alloc(MAXPATHL);
11380 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011382 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11383 {
11384 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011386 if (rettv->vval.v_string != NULL)
11387 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011389 }
11390 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391 }
11392}
11393
11394/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011395 * "getfontname()" function
11396 */
11397 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011398f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011399 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011400 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011401{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011402 rettv->v_type = VAR_STRING;
11403 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011404#ifdef FEAT_GUI
11405 if (gui.in_use)
11406 {
11407 GuiFont font;
11408 char_u *name = NULL;
11409
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011410 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011411 {
11412 /* Get the "Normal" font. Either the name saved by
11413 * hl_set_font_name() or from the font ID. */
11414 font = gui.norm_font;
11415 name = hl_get_font_name();
11416 }
11417 else
11418 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011419 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011420 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11421 return;
11422 font = gui_mch_get_font(name, FALSE);
11423 if (font == NOFONT)
11424 return; /* Invalid font name, return empty string. */
11425 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011427 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011428 gui_mch_free_font(font);
11429 }
11430#endif
11431}
11432
11433/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011434 * "getfperm({fname})" function
11435 */
11436 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011438 typval_T *argvars;
11439 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011440{
11441 char_u *fname;
11442 struct stat st;
11443 char_u *perm = NULL;
11444 char_u flags[] = "rwx";
11445 int i;
11446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011447 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011448
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011449 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011450 if (mch_stat((char *)fname, &st) >= 0)
11451 {
11452 perm = vim_strsave((char_u *)"---------");
11453 if (perm != NULL)
11454 {
11455 for (i = 0; i < 9; i++)
11456 {
11457 if (st.st_mode & (1 << (8 - i)))
11458 perm[i] = flags[i % 3];
11459 }
11460 }
11461 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011462 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011463}
11464
11465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011466 * "getfsize({fname})" function
11467 */
11468 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011469f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011470 typval_T *argvars;
11471 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011472{
11473 char_u *fname;
11474 struct stat st;
11475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011476 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011478 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011479
11480 if (mch_stat((char *)fname, &st) >= 0)
11481 {
11482 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011483 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011484 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011486 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011487
11488 /* non-perfect check for overflow */
11489 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11490 rettv->vval.v_number = -2;
11491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011492 }
11493 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011494 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011495}
11496
11497/*
11498 * "getftime({fname})" function
11499 */
11500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011501f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011502 typval_T *argvars;
11503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011504{
11505 char_u *fname;
11506 struct stat st;
11507
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011508 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011509
11510 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011511 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011512 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011513 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011514}
11515
11516/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011517 * "getftype({fname})" function
11518 */
11519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011520f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011521 typval_T *argvars;
11522 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011523{
11524 char_u *fname;
11525 struct stat st;
11526 char_u *type = NULL;
11527 char *t;
11528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011529 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011531 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011532 if (mch_lstat((char *)fname, &st) >= 0)
11533 {
11534#ifdef S_ISREG
11535 if (S_ISREG(st.st_mode))
11536 t = "file";
11537 else if (S_ISDIR(st.st_mode))
11538 t = "dir";
11539# ifdef S_ISLNK
11540 else if (S_ISLNK(st.st_mode))
11541 t = "link";
11542# endif
11543# ifdef S_ISBLK
11544 else if (S_ISBLK(st.st_mode))
11545 t = "bdev";
11546# endif
11547# ifdef S_ISCHR
11548 else if (S_ISCHR(st.st_mode))
11549 t = "cdev";
11550# endif
11551# ifdef S_ISFIFO
11552 else if (S_ISFIFO(st.st_mode))
11553 t = "fifo";
11554# endif
11555# ifdef S_ISSOCK
11556 else if (S_ISSOCK(st.st_mode))
11557 t = "fifo";
11558# endif
11559 else
11560 t = "other";
11561#else
11562# ifdef S_IFMT
11563 switch (st.st_mode & S_IFMT)
11564 {
11565 case S_IFREG: t = "file"; break;
11566 case S_IFDIR: t = "dir"; break;
11567# ifdef S_IFLNK
11568 case S_IFLNK: t = "link"; break;
11569# endif
11570# ifdef S_IFBLK
11571 case S_IFBLK: t = "bdev"; break;
11572# endif
11573# ifdef S_IFCHR
11574 case S_IFCHR: t = "cdev"; break;
11575# endif
11576# ifdef S_IFIFO
11577 case S_IFIFO: t = "fifo"; break;
11578# endif
11579# ifdef S_IFSOCK
11580 case S_IFSOCK: t = "socket"; break;
11581# endif
11582 default: t = "other";
11583 }
11584# else
11585 if (mch_isdir(fname))
11586 t = "dir";
11587 else
11588 t = "file";
11589# endif
11590#endif
11591 type = vim_strsave((char_u *)t);
11592 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011593 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011594}
11595
11596/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011597 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011598 */
11599 static void
11600f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011601 typval_T *argvars;
11602 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011603{
11604 linenr_T lnum;
11605 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011606 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011607
11608 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011609 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011610 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011611 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011612 retlist = FALSE;
11613 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011614 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011615 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011616 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011617 retlist = TRUE;
11618 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011619
Bram Moolenaar342337a2005-07-21 21:11:17 +000011620 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011621}
11622
11623/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011624 * "getmatches()" function
11625 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011626 static void
11627f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011628 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011629 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011630{
11631#ifdef FEAT_SEARCH_EXTRA
11632 dict_T *dict;
11633 matchitem_T *cur = curwin->w_match_head;
11634
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011635 if (rettv_list_alloc(rettv) == OK)
11636 {
11637 while (cur != NULL)
11638 {
11639 dict = dict_alloc();
11640 if (dict == NULL)
11641 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011642 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11643 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11644 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11645 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11646 list_append_dict(rettv->vval.v_list, dict);
11647 cur = cur->next;
11648 }
11649 }
11650#endif
11651}
11652
11653/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011654 * "getpid()" function
11655 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011656 static void
11657f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011658 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011659 typval_T *rettv;
11660{
11661 rettv->vval.v_number = mch_get_pid();
11662}
11663
11664/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011665 * "getpos(string)" function
11666 */
11667 static void
11668f_getpos(argvars, rettv)
11669 typval_T *argvars;
11670 typval_T *rettv;
11671{
11672 pos_T *fp;
11673 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011674 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011675
11676 if (rettv_list_alloc(rettv) == OK)
11677 {
11678 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011679 fp = var2fpos(&argvars[0], TRUE, &fnum);
11680 if (fnum != -1)
11681 list_append_number(l, (varnumber_T)fnum);
11682 else
11683 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011684 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11685 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011686 list_append_number(l, (fp != NULL)
11687 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011688 : (varnumber_T)0);
11689 list_append_number(l,
11690#ifdef FEAT_VIRTUALEDIT
11691 (fp != NULL) ? (varnumber_T)fp->coladd :
11692#endif
11693 (varnumber_T)0);
11694 }
11695 else
11696 rettv->vval.v_number = FALSE;
11697}
11698
11699/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011700 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011701 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011702 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011703f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011704 typval_T *argvars UNUSED;
11705 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011706{
11707#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011708 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011709#endif
11710
Bram Moolenaar2641f772005-03-25 21:58:17 +000011711#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011712 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011713 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011714 wp = NULL;
11715 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11716 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011717 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011718 if (wp == NULL)
11719 return;
11720 }
11721
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011722 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011723 }
11724#endif
11725}
11726
11727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 * "getreg()" function
11729 */
11730 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011731f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011732 typval_T *argvars;
11733 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011734{
11735 char_u *strregname;
11736 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011737 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011738 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011740 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011742 strregname = get_tv_string_chk(&argvars[0]);
11743 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011744 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011745 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011748 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 regname = (strregname == NULL ? '"' : *strregname);
11750 if (regname == 0)
11751 regname = '"';
11752
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011754 rettv->vval.v_string = error ? NULL :
11755 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011756}
11757
11758/*
11759 * "getregtype()" function
11760 */
11761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011762f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011763 typval_T *argvars;
11764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011765{
11766 char_u *strregname;
11767 int regname;
11768 char_u buf[NUMBUFLEN + 2];
11769 long reglen = 0;
11770
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011771 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011772 {
11773 strregname = get_tv_string_chk(&argvars[0]);
11774 if (strregname == NULL) /* type error; errmsg already given */
11775 {
11776 rettv->v_type = VAR_STRING;
11777 rettv->vval.v_string = NULL;
11778 return;
11779 }
11780 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011781 else
11782 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011783 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011784
11785 regname = (strregname == NULL ? '"' : *strregname);
11786 if (regname == 0)
11787 regname = '"';
11788
11789 buf[0] = NUL;
11790 buf[1] = NUL;
11791 switch (get_reg_type(regname, &reglen))
11792 {
11793 case MLINE: buf[0] = 'V'; break;
11794 case MCHAR: buf[0] = 'v'; break;
11795#ifdef FEAT_VISUAL
11796 case MBLOCK:
11797 buf[0] = Ctrl_V;
11798 sprintf((char *)buf + 1, "%ld", reglen + 1);
11799 break;
11800#endif
11801 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011802 rettv->v_type = VAR_STRING;
11803 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011804}
11805
11806/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011807 * "gettabvar()" function
11808 */
11809 static void
11810f_gettabvar(argvars, rettv)
11811 typval_T *argvars;
11812 typval_T *rettv;
11813{
11814 tabpage_T *tp;
11815 dictitem_T *v;
11816 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011817 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011818
11819 rettv->v_type = VAR_STRING;
11820 rettv->vval.v_string = NULL;
11821
11822 varname = get_tv_string_chk(&argvars[1]);
11823 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11824 if (tp != NULL && varname != NULL)
11825 {
11826 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011827 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011828 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011829 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011830 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011831 done = TRUE;
11832 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011833 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011834
11835 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011836 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011837}
11838
11839/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011840 * "gettabwinvar()" function
11841 */
11842 static void
11843f_gettabwinvar(argvars, rettv)
11844 typval_T *argvars;
11845 typval_T *rettv;
11846{
11847 getwinvar(argvars, rettv, 1);
11848}
11849
11850/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011851 * "getwinposx()" function
11852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011853 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011854f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011855 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011856 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011857{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011858 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011859#ifdef FEAT_GUI
11860 if (gui.in_use)
11861 {
11862 int x, y;
11863
11864 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011866 }
11867#endif
11868}
11869
11870/*
11871 * "getwinposy()" function
11872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011873 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011874f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011875 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011876 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011878 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879#ifdef FEAT_GUI
11880 if (gui.in_use)
11881 {
11882 int x, y;
11883
11884 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011885 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886 }
11887#endif
11888}
11889
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011890/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011891 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011892 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011893 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011894find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011895 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011896 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011897{
11898#ifdef FEAT_WINDOWS
11899 win_T *wp;
11900#endif
11901 int nr;
11902
11903 nr = get_tv_number_chk(vp, NULL);
11904
11905#ifdef FEAT_WINDOWS
11906 if (nr < 0)
11907 return NULL;
11908 if (nr == 0)
11909 return curwin;
11910
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011911 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11912 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011913 if (--nr <= 0)
11914 break;
11915 return wp;
11916#else
11917 if (nr == 0 || nr == 1)
11918 return curwin;
11919 return NULL;
11920#endif
11921}
11922
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923/*
11924 * "getwinvar()" function
11925 */
11926 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011927f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011928 typval_T *argvars;
11929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011931 getwinvar(argvars, rettv, 0);
11932}
11933
11934/*
11935 * getwinvar() and gettabwinvar()
11936 */
11937 static void
11938getwinvar(argvars, rettv, off)
11939 typval_T *argvars;
11940 typval_T *rettv;
11941 int off; /* 1 for gettabwinvar() */
11942{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011943 win_T *win, *oldcurwin;
11944 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011945 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011946 tabpage_T *tp = NULL;
11947 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011948 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011949
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011950#ifdef FEAT_WINDOWS
11951 if (off == 1)
11952 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11953 else
11954 tp = curtab;
11955#endif
11956 win = find_win_by_nr(&argvars[off], tp);
11957 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011958 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011959
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011960 rettv->v_type = VAR_STRING;
11961 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011962
11963 if (win != NULL && varname != NULL)
11964 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011965 /* Set curwin to be our win, temporarily. Also set the tabpage,
11966 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020011967 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011968
Bram Moolenaar071d4272004-06-13 20:20:40 +000011969 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011970 {
11971 if (get_option_tv(&varname, rettv, 1) == OK)
11972 done = TRUE;
11973 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011974 else
11975 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011976 /* Look up the variable. */
11977 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11978 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011979 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011980 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011981 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011982 done = TRUE;
11983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011984 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011985
11986 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020011987 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011988 }
11989
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011990 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11991 /* use the default return value */
11992 copy_tv(&argvars[off + 2], rettv);
11993
Bram Moolenaar071d4272004-06-13 20:20:40 +000011994 --emsg_off;
11995}
11996
11997/*
11998 * "glob()" function
11999 */
12000 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012001f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012002 typval_T *argvars;
12003 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012004{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012005 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012006 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012007 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012008
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012009 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012010 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012011 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012012 if (argvars[1].v_type != VAR_UNKNOWN)
12013 {
12014 if (get_tv_number_chk(&argvars[1], &error))
12015 options |= WILD_KEEP_ALL;
12016 if (argvars[2].v_type != VAR_UNKNOWN
12017 && get_tv_number_chk(&argvars[2], &error))
12018 {
12019 rettv->v_type = VAR_LIST;
12020 rettv->vval.v_list = NULL;
12021 }
12022 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012023 if (!error)
12024 {
12025 ExpandInit(&xpc);
12026 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012027 if (p_wic)
12028 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012029 if (rettv->v_type == VAR_STRING)
12030 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012031 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012032 else if (rettv_list_alloc(rettv) != FAIL)
12033 {
12034 int i;
12035
12036 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12037 NULL, options, WILD_ALL_KEEP);
12038 for (i = 0; i < xpc.xp_numfiles; i++)
12039 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12040
12041 ExpandCleanup(&xpc);
12042 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012043 }
12044 else
12045 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012046}
12047
12048/*
12049 * "globpath()" function
12050 */
12051 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012052f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012053 typval_T *argvars;
12054 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012055{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012056 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012057 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012058 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012059 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012060
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012061 /* When the optional second argument is non-zero, don't remove matches
12062 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12063 if (argvars[2].v_type != VAR_UNKNOWN
12064 && get_tv_number_chk(&argvars[2], &error))
12065 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012066 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012067 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012068 rettv->vval.v_string = NULL;
12069 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012070 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12071 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012072}
12073
12074/*
12075 * "has()" function
12076 */
12077 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012078f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012079 typval_T *argvars;
12080 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012081{
12082 int i;
12083 char_u *name;
12084 int n = FALSE;
12085 static char *(has_list[]) =
12086 {
12087#ifdef AMIGA
12088 "amiga",
12089# ifdef FEAT_ARP
12090 "arp",
12091# endif
12092#endif
12093#ifdef __BEOS__
12094 "beos",
12095#endif
12096#ifdef MSDOS
12097# ifdef DJGPP
12098 "dos32",
12099# else
12100 "dos16",
12101# endif
12102#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012103#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012104 "mac",
12105#endif
12106#if defined(MACOS_X_UNIX)
12107 "macunix",
12108#endif
12109#ifdef OS2
12110 "os2",
12111#endif
12112#ifdef __QNX__
12113 "qnx",
12114#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012115#ifdef UNIX
12116 "unix",
12117#endif
12118#ifdef VMS
12119 "vms",
12120#endif
12121#ifdef WIN16
12122 "win16",
12123#endif
12124#ifdef WIN32
12125 "win32",
12126#endif
12127#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12128 "win32unix",
12129#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012130#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 "win64",
12132#endif
12133#ifdef EBCDIC
12134 "ebcdic",
12135#endif
12136#ifndef CASE_INSENSITIVE_FILENAME
12137 "fname_case",
12138#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012139#ifdef HAVE_ACL
12140 "acl",
12141#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012142#ifdef FEAT_ARABIC
12143 "arabic",
12144#endif
12145#ifdef FEAT_AUTOCMD
12146 "autocmd",
12147#endif
12148#ifdef FEAT_BEVAL
12149 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012150# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12151 "balloon_multiline",
12152# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012153#endif
12154#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12155 "builtin_terms",
12156# ifdef ALL_BUILTIN_TCAPS
12157 "all_builtin_terms",
12158# endif
12159#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012160#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12161 || defined(FEAT_GUI_W32) \
12162 || defined(FEAT_GUI_MOTIF))
12163 "browsefilter",
12164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165#ifdef FEAT_BYTEOFF
12166 "byte_offset",
12167#endif
12168#ifdef FEAT_CINDENT
12169 "cindent",
12170#endif
12171#ifdef FEAT_CLIENTSERVER
12172 "clientserver",
12173#endif
12174#ifdef FEAT_CLIPBOARD
12175 "clipboard",
12176#endif
12177#ifdef FEAT_CMDL_COMPL
12178 "cmdline_compl",
12179#endif
12180#ifdef FEAT_CMDHIST
12181 "cmdline_hist",
12182#endif
12183#ifdef FEAT_COMMENTS
12184 "comments",
12185#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012186#ifdef FEAT_CONCEAL
12187 "conceal",
12188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012189#ifdef FEAT_CRYPT
12190 "cryptv",
12191#endif
12192#ifdef FEAT_CSCOPE
12193 "cscope",
12194#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012195#ifdef FEAT_CURSORBIND
12196 "cursorbind",
12197#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012198#ifdef CURSOR_SHAPE
12199 "cursorshape",
12200#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012201#ifdef DEBUG
12202 "debug",
12203#endif
12204#ifdef FEAT_CON_DIALOG
12205 "dialog_con",
12206#endif
12207#ifdef FEAT_GUI_DIALOG
12208 "dialog_gui",
12209#endif
12210#ifdef FEAT_DIFF
12211 "diff",
12212#endif
12213#ifdef FEAT_DIGRAPHS
12214 "digraphs",
12215#endif
12216#ifdef FEAT_DND
12217 "dnd",
12218#endif
12219#ifdef FEAT_EMACS_TAGS
12220 "emacs_tags",
12221#endif
12222 "eval", /* always present, of course! */
12223#ifdef FEAT_EX_EXTRA
12224 "ex_extra",
12225#endif
12226#ifdef FEAT_SEARCH_EXTRA
12227 "extra_search",
12228#endif
12229#ifdef FEAT_FKMAP
12230 "farsi",
12231#endif
12232#ifdef FEAT_SEARCHPATH
12233 "file_in_path",
12234#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012235#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012236 "filterpipe",
12237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238#ifdef FEAT_FIND_ID
12239 "find_in_path",
12240#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012241#ifdef FEAT_FLOAT
12242 "float",
12243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012244#ifdef FEAT_FOLDING
12245 "folding",
12246#endif
12247#ifdef FEAT_FOOTER
12248 "footer",
12249#endif
12250#if !defined(USE_SYSTEM) && defined(UNIX)
12251 "fork",
12252#endif
12253#ifdef FEAT_GETTEXT
12254 "gettext",
12255#endif
12256#ifdef FEAT_GUI
12257 "gui",
12258#endif
12259#ifdef FEAT_GUI_ATHENA
12260# ifdef FEAT_GUI_NEXTAW
12261 "gui_neXtaw",
12262# else
12263 "gui_athena",
12264# endif
12265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266#ifdef FEAT_GUI_GTK
12267 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012269#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012270#ifdef FEAT_GUI_GNOME
12271 "gui_gnome",
12272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012273#ifdef FEAT_GUI_MAC
12274 "gui_mac",
12275#endif
12276#ifdef FEAT_GUI_MOTIF
12277 "gui_motif",
12278#endif
12279#ifdef FEAT_GUI_PHOTON
12280 "gui_photon",
12281#endif
12282#ifdef FEAT_GUI_W16
12283 "gui_win16",
12284#endif
12285#ifdef FEAT_GUI_W32
12286 "gui_win32",
12287#endif
12288#ifdef FEAT_HANGULIN
12289 "hangul_input",
12290#endif
12291#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12292 "iconv",
12293#endif
12294#ifdef FEAT_INS_EXPAND
12295 "insert_expand",
12296#endif
12297#ifdef FEAT_JUMPLIST
12298 "jumplist",
12299#endif
12300#ifdef FEAT_KEYMAP
12301 "keymap",
12302#endif
12303#ifdef FEAT_LANGMAP
12304 "langmap",
12305#endif
12306#ifdef FEAT_LIBCALL
12307 "libcall",
12308#endif
12309#ifdef FEAT_LINEBREAK
12310 "linebreak",
12311#endif
12312#ifdef FEAT_LISP
12313 "lispindent",
12314#endif
12315#ifdef FEAT_LISTCMDS
12316 "listcmds",
12317#endif
12318#ifdef FEAT_LOCALMAP
12319 "localmap",
12320#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012321#ifdef FEAT_LUA
12322# ifndef DYNAMIC_LUA
12323 "lua",
12324# endif
12325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012326#ifdef FEAT_MENU
12327 "menu",
12328#endif
12329#ifdef FEAT_SESSION
12330 "mksession",
12331#endif
12332#ifdef FEAT_MODIFY_FNAME
12333 "modify_fname",
12334#endif
12335#ifdef FEAT_MOUSE
12336 "mouse",
12337#endif
12338#ifdef FEAT_MOUSESHAPE
12339 "mouseshape",
12340#endif
12341#if defined(UNIX) || defined(VMS)
12342# ifdef FEAT_MOUSE_DEC
12343 "mouse_dec",
12344# endif
12345# ifdef FEAT_MOUSE_GPM
12346 "mouse_gpm",
12347# endif
12348# ifdef FEAT_MOUSE_JSB
12349 "mouse_jsbterm",
12350# endif
12351# ifdef FEAT_MOUSE_NET
12352 "mouse_netterm",
12353# endif
12354# ifdef FEAT_MOUSE_PTERM
12355 "mouse_pterm",
12356# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012357# ifdef FEAT_MOUSE_SGR
12358 "mouse_sgr",
12359# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012360# ifdef FEAT_SYSMOUSE
12361 "mouse_sysmouse",
12362# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012363# ifdef FEAT_MOUSE_URXVT
12364 "mouse_urxvt",
12365# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012366# ifdef FEAT_MOUSE_XTERM
12367 "mouse_xterm",
12368# endif
12369#endif
12370#ifdef FEAT_MBYTE
12371 "multi_byte",
12372#endif
12373#ifdef FEAT_MBYTE_IME
12374 "multi_byte_ime",
12375#endif
12376#ifdef FEAT_MULTI_LANG
12377 "multi_lang",
12378#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012379#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012380#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012381 "mzscheme",
12382#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012383#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384#ifdef FEAT_OLE
12385 "ole",
12386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012387#ifdef FEAT_PATH_EXTRA
12388 "path_extra",
12389#endif
12390#ifdef FEAT_PERL
12391#ifndef DYNAMIC_PERL
12392 "perl",
12393#endif
12394#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012395#ifdef FEAT_PERSISTENT_UNDO
12396 "persistent_undo",
12397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012398#ifdef FEAT_PYTHON
12399#ifndef DYNAMIC_PYTHON
12400 "python",
12401#endif
12402#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012403#ifdef FEAT_PYTHON3
12404#ifndef DYNAMIC_PYTHON3
12405 "python3",
12406#endif
12407#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012408#ifdef FEAT_POSTSCRIPT
12409 "postscript",
12410#endif
12411#ifdef FEAT_PRINTER
12412 "printer",
12413#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012414#ifdef FEAT_PROFILE
12415 "profile",
12416#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012417#ifdef FEAT_RELTIME
12418 "reltime",
12419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012420#ifdef FEAT_QUICKFIX
12421 "quickfix",
12422#endif
12423#ifdef FEAT_RIGHTLEFT
12424 "rightleft",
12425#endif
12426#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12427 "ruby",
12428#endif
12429#ifdef FEAT_SCROLLBIND
12430 "scrollbind",
12431#endif
12432#ifdef FEAT_CMDL_INFO
12433 "showcmd",
12434 "cmdline_info",
12435#endif
12436#ifdef FEAT_SIGNS
12437 "signs",
12438#endif
12439#ifdef FEAT_SMARTINDENT
12440 "smartindent",
12441#endif
12442#ifdef FEAT_SNIFF
12443 "sniff",
12444#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012445#ifdef STARTUPTIME
12446 "startuptime",
12447#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012448#ifdef FEAT_STL_OPT
12449 "statusline",
12450#endif
12451#ifdef FEAT_SUN_WORKSHOP
12452 "sun_workshop",
12453#endif
12454#ifdef FEAT_NETBEANS_INTG
12455 "netbeans_intg",
12456#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012457#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012458 "spell",
12459#endif
12460#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461 "syntax",
12462#endif
12463#if defined(USE_SYSTEM) || !defined(UNIX)
12464 "system",
12465#endif
12466#ifdef FEAT_TAG_BINS
12467 "tag_binary",
12468#endif
12469#ifdef FEAT_TAG_OLDSTATIC
12470 "tag_old_static",
12471#endif
12472#ifdef FEAT_TAG_ANYWHITE
12473 "tag_any_white",
12474#endif
12475#ifdef FEAT_TCL
12476# ifndef DYNAMIC_TCL
12477 "tcl",
12478# endif
12479#endif
12480#ifdef TERMINFO
12481 "terminfo",
12482#endif
12483#ifdef FEAT_TERMRESPONSE
12484 "termresponse",
12485#endif
12486#ifdef FEAT_TEXTOBJ
12487 "textobjects",
12488#endif
12489#ifdef HAVE_TGETENT
12490 "tgetent",
12491#endif
12492#ifdef FEAT_TITLE
12493 "title",
12494#endif
12495#ifdef FEAT_TOOLBAR
12496 "toolbar",
12497#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012498#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12499 "unnamedplus",
12500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012501#ifdef FEAT_USR_CMDS
12502 "user-commands", /* was accidentally included in 5.4 */
12503 "user_commands",
12504#endif
12505#ifdef FEAT_VIMINFO
12506 "viminfo",
12507#endif
12508#ifdef FEAT_VERTSPLIT
12509 "vertsplit",
12510#endif
12511#ifdef FEAT_VIRTUALEDIT
12512 "virtualedit",
12513#endif
12514#ifdef FEAT_VISUAL
12515 "visual",
12516#endif
12517#ifdef FEAT_VISUALEXTRA
12518 "visualextra",
12519#endif
12520#ifdef FEAT_VREPLACE
12521 "vreplace",
12522#endif
12523#ifdef FEAT_WILDIGN
12524 "wildignore",
12525#endif
12526#ifdef FEAT_WILDMENU
12527 "wildmenu",
12528#endif
12529#ifdef FEAT_WINDOWS
12530 "windows",
12531#endif
12532#ifdef FEAT_WAK
12533 "winaltkeys",
12534#endif
12535#ifdef FEAT_WRITEBACKUP
12536 "writebackup",
12537#endif
12538#ifdef FEAT_XIM
12539 "xim",
12540#endif
12541#ifdef FEAT_XFONTSET
12542 "xfontset",
12543#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012544#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012545 "xpm",
12546 "xpm_w32", /* for backward compatibility */
12547#else
12548# if defined(HAVE_XPM)
12549 "xpm",
12550# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012551#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012552#ifdef USE_XSMP
12553 "xsmp",
12554#endif
12555#ifdef USE_XSMP_INTERACT
12556 "xsmp_interact",
12557#endif
12558#ifdef FEAT_XCLIPBOARD
12559 "xterm_clipboard",
12560#endif
12561#ifdef FEAT_XTERM_SAVE
12562 "xterm_save",
12563#endif
12564#if defined(UNIX) && defined(FEAT_X11)
12565 "X11",
12566#endif
12567 NULL
12568 };
12569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012570 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571 for (i = 0; has_list[i] != NULL; ++i)
12572 if (STRICMP(name, has_list[i]) == 0)
12573 {
12574 n = TRUE;
12575 break;
12576 }
12577
12578 if (n == FALSE)
12579 {
12580 if (STRNICMP(name, "patch", 5) == 0)
12581 n = has_patch(atoi((char *)name + 5));
12582 else if (STRICMP(name, "vim_starting") == 0)
12583 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012584#ifdef FEAT_MBYTE
12585 else if (STRICMP(name, "multi_byte_encoding") == 0)
12586 n = has_mbyte;
12587#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012588#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12589 else if (STRICMP(name, "balloon_multiline") == 0)
12590 n = multiline_balloon_available();
12591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592#ifdef DYNAMIC_TCL
12593 else if (STRICMP(name, "tcl") == 0)
12594 n = tcl_enabled(FALSE);
12595#endif
12596#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12597 else if (STRICMP(name, "iconv") == 0)
12598 n = iconv_enabled(FALSE);
12599#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012600#ifdef DYNAMIC_LUA
12601 else if (STRICMP(name, "lua") == 0)
12602 n = lua_enabled(FALSE);
12603#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012604#ifdef DYNAMIC_MZSCHEME
12605 else if (STRICMP(name, "mzscheme") == 0)
12606 n = mzscheme_enabled(FALSE);
12607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608#ifdef DYNAMIC_RUBY
12609 else if (STRICMP(name, "ruby") == 0)
12610 n = ruby_enabled(FALSE);
12611#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012612#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613#ifdef DYNAMIC_PYTHON
12614 else if (STRICMP(name, "python") == 0)
12615 n = python_enabled(FALSE);
12616#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012617#endif
12618#ifdef FEAT_PYTHON3
12619#ifdef DYNAMIC_PYTHON3
12620 else if (STRICMP(name, "python3") == 0)
12621 n = python3_enabled(FALSE);
12622#endif
12623#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624#ifdef DYNAMIC_PERL
12625 else if (STRICMP(name, "perl") == 0)
12626 n = perl_enabled(FALSE);
12627#endif
12628#ifdef FEAT_GUI
12629 else if (STRICMP(name, "gui_running") == 0)
12630 n = (gui.in_use || gui.starting);
12631# ifdef FEAT_GUI_W32
12632 else if (STRICMP(name, "gui_win32s") == 0)
12633 n = gui_is_win32s();
12634# endif
12635# ifdef FEAT_BROWSE
12636 else if (STRICMP(name, "browse") == 0)
12637 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12638# endif
12639#endif
12640#ifdef FEAT_SYN_HL
12641 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012642 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012643#endif
12644#if defined(WIN3264)
12645 else if (STRICMP(name, "win95") == 0)
12646 n = mch_windows95();
12647#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012648#ifdef FEAT_NETBEANS_INTG
12649 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012650 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 }
12653
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012654 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012655}
12656
12657/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012658 * "has_key()" function
12659 */
12660 static void
12661f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012662 typval_T *argvars;
12663 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012664{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012665 if (argvars[0].v_type != VAR_DICT)
12666 {
12667 EMSG(_(e_dictreq));
12668 return;
12669 }
12670 if (argvars[0].vval.v_dict == NULL)
12671 return;
12672
12673 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012674 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012675}
12676
12677/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012678 * "haslocaldir()" function
12679 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012680 static void
12681f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012682 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012683 typval_T *rettv;
12684{
12685 rettv->vval.v_number = (curwin->w_localdir != NULL);
12686}
12687
12688/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689 * "hasmapto()" function
12690 */
12691 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012692f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012693 typval_T *argvars;
12694 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012695{
12696 char_u *name;
12697 char_u *mode;
12698 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012699 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012701 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012702 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012703 mode = (char_u *)"nvo";
12704 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012706 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012707 if (argvars[2].v_type != VAR_UNKNOWN)
12708 abbr = get_tv_number(&argvars[2]);
12709 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710
Bram Moolenaar2c932302006-03-18 21:42:09 +000012711 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715}
12716
12717/*
12718 * "histadd()" function
12719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012721f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012722 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724{
12725#ifdef FEAT_CMDHIST
12726 int histype;
12727 char_u *str;
12728 char_u buf[NUMBUFLEN];
12729#endif
12730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012731 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012732 if (check_restricted() || check_secure())
12733 return;
12734#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012735 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12736 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012737 if (histype >= 0)
12738 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012739 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012740 if (*str != NUL)
12741 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012742 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012743 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012744 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012745 return;
12746 }
12747 }
12748#endif
12749}
12750
12751/*
12752 * "histdel()" function
12753 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012754 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012755f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012756 typval_T *argvars UNUSED;
12757 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012758{
12759#ifdef FEAT_CMDHIST
12760 int n;
12761 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012762 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012763
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012764 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12765 if (str == NULL)
12766 n = 0;
12767 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012768 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012769 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012770 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012771 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012772 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012773 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012774 else
12775 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012776 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012777 get_tv_string_buf(&argvars[1], buf));
12778 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012779#endif
12780}
12781
12782/*
12783 * "histget()" function
12784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012786f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012787 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012789{
12790#ifdef FEAT_CMDHIST
12791 int type;
12792 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012793 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012795 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12796 if (str == NULL)
12797 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012799 {
12800 type = get_histtype(str);
12801 if (argvars[1].v_type == VAR_UNKNOWN)
12802 idx = get_history_idx(type);
12803 else
12804 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12805 /* -1 on type error */
12806 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012808#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012809 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012811 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012812}
12813
12814/*
12815 * "histnr()" function
12816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012817 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012819 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012820 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012821{
12822 int i;
12823
12824#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012825 char_u *history = get_tv_string_chk(&argvars[0]);
12826
12827 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012828 if (i >= HIST_CMD && i < HIST_COUNT)
12829 i = get_history_idx(i);
12830 else
12831#endif
12832 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834}
12835
12836/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012837 * "highlightID(name)" function
12838 */
12839 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012840f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012841 typval_T *argvars;
12842 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012843{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845}
12846
12847/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012848 * "highlight_exists()" function
12849 */
12850 static void
12851f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012852 typval_T *argvars;
12853 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012854{
12855 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12856}
12857
12858/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012859 * "hostname()" function
12860 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012861 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012862f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012863 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012864 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012865{
12866 char_u hostname[256];
12867
12868 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012869 rettv->v_type = VAR_STRING;
12870 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871}
12872
12873/*
12874 * iconv() function
12875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012876 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012877f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012878 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012879 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012880{
12881#ifdef FEAT_MBYTE
12882 char_u buf1[NUMBUFLEN];
12883 char_u buf2[NUMBUFLEN];
12884 char_u *from, *to, *str;
12885 vimconv_T vimconv;
12886#endif
12887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012888 rettv->v_type = VAR_STRING;
12889 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890
12891#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012892 str = get_tv_string(&argvars[0]);
12893 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12894 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012895 vimconv.vc_type = CONV_NONE;
12896 convert_setup(&vimconv, from, to);
12897
12898 /* If the encodings are equal, no conversion needed. */
12899 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012900 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012901 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012902 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903
12904 convert_setup(&vimconv, NULL, NULL);
12905 vim_free(from);
12906 vim_free(to);
12907#endif
12908}
12909
12910/*
12911 * "indent()" function
12912 */
12913 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012914f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012915 typval_T *argvars;
12916 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917{
12918 linenr_T lnum;
12919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012920 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012921 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012924 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925}
12926
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012927/*
12928 * "index()" function
12929 */
12930 static void
12931f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012932 typval_T *argvars;
12933 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012934{
Bram Moolenaar33570922005-01-25 22:26:29 +000012935 list_T *l;
12936 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012937 long idx = 0;
12938 int ic = FALSE;
12939
12940 rettv->vval.v_number = -1;
12941 if (argvars[0].v_type != VAR_LIST)
12942 {
12943 EMSG(_(e_listreq));
12944 return;
12945 }
12946 l = argvars[0].vval.v_list;
12947 if (l != NULL)
12948 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012949 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012950 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012951 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012952 int error = FALSE;
12953
Bram Moolenaar758711c2005-02-02 23:11:38 +000012954 /* Start at specified item. Use the cached index that list_find()
12955 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012956 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012957 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012958 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012959 ic = get_tv_number_chk(&argvars[3], &error);
12960 if (error)
12961 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012962 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012963
Bram Moolenaar758711c2005-02-02 23:11:38 +000012964 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012965 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012966 {
12967 rettv->vval.v_number = idx;
12968 break;
12969 }
12970 }
12971}
12972
Bram Moolenaar071d4272004-06-13 20:20:40 +000012973static int inputsecret_flag = 0;
12974
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012975static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12976
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012978 * This function is used by f_input() and f_inputdialog() functions. The third
12979 * argument to f_input() specifies the type of completion to use at the
12980 * prompt. The third argument to f_inputdialog() specifies the value to return
12981 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982 */
12983 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012984get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012985 typval_T *argvars;
12986 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012987 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012988{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012989 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990 char_u *p = NULL;
12991 int c;
12992 char_u buf[NUMBUFLEN];
12993 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012994 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012995 int xp_type = EXPAND_NOTHING;
12996 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012997
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012998 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012999 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000
13001#ifdef NO_CONSOLE_INPUT
13002 /* While starting up, there is no place to enter text. */
13003 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013005#endif
13006
13007 cmd_silent = FALSE; /* Want to see the prompt. */
13008 if (prompt != NULL)
13009 {
13010 /* Only the part of the message after the last NL is considered as
13011 * prompt for the command line */
13012 p = vim_strrchr(prompt, '\n');
13013 if (p == NULL)
13014 p = prompt;
13015 else
13016 {
13017 ++p;
13018 c = *p;
13019 *p = NUL;
13020 msg_start();
13021 msg_clr_eos();
13022 msg_puts_attr(prompt, echo_attr);
13023 msg_didout = FALSE;
13024 msg_starthere();
13025 *p = c;
13026 }
13027 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013028
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013029 if (argvars[1].v_type != VAR_UNKNOWN)
13030 {
13031 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13032 if (defstr != NULL)
13033 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013034
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013035 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013036 {
13037 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013038 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013039 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013040
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013041 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013042 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013043
Bram Moolenaar4463f292005-09-25 22:20:24 +000013044 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13045 if (xp_name == NULL)
13046 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013047
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013048 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013049
Bram Moolenaar4463f292005-09-25 22:20:24 +000013050 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13051 &xp_arg) == FAIL)
13052 return;
13053 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013054 }
13055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013056 if (defstr != NULL)
13057 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013058 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13059 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013060 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013061 && argvars[1].v_type != VAR_UNKNOWN
13062 && argvars[2].v_type != VAR_UNKNOWN)
13063 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13064 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013065
13066 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013067
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013068 /* since the user typed this, no need to wait for return */
13069 need_wait_return = FALSE;
13070 msg_didout = FALSE;
13071 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013072 cmd_silent = cmd_silent_save;
13073}
13074
13075/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013076 * "input()" function
13077 * Also handles inputsecret() when inputsecret is set.
13078 */
13079 static void
13080f_input(argvars, rettv)
13081 typval_T *argvars;
13082 typval_T *rettv;
13083{
13084 get_user_input(argvars, rettv, FALSE);
13085}
13086
13087/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013088 * "inputdialog()" function
13089 */
13090 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013091f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013092 typval_T *argvars;
13093 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013094{
13095#if defined(FEAT_GUI_TEXTDIALOG)
13096 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13097 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13098 {
13099 char_u *message;
13100 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013101 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013103 message = get_tv_string_chk(&argvars[0]);
13104 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013105 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013106 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013107 else
13108 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013109 if (message != NULL && defstr != NULL
13110 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013111 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013112 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113 else
13114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013115 if (message != NULL && defstr != NULL
13116 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013117 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013118 rettv->vval.v_string = vim_strsave(
13119 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013121 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013122 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013123 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124 }
13125 else
13126#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013127 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013128}
13129
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013130/*
13131 * "inputlist()" function
13132 */
13133 static void
13134f_inputlist(argvars, rettv)
13135 typval_T *argvars;
13136 typval_T *rettv;
13137{
13138 listitem_T *li;
13139 int selected;
13140 int mouse_used;
13141
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013142#ifdef NO_CONSOLE_INPUT
13143 /* While starting up, there is no place to enter text. */
13144 if (no_console_input())
13145 return;
13146#endif
13147 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13148 {
13149 EMSG2(_(e_listarg), "inputlist()");
13150 return;
13151 }
13152
13153 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013154 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013155 lines_left = Rows; /* avoid more prompt */
13156 msg_scroll = TRUE;
13157 msg_clr_eos();
13158
13159 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13160 {
13161 msg_puts(get_tv_string(&li->li_tv));
13162 msg_putchar('\n');
13163 }
13164
13165 /* Ask for choice. */
13166 selected = prompt_for_number(&mouse_used);
13167 if (mouse_used)
13168 selected -= lines_left;
13169
13170 rettv->vval.v_number = selected;
13171}
13172
13173
Bram Moolenaar071d4272004-06-13 20:20:40 +000013174static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13175
13176/*
13177 * "inputrestore()" function
13178 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013179 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013180f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013181 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013182 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013183{
13184 if (ga_userinput.ga_len > 0)
13185 {
13186 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13188 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013189 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013190 }
13191 else if (p_verbose > 1)
13192 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013193 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013194 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013195 }
13196}
13197
13198/*
13199 * "inputsave()" function
13200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013201 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013202f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013203 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013204 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013206 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207 if (ga_grow(&ga_userinput, 1) == OK)
13208 {
13209 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13210 + ga_userinput.ga_len);
13211 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013212 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013213 }
13214 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013215 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013216}
13217
13218/*
13219 * "inputsecret()" function
13220 */
13221 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013222f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013223 typval_T *argvars;
13224 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225{
13226 ++cmdline_star;
13227 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013228 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229 --cmdline_star;
13230 --inputsecret_flag;
13231}
13232
13233/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013234 * "insert()" function
13235 */
13236 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013237f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013238 typval_T *argvars;
13239 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013240{
13241 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013242 listitem_T *item;
13243 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013244 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013245
13246 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013247 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013248 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013249 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013250 {
13251 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013252 before = get_tv_number_chk(&argvars[2], &error);
13253 if (error)
13254 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013255
Bram Moolenaar758711c2005-02-02 23:11:38 +000013256 if (before == l->lv_len)
13257 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013258 else
13259 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013260 item = list_find(l, before);
13261 if (item == NULL)
13262 {
13263 EMSGN(_(e_listidx), before);
13264 l = NULL;
13265 }
13266 }
13267 if (l != NULL)
13268 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013269 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013270 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013271 }
13272 }
13273}
13274
13275/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013276 * "invert(expr)" function
13277 */
13278 static void
13279f_invert(argvars, rettv)
13280 typval_T *argvars;
13281 typval_T *rettv;
13282{
13283 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13284}
13285
13286/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287 * "isdirectory()" function
13288 */
13289 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013290f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013291 typval_T *argvars;
13292 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013293{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013294 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013295}
13296
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013297/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013298 * "islocked()" function
13299 */
13300 static void
13301f_islocked(argvars, rettv)
13302 typval_T *argvars;
13303 typval_T *rettv;
13304{
13305 lval_T lv;
13306 char_u *end;
13307 dictitem_T *di;
13308
13309 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013310 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13311 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013312 if (end != NULL && lv.ll_name != NULL)
13313 {
13314 if (*end != NUL)
13315 EMSG(_(e_trailing));
13316 else
13317 {
13318 if (lv.ll_tv == NULL)
13319 {
13320 if (check_changedtick(lv.ll_name))
13321 rettv->vval.v_number = 1; /* always locked */
13322 else
13323 {
13324 di = find_var(lv.ll_name, NULL);
13325 if (di != NULL)
13326 {
13327 /* Consider a variable locked when:
13328 * 1. the variable itself is locked
13329 * 2. the value of the variable is locked.
13330 * 3. the List or Dict value is locked.
13331 */
13332 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13333 || tv_islocked(&di->di_tv));
13334 }
13335 }
13336 }
13337 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013338 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013339 else if (lv.ll_newkey != NULL)
13340 EMSG2(_(e_dictkey), lv.ll_newkey);
13341 else if (lv.ll_list != NULL)
13342 /* List item. */
13343 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13344 else
13345 /* Dictionary item. */
13346 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13347 }
13348 }
13349
13350 clear_lval(&lv);
13351}
13352
Bram Moolenaar33570922005-01-25 22:26:29 +000013353static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013354
13355/*
13356 * Turn a dict into a list:
13357 * "what" == 0: list of keys
13358 * "what" == 1: list of values
13359 * "what" == 2: list of items
13360 */
13361 static void
13362dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013363 typval_T *argvars;
13364 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013365 int what;
13366{
Bram Moolenaar33570922005-01-25 22:26:29 +000013367 list_T *l2;
13368 dictitem_T *di;
13369 hashitem_T *hi;
13370 listitem_T *li;
13371 listitem_T *li2;
13372 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013373 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013374
Bram Moolenaar8c711452005-01-14 21:53:12 +000013375 if (argvars[0].v_type != VAR_DICT)
13376 {
13377 EMSG(_(e_dictreq));
13378 return;
13379 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013380 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013381 return;
13382
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013383 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013384 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013385
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013386 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013387 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013388 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013389 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013390 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013391 --todo;
13392 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013393
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013394 li = listitem_alloc();
13395 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013396 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013397 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013398
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013399 if (what == 0)
13400 {
13401 /* keys() */
13402 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013403 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013404 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13405 }
13406 else if (what == 1)
13407 {
13408 /* values() */
13409 copy_tv(&di->di_tv, &li->li_tv);
13410 }
13411 else
13412 {
13413 /* items() */
13414 l2 = list_alloc();
13415 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013416 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013417 li->li_tv.vval.v_list = l2;
13418 if (l2 == NULL)
13419 break;
13420 ++l2->lv_refcount;
13421
13422 li2 = listitem_alloc();
13423 if (li2 == NULL)
13424 break;
13425 list_append(l2, li2);
13426 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013427 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013428 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13429
13430 li2 = listitem_alloc();
13431 if (li2 == NULL)
13432 break;
13433 list_append(l2, li2);
13434 copy_tv(&di->di_tv, &li2->li_tv);
13435 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013436 }
13437 }
13438}
13439
13440/*
13441 * "items(dict)" function
13442 */
13443 static void
13444f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013445 typval_T *argvars;
13446 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013447{
13448 dict_list(argvars, rettv, 2);
13449}
13450
Bram Moolenaar071d4272004-06-13 20:20:40 +000013451/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013452 * "join()" function
13453 */
13454 static void
13455f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013456 typval_T *argvars;
13457 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013458{
13459 garray_T ga;
13460 char_u *sep;
13461
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013462 if (argvars[0].v_type != VAR_LIST)
13463 {
13464 EMSG(_(e_listreq));
13465 return;
13466 }
13467 if (argvars[0].vval.v_list == NULL)
13468 return;
13469 if (argvars[1].v_type == VAR_UNKNOWN)
13470 sep = (char_u *)" ";
13471 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013472 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013473
13474 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013475
13476 if (sep != NULL)
13477 {
13478 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013479 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013480 ga_append(&ga, NUL);
13481 rettv->vval.v_string = (char_u *)ga.ga_data;
13482 }
13483 else
13484 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013485}
13486
13487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013488 * "keys()" function
13489 */
13490 static void
13491f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013492 typval_T *argvars;
13493 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013494{
13495 dict_list(argvars, rettv, 0);
13496}
13497
13498/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013499 * "last_buffer_nr()" function.
13500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013501 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013502f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013503 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013504 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505{
13506 int n = 0;
13507 buf_T *buf;
13508
13509 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13510 if (n < buf->b_fnum)
13511 n = buf->b_fnum;
13512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013513 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013514}
13515
13516/*
13517 * "len()" function
13518 */
13519 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013520f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013521 typval_T *argvars;
13522 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013523{
13524 switch (argvars[0].v_type)
13525 {
13526 case VAR_STRING:
13527 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013528 rettv->vval.v_number = (varnumber_T)STRLEN(
13529 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013530 break;
13531 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013532 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013533 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013534 case VAR_DICT:
13535 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13536 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013537 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013538 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013539 break;
13540 }
13541}
13542
Bram Moolenaar33570922005-01-25 22:26:29 +000013543static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013544
13545 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013546libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013547 typval_T *argvars;
13548 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013549 int type;
13550{
13551#ifdef FEAT_LIBCALL
13552 char_u *string_in;
13553 char_u **string_result;
13554 int nr_result;
13555#endif
13556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013557 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013558 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013559 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013560
13561 if (check_restricted() || check_secure())
13562 return;
13563
13564#ifdef FEAT_LIBCALL
13565 /* The first two args must be strings, otherwise its meaningless */
13566 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13567 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013568 string_in = NULL;
13569 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013570 string_in = argvars[2].vval.v_string;
13571 if (type == VAR_NUMBER)
13572 string_result = NULL;
13573 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013574 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013575 if (mch_libcall(argvars[0].vval.v_string,
13576 argvars[1].vval.v_string,
13577 string_in,
13578 argvars[2].vval.v_number,
13579 string_result,
13580 &nr_result) == OK
13581 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013582 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013583 }
13584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585}
13586
13587/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013588 * "libcall()" function
13589 */
13590 static void
13591f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013592 typval_T *argvars;
13593 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013594{
13595 libcall_common(argvars, rettv, VAR_STRING);
13596}
13597
13598/*
13599 * "libcallnr()" function
13600 */
13601 static void
13602f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013603 typval_T *argvars;
13604 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013605{
13606 libcall_common(argvars, rettv, VAR_NUMBER);
13607}
13608
13609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013610 * "line(string)" function
13611 */
13612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013613f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013614 typval_T *argvars;
13615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013616{
13617 linenr_T lnum = 0;
13618 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013619 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013620
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013621 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622 if (fp != NULL)
13623 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013624 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013625}
13626
13627/*
13628 * "line2byte(lnum)" function
13629 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013630 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013631f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013632 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013633 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013634{
13635#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013636 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637#else
13638 linenr_T lnum;
13639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013640 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013641 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013642 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013644 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13645 if (rettv->vval.v_number >= 0)
13646 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013647#endif
13648}
13649
13650/*
13651 * "lispindent(lnum)" function
13652 */
13653 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013654f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013655 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013656 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013657{
13658#ifdef FEAT_LISP
13659 pos_T pos;
13660 linenr_T lnum;
13661
13662 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013663 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13665 {
13666 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013667 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013668 curwin->w_cursor = pos;
13669 }
13670 else
13671#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013672 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013673}
13674
13675/*
13676 * "localtime()" function
13677 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013679f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013680 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013681 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013682{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013683 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013684}
13685
Bram Moolenaar33570922005-01-25 22:26:29 +000013686static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013687
13688 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013689get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013690 typval_T *argvars;
13691 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013692 int exact;
13693{
13694 char_u *keys;
13695 char_u *which;
13696 char_u buf[NUMBUFLEN];
13697 char_u *keys_buf = NULL;
13698 char_u *rhs;
13699 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013700 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013701 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013702 mapblock_T *mp;
13703 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704
13705 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013706 rettv->v_type = VAR_STRING;
13707 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013709 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710 if (*keys == NUL)
13711 return;
13712
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013713 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013714 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013715 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013716 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013717 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013718 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013719 if (argvars[3].v_type != VAR_UNKNOWN)
13720 get_dict = get_tv_number(&argvars[3]);
13721 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013722 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013723 else
13724 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013725 if (which == NULL)
13726 return;
13727
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728 mode = get_map_mode(&which, 0);
13729
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013730 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013731 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013733
13734 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013735 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013736 /* Return a string. */
13737 if (rhs != NULL)
13738 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013739
Bram Moolenaarbd743252010-10-20 21:23:33 +020013740 }
13741 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13742 {
13743 /* Return a dictionary. */
13744 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13745 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13746 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013747
Bram Moolenaarbd743252010-10-20 21:23:33 +020013748 dict_add_nr_str(dict, "lhs", 0L, lhs);
13749 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13750 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13751 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13752 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13753 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13754 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013755 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013756 dict_add_nr_str(dict, "mode", 0L, mapmode);
13757
13758 vim_free(lhs);
13759 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760 }
13761}
13762
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013763#ifdef FEAT_FLOAT
13764/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013765 * "log()" function
13766 */
13767 static void
13768f_log(argvars, rettv)
13769 typval_T *argvars;
13770 typval_T *rettv;
13771{
13772 float_T f;
13773
13774 rettv->v_type = VAR_FLOAT;
13775 if (get_float_arg(argvars, &f) == OK)
13776 rettv->vval.v_float = log(f);
13777 else
13778 rettv->vval.v_float = 0.0;
13779}
13780
13781/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013782 * "log10()" function
13783 */
13784 static void
13785f_log10(argvars, rettv)
13786 typval_T *argvars;
13787 typval_T *rettv;
13788{
13789 float_T f;
13790
13791 rettv->v_type = VAR_FLOAT;
13792 if (get_float_arg(argvars, &f) == OK)
13793 rettv->vval.v_float = log10(f);
13794 else
13795 rettv->vval.v_float = 0.0;
13796}
13797#endif
13798
Bram Moolenaar1dced572012-04-05 16:54:08 +020013799#ifdef FEAT_LUA
13800/*
13801 * "luaeval()" function
13802 */
13803 static void
13804f_luaeval(argvars, rettv)
13805 typval_T *argvars;
13806 typval_T *rettv;
13807{
13808 char_u *str;
13809 char_u buf[NUMBUFLEN];
13810
13811 str = get_tv_string_buf(&argvars[0], buf);
13812 do_luaeval(str, argvars + 1, rettv);
13813}
13814#endif
13815
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013817 * "map()" function
13818 */
13819 static void
13820f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013821 typval_T *argvars;
13822 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013823{
13824 filter_map(argvars, rettv, TRUE);
13825}
13826
13827/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013828 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013829 */
13830 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013831f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013832 typval_T *argvars;
13833 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013835 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836}
13837
13838/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013839 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013840 */
13841 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013842f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013843 typval_T *argvars;
13844 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013846 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847}
13848
Bram Moolenaar33570922005-01-25 22:26:29 +000013849static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013850
13851 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013852find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013853 typval_T *argvars;
13854 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013855 int type;
13856{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013857 char_u *str = NULL;
13858 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013859 char_u *pat;
13860 regmatch_T regmatch;
13861 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013862 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013863 char_u *save_cpo;
13864 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013865 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013866 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013867 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013868 list_T *l = NULL;
13869 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013870 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013871 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872
13873 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13874 save_cpo = p_cpo;
13875 p_cpo = (char_u *)"";
13876
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013877 rettv->vval.v_number = -1;
13878 if (type == 3)
13879 {
13880 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013881 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013882 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013883 }
13884 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013885 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013886 rettv->v_type = VAR_STRING;
13887 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013890 if (argvars[0].v_type == VAR_LIST)
13891 {
13892 if ((l = argvars[0].vval.v_list) == NULL)
13893 goto theend;
13894 li = l->lv_first;
13895 }
13896 else
13897 expr = str = get_tv_string(&argvars[0]);
13898
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013899 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13900 if (pat == NULL)
13901 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013902
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013903 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013905 int error = FALSE;
13906
13907 start = get_tv_number_chk(&argvars[2], &error);
13908 if (error)
13909 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013910 if (l != NULL)
13911 {
13912 li = list_find(l, start);
13913 if (li == NULL)
13914 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013915 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013916 }
13917 else
13918 {
13919 if (start < 0)
13920 start = 0;
13921 if (start > (long)STRLEN(str))
13922 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013923 /* When "count" argument is there ignore matches before "start",
13924 * otherwise skip part of the string. Differs when pattern is "^"
13925 * or "\<". */
13926 if (argvars[3].v_type != VAR_UNKNOWN)
13927 startcol = start;
13928 else
13929 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013930 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013931
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013932 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013933 nth = get_tv_number_chk(&argvars[3], &error);
13934 if (error)
13935 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936 }
13937
13938 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13939 if (regmatch.regprog != NULL)
13940 {
13941 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013942
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013943 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013944 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013945 if (l != NULL)
13946 {
13947 if (li == NULL)
13948 {
13949 match = FALSE;
13950 break;
13951 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013952 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013953 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013954 if (str == NULL)
13955 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013956 }
13957
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013958 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013959
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013960 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013961 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013962 if (l == NULL && !match)
13963 break;
13964
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013965 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013966 if (l != NULL)
13967 {
13968 li = li->li_next;
13969 ++idx;
13970 }
13971 else
13972 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013973#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013974 startcol = (colnr_T)(regmatch.startp[0]
13975 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013976#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013977 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013978#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013979 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013980 }
13981
13982 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013983 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013984 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013985 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013986 int i;
13987
13988 /* return list with matched string and submatches */
13989 for (i = 0; i < NSUBEXP; ++i)
13990 {
13991 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013992 {
13993 if (list_append_string(rettv->vval.v_list,
13994 (char_u *)"", 0) == FAIL)
13995 break;
13996 }
13997 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013998 regmatch.startp[i],
13999 (int)(regmatch.endp[i] - regmatch.startp[i]))
14000 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014001 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014002 }
14003 }
14004 else if (type == 2)
14005 {
14006 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014007 if (l != NULL)
14008 copy_tv(&li->li_tv, rettv);
14009 else
14010 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014011 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014012 }
14013 else if (l != NULL)
14014 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 else
14016 {
14017 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014018 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019 (varnumber_T)(regmatch.startp[0] - str);
14020 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014021 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014023 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024 }
14025 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014026 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027 }
14028
14029theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014030 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 p_cpo = save_cpo;
14032}
14033
14034/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014035 * "match()" function
14036 */
14037 static void
14038f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014039 typval_T *argvars;
14040 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014041{
14042 find_some_match(argvars, rettv, 1);
14043}
14044
14045/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014046 * "matchadd()" function
14047 */
14048 static void
14049f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014050 typval_T *argvars UNUSED;
14051 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014052{
14053#ifdef FEAT_SEARCH_EXTRA
14054 char_u buf[NUMBUFLEN];
14055 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14056 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14057 int prio = 10; /* default priority */
14058 int id = -1;
14059 int error = FALSE;
14060
14061 rettv->vval.v_number = -1;
14062
14063 if (grp == NULL || pat == NULL)
14064 return;
14065 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014066 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014067 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014068 if (argvars[3].v_type != VAR_UNKNOWN)
14069 id = get_tv_number_chk(&argvars[3], &error);
14070 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014071 if (error == TRUE)
14072 return;
14073 if (id >= 1 && id <= 3)
14074 {
14075 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14076 return;
14077 }
14078
14079 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14080#endif
14081}
14082
14083/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014084 * "matcharg()" function
14085 */
14086 static void
14087f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014088 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014089 typval_T *rettv;
14090{
14091 if (rettv_list_alloc(rettv) == OK)
14092 {
14093#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014094 int id = get_tv_number(&argvars[0]);
14095 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014096
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014097 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014098 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014099 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14100 {
14101 list_append_string(rettv->vval.v_list,
14102 syn_id2name(m->hlg_id), -1);
14103 list_append_string(rettv->vval.v_list, m->pattern, -1);
14104 }
14105 else
14106 {
14107 list_append_string(rettv->vval.v_list, NUL, -1);
14108 list_append_string(rettv->vval.v_list, NUL, -1);
14109 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014110 }
14111#endif
14112 }
14113}
14114
14115/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014116 * "matchdelete()" function
14117 */
14118 static void
14119f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014120 typval_T *argvars UNUSED;
14121 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014122{
14123#ifdef FEAT_SEARCH_EXTRA
14124 rettv->vval.v_number = match_delete(curwin,
14125 (int)get_tv_number(&argvars[0]), TRUE);
14126#endif
14127}
14128
14129/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014130 * "matchend()" function
14131 */
14132 static void
14133f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014134 typval_T *argvars;
14135 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014136{
14137 find_some_match(argvars, rettv, 0);
14138}
14139
14140/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014141 * "matchlist()" function
14142 */
14143 static void
14144f_matchlist(argvars, rettv)
14145 typval_T *argvars;
14146 typval_T *rettv;
14147{
14148 find_some_match(argvars, rettv, 3);
14149}
14150
14151/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014152 * "matchstr()" function
14153 */
14154 static void
14155f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014156 typval_T *argvars;
14157 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014158{
14159 find_some_match(argvars, rettv, 2);
14160}
14161
Bram Moolenaar33570922005-01-25 22:26:29 +000014162static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014163
14164 static void
14165max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014166 typval_T *argvars;
14167 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014168 int domax;
14169{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014170 long n = 0;
14171 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014172 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014173
14174 if (argvars[0].v_type == VAR_LIST)
14175 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014176 list_T *l;
14177 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014178
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014179 l = argvars[0].vval.v_list;
14180 if (l != NULL)
14181 {
14182 li = l->lv_first;
14183 if (li != NULL)
14184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014185 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014186 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014187 {
14188 li = li->li_next;
14189 if (li == NULL)
14190 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014191 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014192 if (domax ? i > n : i < n)
14193 n = i;
14194 }
14195 }
14196 }
14197 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014198 else if (argvars[0].v_type == VAR_DICT)
14199 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014200 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014201 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014202 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014203 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014204
14205 d = argvars[0].vval.v_dict;
14206 if (d != NULL)
14207 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014208 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014209 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014210 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014211 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014212 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014213 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014214 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014215 if (first)
14216 {
14217 n = i;
14218 first = FALSE;
14219 }
14220 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014221 n = i;
14222 }
14223 }
14224 }
14225 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014226 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014227 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014228 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014229}
14230
14231/*
14232 * "max()" function
14233 */
14234 static void
14235f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014236 typval_T *argvars;
14237 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014238{
14239 max_min(argvars, rettv, TRUE);
14240}
14241
14242/*
14243 * "min()" function
14244 */
14245 static void
14246f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014247 typval_T *argvars;
14248 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014249{
14250 max_min(argvars, rettv, FALSE);
14251}
14252
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014253static int mkdir_recurse __ARGS((char_u *dir, int prot));
14254
14255/*
14256 * Create the directory in which "dir" is located, and higher levels when
14257 * needed.
14258 */
14259 static int
14260mkdir_recurse(dir, prot)
14261 char_u *dir;
14262 int prot;
14263{
14264 char_u *p;
14265 char_u *updir;
14266 int r = FAIL;
14267
14268 /* Get end of directory name in "dir".
14269 * We're done when it's "/" or "c:/". */
14270 p = gettail_sep(dir);
14271 if (p <= get_past_head(dir))
14272 return OK;
14273
14274 /* If the directory exists we're done. Otherwise: create it.*/
14275 updir = vim_strnsave(dir, (int)(p - dir));
14276 if (updir == NULL)
14277 return FAIL;
14278 if (mch_isdir(updir))
14279 r = OK;
14280 else if (mkdir_recurse(updir, prot) == OK)
14281 r = vim_mkdir_emsg(updir, prot);
14282 vim_free(updir);
14283 return r;
14284}
14285
14286#ifdef vim_mkdir
14287/*
14288 * "mkdir()" function
14289 */
14290 static void
14291f_mkdir(argvars, rettv)
14292 typval_T *argvars;
14293 typval_T *rettv;
14294{
14295 char_u *dir;
14296 char_u buf[NUMBUFLEN];
14297 int prot = 0755;
14298
14299 rettv->vval.v_number = FAIL;
14300 if (check_restricted() || check_secure())
14301 return;
14302
14303 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014304 if (*dir == NUL)
14305 rettv->vval.v_number = FAIL;
14306 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014307 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014308 if (*gettail(dir) == NUL)
14309 /* remove trailing slashes */
14310 *gettail_sep(dir) = NUL;
14311
14312 if (argvars[1].v_type != VAR_UNKNOWN)
14313 {
14314 if (argvars[2].v_type != VAR_UNKNOWN)
14315 prot = get_tv_number_chk(&argvars[2], NULL);
14316 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14317 mkdir_recurse(dir, prot);
14318 }
14319 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014320 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014321}
14322#endif
14323
Bram Moolenaar0d660222005-01-07 21:51:51 +000014324/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325 * "mode()" function
14326 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014328f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014329 typval_T *argvars;
14330 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014332 char_u buf[3];
14333
14334 buf[1] = NUL;
14335 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336
14337#ifdef FEAT_VISUAL
14338 if (VIsual_active)
14339 {
14340 if (VIsual_select)
14341 buf[0] = VIsual_mode + 's' - 'v';
14342 else
14343 buf[0] = VIsual_mode;
14344 }
14345 else
14346#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014347 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14348 || State == CONFIRM)
14349 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014351 if (State == ASKMORE)
14352 buf[1] = 'm';
14353 else if (State == CONFIRM)
14354 buf[1] = '?';
14355 }
14356 else if (State == EXTERNCMD)
14357 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014358 else if (State & INSERT)
14359 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014360#ifdef FEAT_VREPLACE
14361 if (State & VREPLACE_FLAG)
14362 {
14363 buf[0] = 'R';
14364 buf[1] = 'v';
14365 }
14366 else
14367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014368 if (State & REPLACE_FLAG)
14369 buf[0] = 'R';
14370 else
14371 buf[0] = 'i';
14372 }
14373 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014374 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014375 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014376 if (exmode_active)
14377 buf[1] = 'v';
14378 }
14379 else if (exmode_active)
14380 {
14381 buf[0] = 'c';
14382 buf[1] = 'e';
14383 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014385 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014387 if (finish_op)
14388 buf[1] = 'o';
14389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014391 /* Clear out the minor mode when the argument is not a non-zero number or
14392 * non-empty string. */
14393 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014394 buf[1] = NUL;
14395
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014396 rettv->vval.v_string = vim_strsave(buf);
14397 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398}
14399
Bram Moolenaar429fa852013-04-15 12:27:36 +020014400#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014401/*
14402 * "mzeval()" function
14403 */
14404 static void
14405f_mzeval(argvars, rettv)
14406 typval_T *argvars;
14407 typval_T *rettv;
14408{
14409 char_u *str;
14410 char_u buf[NUMBUFLEN];
14411
14412 str = get_tv_string_buf(&argvars[0], buf);
14413 do_mzeval(str, rettv);
14414}
Bram Moolenaar75676462013-01-30 14:55:42 +010014415
14416 void
14417mzscheme_call_vim(name, args, rettv)
14418 char_u *name;
14419 typval_T *args;
14420 typval_T *rettv;
14421{
14422 typval_T argvars[3];
14423
14424 argvars[0].v_type = VAR_STRING;
14425 argvars[0].vval.v_string = name;
14426 copy_tv(args, &argvars[1]);
14427 argvars[2].v_type = VAR_UNKNOWN;
14428 f_call(argvars, rettv);
14429 clear_tv(&argvars[1]);
14430}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014431#endif
14432
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014434 * "nextnonblank()" function
14435 */
14436 static void
14437f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014438 typval_T *argvars;
14439 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014440{
14441 linenr_T lnum;
14442
14443 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014445 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014446 {
14447 lnum = 0;
14448 break;
14449 }
14450 if (*skipwhite(ml_get(lnum)) != NUL)
14451 break;
14452 }
14453 rettv->vval.v_number = lnum;
14454}
14455
14456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457 * "nr2char()" function
14458 */
14459 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014460f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014461 typval_T *argvars;
14462 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463{
14464 char_u buf[NUMBUFLEN];
14465
14466#ifdef FEAT_MBYTE
14467 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014468 {
14469 int utf8 = 0;
14470
14471 if (argvars[1].v_type != VAR_UNKNOWN)
14472 utf8 = get_tv_number_chk(&argvars[1], NULL);
14473 if (utf8)
14474 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14475 else
14476 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014478 else
14479#endif
14480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014481 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 buf[1] = NUL;
14483 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014484 rettv->v_type = VAR_STRING;
14485 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014486}
14487
14488/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014489 * "or(expr, expr)" function
14490 */
14491 static void
14492f_or(argvars, rettv)
14493 typval_T *argvars;
14494 typval_T *rettv;
14495{
14496 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14497 | get_tv_number_chk(&argvars[1], NULL);
14498}
14499
14500/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014501 * "pathshorten()" function
14502 */
14503 static void
14504f_pathshorten(argvars, rettv)
14505 typval_T *argvars;
14506 typval_T *rettv;
14507{
14508 char_u *p;
14509
14510 rettv->v_type = VAR_STRING;
14511 p = get_tv_string_chk(&argvars[0]);
14512 if (p == NULL)
14513 rettv->vval.v_string = NULL;
14514 else
14515 {
14516 p = vim_strsave(p);
14517 rettv->vval.v_string = p;
14518 if (p != NULL)
14519 shorten_dir(p);
14520 }
14521}
14522
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014523#ifdef FEAT_FLOAT
14524/*
14525 * "pow()" function
14526 */
14527 static void
14528f_pow(argvars, rettv)
14529 typval_T *argvars;
14530 typval_T *rettv;
14531{
14532 float_T fx, fy;
14533
14534 rettv->v_type = VAR_FLOAT;
14535 if (get_float_arg(argvars, &fx) == OK
14536 && get_float_arg(&argvars[1], &fy) == OK)
14537 rettv->vval.v_float = pow(fx, fy);
14538 else
14539 rettv->vval.v_float = 0.0;
14540}
14541#endif
14542
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014543/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014544 * "prevnonblank()" function
14545 */
14546 static void
14547f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014548 typval_T *argvars;
14549 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014550{
14551 linenr_T lnum;
14552
14553 lnum = get_tv_lnum(argvars);
14554 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14555 lnum = 0;
14556 else
14557 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14558 --lnum;
14559 rettv->vval.v_number = lnum;
14560}
14561
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014562#ifdef HAVE_STDARG_H
14563/* This dummy va_list is here because:
14564 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14565 * - locally in the function results in a "used before set" warning
14566 * - using va_start() to initialize it gives "function with fixed args" error */
14567static va_list ap;
14568#endif
14569
Bram Moolenaar8c711452005-01-14 21:53:12 +000014570/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014571 * "printf()" function
14572 */
14573 static void
14574f_printf(argvars, rettv)
14575 typval_T *argvars;
14576 typval_T *rettv;
14577{
14578 rettv->v_type = VAR_STRING;
14579 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014580#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014581 {
14582 char_u buf[NUMBUFLEN];
14583 int len;
14584 char_u *s;
14585 int saved_did_emsg = did_emsg;
14586 char *fmt;
14587
14588 /* Get the required length, allocate the buffer and do it for real. */
14589 did_emsg = FALSE;
14590 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014591 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014592 if (!did_emsg)
14593 {
14594 s = alloc(len + 1);
14595 if (s != NULL)
14596 {
14597 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014598 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014599 }
14600 }
14601 did_emsg |= saved_did_emsg;
14602 }
14603#endif
14604}
14605
14606/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014607 * "pumvisible()" function
14608 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014609 static void
14610f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014611 typval_T *argvars UNUSED;
14612 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014613{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014614#ifdef FEAT_INS_EXPAND
14615 if (pum_visible())
14616 rettv->vval.v_number = 1;
14617#endif
14618}
14619
Bram Moolenaardb913952012-06-29 12:54:53 +020014620#ifdef FEAT_PYTHON3
14621/*
14622 * "py3eval()" function
14623 */
14624 static void
14625f_py3eval(argvars, rettv)
14626 typval_T *argvars;
14627 typval_T *rettv;
14628{
14629 char_u *str;
14630 char_u buf[NUMBUFLEN];
14631
14632 str = get_tv_string_buf(&argvars[0], buf);
14633 do_py3eval(str, rettv);
14634}
14635#endif
14636
14637#ifdef FEAT_PYTHON
14638/*
14639 * "pyeval()" function
14640 */
14641 static void
14642f_pyeval(argvars, rettv)
14643 typval_T *argvars;
14644 typval_T *rettv;
14645{
14646 char_u *str;
14647 char_u buf[NUMBUFLEN];
14648
14649 str = get_tv_string_buf(&argvars[0], buf);
14650 do_pyeval(str, rettv);
14651}
14652#endif
14653
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014654/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014655 * "range()" function
14656 */
14657 static void
14658f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014659 typval_T *argvars;
14660 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014661{
14662 long start;
14663 long end;
14664 long stride = 1;
14665 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014666 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014667
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014668 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014669 if (argvars[1].v_type == VAR_UNKNOWN)
14670 {
14671 end = start - 1;
14672 start = 0;
14673 }
14674 else
14675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014676 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014677 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014678 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014679 }
14680
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014681 if (error)
14682 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014683 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014684 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014685 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014686 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014687 else
14688 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014689 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014690 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014691 if (list_append_number(rettv->vval.v_list,
14692 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014693 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014694 }
14695}
14696
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014697/*
14698 * "readfile()" function
14699 */
14700 static void
14701f_readfile(argvars, rettv)
14702 typval_T *argvars;
14703 typval_T *rettv;
14704{
14705 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014706 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014707 char_u *fname;
14708 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014709 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14710 int io_size = sizeof(buf);
14711 int readlen; /* size of last fread() */
14712 char_u *prev = NULL; /* previously read bytes, if any */
14713 long prevlen = 0; /* length of data in prev */
14714 long prevsize = 0; /* size of prev buffer */
14715 long maxline = MAXLNUM;
14716 long cnt = 0;
14717 char_u *p; /* position in buf */
14718 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014719
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014720 if (argvars[1].v_type != VAR_UNKNOWN)
14721 {
14722 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14723 binary = TRUE;
14724 if (argvars[2].v_type != VAR_UNKNOWN)
14725 maxline = get_tv_number(&argvars[2]);
14726 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014727
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014728 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014729 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014730
14731 /* Always open the file in binary mode, library functions have a mind of
14732 * their own about CR-LF conversion. */
14733 fname = get_tv_string(&argvars[0]);
14734 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14735 {
14736 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14737 return;
14738 }
14739
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014740 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014741 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014742 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014743
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014744 /* This for loop processes what was read, but is also entered at end
14745 * of file so that either:
14746 * - an incomplete line gets written
14747 * - a "binary" file gets an empty line at the end if it ends in a
14748 * newline. */
14749 for (p = buf, start = buf;
14750 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14751 ++p)
14752 {
14753 if (*p == '\n' || readlen <= 0)
14754 {
14755 listitem_T *li;
14756 char_u *s = NULL;
14757 long_u len = p - start;
14758
14759 /* Finished a line. Remove CRs before NL. */
14760 if (readlen > 0 && !binary)
14761 {
14762 while (len > 0 && start[len - 1] == '\r')
14763 --len;
14764 /* removal may cross back to the "prev" string */
14765 if (len == 0)
14766 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14767 --prevlen;
14768 }
14769 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014770 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014771 else
14772 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014773 /* Change "prev" buffer to be the right size. This way
14774 * the bytes are only copied once, and very long lines are
14775 * allocated only once. */
14776 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014777 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014778 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014779 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014780 prev = NULL; /* the list will own the string */
14781 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014782 }
14783 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014784 if (s == NULL)
14785 {
14786 do_outofmem_msg((long_u) prevlen + len + 1);
14787 failed = TRUE;
14788 break;
14789 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014790
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014791 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014792 {
14793 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014794 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014795 break;
14796 }
14797 li->li_tv.v_type = VAR_STRING;
14798 li->li_tv.v_lock = 0;
14799 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014800 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014801
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014802 start = p + 1; /* step over newline */
14803 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014804 break;
14805 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014806 else if (*p == NUL)
14807 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014808#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014809 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14810 * when finding the BF and check the previous two bytes. */
14811 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014812 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014813 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14814 * + 1, these may be in the "prev" string. */
14815 char_u back1 = p >= buf + 1 ? p[-1]
14816 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14817 char_u back2 = p >= buf + 2 ? p[-2]
14818 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14819 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014820
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014821 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014822 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014823 char_u *dest = p - 2;
14824
14825 /* Usually a BOM is at the beginning of a file, and so at
14826 * the beginning of a line; then we can just step over it.
14827 */
14828 if (start == dest)
14829 start = p + 1;
14830 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014831 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014832 /* have to shuffle buf to close gap */
14833 int adjust_prevlen = 0;
14834
14835 if (dest < buf)
14836 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014837 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014838 dest = buf;
14839 }
14840 if (readlen > p - buf + 1)
14841 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14842 readlen -= 3 - adjust_prevlen;
14843 prevlen -= adjust_prevlen;
14844 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014845 }
14846 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014847 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014848#endif
14849 } /* for */
14850
14851 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14852 break;
14853 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014854 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014855 /* There's part of a line in buf, store it in "prev". */
14856 if (p - start + prevlen >= prevsize)
14857 {
14858 /* need bigger "prev" buffer */
14859 char_u *newprev;
14860
14861 /* A common use case is ordinary text files and "prev" gets a
14862 * fragment of a line, so the first allocation is made
14863 * small, to avoid repeatedly 'allocing' large and
14864 * 'reallocing' small. */
14865 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014866 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014867 else
14868 {
14869 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014870 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014871 prevsize = grow50pc > growmin ? grow50pc : growmin;
14872 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014873 newprev = prev == NULL ? alloc(prevsize)
14874 : vim_realloc(prev, prevsize);
14875 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014876 {
14877 do_outofmem_msg((long_u)prevsize);
14878 failed = TRUE;
14879 break;
14880 }
14881 prev = newprev;
14882 }
14883 /* Add the line part to end of "prev". */
14884 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014885 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014886 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014887 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014888
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014889 /*
14890 * For a negative line count use only the lines at the end of the file,
14891 * free the rest.
14892 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014893 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014894 while (cnt > -maxline)
14895 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014896 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014897 --cnt;
14898 }
14899
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014900 if (failed)
14901 {
14902 list_free(rettv->vval.v_list, TRUE);
14903 /* readfile doc says an empty list is returned on error */
14904 rettv->vval.v_list = list_alloc();
14905 }
14906
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014907 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014908 fclose(fd);
14909}
14910
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014911#if defined(FEAT_RELTIME)
14912static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14913
14914/*
14915 * Convert a List to proftime_T.
14916 * Return FAIL when there is something wrong.
14917 */
14918 static int
14919list2proftime(arg, tm)
14920 typval_T *arg;
14921 proftime_T *tm;
14922{
14923 long n1, n2;
14924 int error = FALSE;
14925
14926 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14927 || arg->vval.v_list->lv_len != 2)
14928 return FAIL;
14929 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14930 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14931# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014932 tm->HighPart = n1;
14933 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014934# else
14935 tm->tv_sec = n1;
14936 tm->tv_usec = n2;
14937# endif
14938 return error ? FAIL : OK;
14939}
14940#endif /* FEAT_RELTIME */
14941
14942/*
14943 * "reltime()" function
14944 */
14945 static void
14946f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014947 typval_T *argvars UNUSED;
14948 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014949{
14950#ifdef FEAT_RELTIME
14951 proftime_T res;
14952 proftime_T start;
14953
14954 if (argvars[0].v_type == VAR_UNKNOWN)
14955 {
14956 /* No arguments: get current time. */
14957 profile_start(&res);
14958 }
14959 else if (argvars[1].v_type == VAR_UNKNOWN)
14960 {
14961 if (list2proftime(&argvars[0], &res) == FAIL)
14962 return;
14963 profile_end(&res);
14964 }
14965 else
14966 {
14967 /* Two arguments: compute the difference. */
14968 if (list2proftime(&argvars[0], &start) == FAIL
14969 || list2proftime(&argvars[1], &res) == FAIL)
14970 return;
14971 profile_sub(&res, &start);
14972 }
14973
14974 if (rettv_list_alloc(rettv) == OK)
14975 {
14976 long n1, n2;
14977
14978# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014979 n1 = res.HighPart;
14980 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014981# else
14982 n1 = res.tv_sec;
14983 n2 = res.tv_usec;
14984# endif
14985 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14986 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14987 }
14988#endif
14989}
14990
14991/*
14992 * "reltimestr()" function
14993 */
14994 static void
14995f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014996 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014997 typval_T *rettv;
14998{
14999#ifdef FEAT_RELTIME
15000 proftime_T tm;
15001#endif
15002
15003 rettv->v_type = VAR_STRING;
15004 rettv->vval.v_string = NULL;
15005#ifdef FEAT_RELTIME
15006 if (list2proftime(&argvars[0], &tm) == OK)
15007 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15008#endif
15009}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015010
Bram Moolenaar0d660222005-01-07 21:51:51 +000015011#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15012static void make_connection __ARGS((void));
15013static int check_connection __ARGS((void));
15014
15015 static void
15016make_connection()
15017{
15018 if (X_DISPLAY == NULL
15019# ifdef FEAT_GUI
15020 && !gui.in_use
15021# endif
15022 )
15023 {
15024 x_force_connect = TRUE;
15025 setup_term_clip();
15026 x_force_connect = FALSE;
15027 }
15028}
15029
15030 static int
15031check_connection()
15032{
15033 make_connection();
15034 if (X_DISPLAY == NULL)
15035 {
15036 EMSG(_("E240: No connection to Vim server"));
15037 return FAIL;
15038 }
15039 return OK;
15040}
15041#endif
15042
15043#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015044static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015045
15046 static void
15047remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015048 typval_T *argvars;
15049 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015050 int expr;
15051{
15052 char_u *server_name;
15053 char_u *keys;
15054 char_u *r = NULL;
15055 char_u buf[NUMBUFLEN];
15056# ifdef WIN32
15057 HWND w;
15058# else
15059 Window w;
15060# endif
15061
15062 if (check_restricted() || check_secure())
15063 return;
15064
15065# ifdef FEAT_X11
15066 if (check_connection() == FAIL)
15067 return;
15068# endif
15069
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015070 server_name = get_tv_string_chk(&argvars[0]);
15071 if (server_name == NULL)
15072 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015073 keys = get_tv_string_buf(&argvars[1], buf);
15074# ifdef WIN32
15075 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15076# else
15077 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15078 < 0)
15079# endif
15080 {
15081 if (r != NULL)
15082 EMSG(r); /* sending worked but evaluation failed */
15083 else
15084 EMSG2(_("E241: Unable to send to %s"), server_name);
15085 return;
15086 }
15087
15088 rettv->vval.v_string = r;
15089
15090 if (argvars[2].v_type != VAR_UNKNOWN)
15091 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015092 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015093 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015094 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015095
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015096 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015097 v.di_tv.v_type = VAR_STRING;
15098 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015099 idvar = get_tv_string_chk(&argvars[2]);
15100 if (idvar != NULL)
15101 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015102 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015103 }
15104}
15105#endif
15106
15107/*
15108 * "remote_expr()" function
15109 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015110 static void
15111f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015112 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015113 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015114{
15115 rettv->v_type = VAR_STRING;
15116 rettv->vval.v_string = NULL;
15117#ifdef FEAT_CLIENTSERVER
15118 remote_common(argvars, rettv, TRUE);
15119#endif
15120}
15121
15122/*
15123 * "remote_foreground()" function
15124 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015125 static void
15126f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015127 typval_T *argvars UNUSED;
15128 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015130#ifdef FEAT_CLIENTSERVER
15131# ifdef WIN32
15132 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015133 {
15134 char_u *server_name = get_tv_string_chk(&argvars[0]);
15135
15136 if (server_name != NULL)
15137 serverForeground(server_name);
15138 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015139# else
15140 /* Send a foreground() expression to the server. */
15141 argvars[1].v_type = VAR_STRING;
15142 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15143 argvars[2].v_type = VAR_UNKNOWN;
15144 remote_common(argvars, rettv, TRUE);
15145 vim_free(argvars[1].vval.v_string);
15146# endif
15147#endif
15148}
15149
Bram Moolenaar0d660222005-01-07 21:51:51 +000015150 static void
15151f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015152 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015153 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015154{
15155#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015156 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015157 char_u *s = NULL;
15158# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015159 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015160# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015161 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015162
15163 if (check_restricted() || check_secure())
15164 {
15165 rettv->vval.v_number = -1;
15166 return;
15167 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015168 serverid = get_tv_string_chk(&argvars[0]);
15169 if (serverid == NULL)
15170 {
15171 rettv->vval.v_number = -1;
15172 return; /* type error; errmsg already given */
15173 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015174# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015175 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015176 if (n == 0)
15177 rettv->vval.v_number = -1;
15178 else
15179 {
15180 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15181 rettv->vval.v_number = (s != NULL);
15182 }
15183# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015184 if (check_connection() == FAIL)
15185 return;
15186
15187 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015188 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015189# endif
15190
15191 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015193 char_u *retvar;
15194
Bram Moolenaar33570922005-01-25 22:26:29 +000015195 v.di_tv.v_type = VAR_STRING;
15196 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015197 retvar = get_tv_string_chk(&argvars[1]);
15198 if (retvar != NULL)
15199 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015200 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015201 }
15202#else
15203 rettv->vval.v_number = -1;
15204#endif
15205}
15206
Bram Moolenaar0d660222005-01-07 21:51:51 +000015207 static void
15208f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015209 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015210 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015211{
15212 char_u *r = NULL;
15213
15214#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015215 char_u *serverid = get_tv_string_chk(&argvars[0]);
15216
15217 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015218 {
15219# ifdef WIN32
15220 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015221 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015222
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015223 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015224 if (n != 0)
15225 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15226 if (r == NULL)
15227# else
15228 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015229 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015230# endif
15231 EMSG(_("E277: Unable to read a server reply"));
15232 }
15233#endif
15234 rettv->v_type = VAR_STRING;
15235 rettv->vval.v_string = r;
15236}
15237
15238/*
15239 * "remote_send()" function
15240 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015241 static void
15242f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015243 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015244 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015245{
15246 rettv->v_type = VAR_STRING;
15247 rettv->vval.v_string = NULL;
15248#ifdef FEAT_CLIENTSERVER
15249 remote_common(argvars, rettv, FALSE);
15250#endif
15251}
15252
15253/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015254 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015255 */
15256 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015257f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015258 typval_T *argvars;
15259 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015260{
Bram Moolenaar33570922005-01-25 22:26:29 +000015261 list_T *l;
15262 listitem_T *item, *item2;
15263 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015264 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015265 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015266 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015267 dict_T *d;
15268 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015269 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015270
Bram Moolenaar8c711452005-01-14 21:53:12 +000015271 if (argvars[0].v_type == VAR_DICT)
15272 {
15273 if (argvars[2].v_type != VAR_UNKNOWN)
15274 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015275 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015276 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015277 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015278 key = get_tv_string_chk(&argvars[1]);
15279 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015280 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015281 di = dict_find(d, key, -1);
15282 if (di == NULL)
15283 EMSG2(_(e_dictkey), key);
15284 else
15285 {
15286 *rettv = di->di_tv;
15287 init_tv(&di->di_tv);
15288 dictitem_remove(d, di);
15289 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015290 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015291 }
15292 }
15293 else if (argvars[0].v_type != VAR_LIST)
15294 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015295 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015296 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015297 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015298 int error = FALSE;
15299
15300 idx = get_tv_number_chk(&argvars[1], &error);
15301 if (error)
15302 ; /* type error: do nothing, errmsg already given */
15303 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015304 EMSGN(_(e_listidx), idx);
15305 else
15306 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015307 if (argvars[2].v_type == VAR_UNKNOWN)
15308 {
15309 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015310 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015311 *rettv = item->li_tv;
15312 vim_free(item);
15313 }
15314 else
15315 {
15316 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015317 end = get_tv_number_chk(&argvars[2], &error);
15318 if (error)
15319 ; /* type error: do nothing */
15320 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015321 EMSGN(_(e_listidx), end);
15322 else
15323 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015324 int cnt = 0;
15325
15326 for (li = item; li != NULL; li = li->li_next)
15327 {
15328 ++cnt;
15329 if (li == item2)
15330 break;
15331 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015332 if (li == NULL) /* didn't find "item2" after "item" */
15333 EMSG(_(e_invrange));
15334 else
15335 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015336 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015337 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015338 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015339 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015340 l->lv_first = item;
15341 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015342 item->li_prev = NULL;
15343 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015344 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015345 }
15346 }
15347 }
15348 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015349 }
15350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351}
15352
15353/*
15354 * "rename({from}, {to})" function
15355 */
15356 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015357f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015358 typval_T *argvars;
15359 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360{
15361 char_u buf[NUMBUFLEN];
15362
15363 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015364 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015365 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015366 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15367 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015368}
15369
15370/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015371 * "repeat()" function
15372 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015373 static void
15374f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015375 typval_T *argvars;
15376 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015377{
15378 char_u *p;
15379 int n;
15380 int slen;
15381 int len;
15382 char_u *r;
15383 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015384
15385 n = get_tv_number(&argvars[1]);
15386 if (argvars[0].v_type == VAR_LIST)
15387 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015388 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015389 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015390 if (list_extend(rettv->vval.v_list,
15391 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015392 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015393 }
15394 else
15395 {
15396 p = get_tv_string(&argvars[0]);
15397 rettv->v_type = VAR_STRING;
15398 rettv->vval.v_string = NULL;
15399
15400 slen = (int)STRLEN(p);
15401 len = slen * n;
15402 if (len <= 0)
15403 return;
15404
15405 r = alloc(len + 1);
15406 if (r != NULL)
15407 {
15408 for (i = 0; i < n; i++)
15409 mch_memmove(r + i * slen, p, (size_t)slen);
15410 r[len] = NUL;
15411 }
15412
15413 rettv->vval.v_string = r;
15414 }
15415}
15416
15417/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015418 * "resolve()" function
15419 */
15420 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015421f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015422 typval_T *argvars;
15423 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424{
15425 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015426#ifdef HAVE_READLINK
15427 char_u *buf = NULL;
15428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015430 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015431#ifdef FEAT_SHORTCUT
15432 {
15433 char_u *v = NULL;
15434
15435 v = mch_resolve_shortcut(p);
15436 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015437 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015438 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015439 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 }
15441#else
15442# ifdef HAVE_READLINK
15443 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444 char_u *cpy;
15445 int len;
15446 char_u *remain = NULL;
15447 char_u *q;
15448 int is_relative_to_current = FALSE;
15449 int has_trailing_pathsep = FALSE;
15450 int limit = 100;
15451
15452 p = vim_strsave(p);
15453
15454 if (p[0] == '.' && (vim_ispathsep(p[1])
15455 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15456 is_relative_to_current = TRUE;
15457
15458 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015459 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015462 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015464
15465 q = getnextcomp(p);
15466 if (*q != NUL)
15467 {
15468 /* Separate the first path component in "p", and keep the
15469 * remainder (beginning with the path separator). */
15470 remain = vim_strsave(q - 1);
15471 q[-1] = NUL;
15472 }
15473
Bram Moolenaard9462e32011-04-11 21:35:11 +020015474 buf = alloc(MAXPATHL + 1);
15475 if (buf == NULL)
15476 goto fail;
15477
Bram Moolenaar071d4272004-06-13 20:20:40 +000015478 for (;;)
15479 {
15480 for (;;)
15481 {
15482 len = readlink((char *)p, (char *)buf, MAXPATHL);
15483 if (len <= 0)
15484 break;
15485 buf[len] = NUL;
15486
15487 if (limit-- == 0)
15488 {
15489 vim_free(p);
15490 vim_free(remain);
15491 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015492 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015493 goto fail;
15494 }
15495
15496 /* Ensure that the result will have a trailing path separator
15497 * if the argument has one. */
15498 if (remain == NULL && has_trailing_pathsep)
15499 add_pathsep(buf);
15500
15501 /* Separate the first path component in the link value and
15502 * concatenate the remainders. */
15503 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15504 if (*q != NUL)
15505 {
15506 if (remain == NULL)
15507 remain = vim_strsave(q - 1);
15508 else
15509 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015510 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015511 if (cpy != NULL)
15512 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015513 vim_free(remain);
15514 remain = cpy;
15515 }
15516 }
15517 q[-1] = NUL;
15518 }
15519
15520 q = gettail(p);
15521 if (q > p && *q == NUL)
15522 {
15523 /* Ignore trailing path separator. */
15524 q[-1] = NUL;
15525 q = gettail(p);
15526 }
15527 if (q > p && !mch_isFullName(buf))
15528 {
15529 /* symlink is relative to directory of argument */
15530 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15531 if (cpy != NULL)
15532 {
15533 STRCPY(cpy, p);
15534 STRCPY(gettail(cpy), buf);
15535 vim_free(p);
15536 p = cpy;
15537 }
15538 }
15539 else
15540 {
15541 vim_free(p);
15542 p = vim_strsave(buf);
15543 }
15544 }
15545
15546 if (remain == NULL)
15547 break;
15548
15549 /* Append the first path component of "remain" to "p". */
15550 q = getnextcomp(remain + 1);
15551 len = q - remain - (*q != NUL);
15552 cpy = vim_strnsave(p, STRLEN(p) + len);
15553 if (cpy != NULL)
15554 {
15555 STRNCAT(cpy, remain, len);
15556 vim_free(p);
15557 p = cpy;
15558 }
15559 /* Shorten "remain". */
15560 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015561 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562 else
15563 {
15564 vim_free(remain);
15565 remain = NULL;
15566 }
15567 }
15568
15569 /* If the result is a relative path name, make it explicitly relative to
15570 * the current directory if and only if the argument had this form. */
15571 if (!vim_ispathsep(*p))
15572 {
15573 if (is_relative_to_current
15574 && *p != NUL
15575 && !(p[0] == '.'
15576 && (p[1] == NUL
15577 || vim_ispathsep(p[1])
15578 || (p[1] == '.'
15579 && (p[2] == NUL
15580 || vim_ispathsep(p[2]))))))
15581 {
15582 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015583 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015584 if (cpy != NULL)
15585 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015586 vim_free(p);
15587 p = cpy;
15588 }
15589 }
15590 else if (!is_relative_to_current)
15591 {
15592 /* Strip leading "./". */
15593 q = p;
15594 while (q[0] == '.' && vim_ispathsep(q[1]))
15595 q += 2;
15596 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015597 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598 }
15599 }
15600
15601 /* Ensure that the result will have no trailing path separator
15602 * if the argument had none. But keep "/" or "//". */
15603 if (!has_trailing_pathsep)
15604 {
15605 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015606 if (after_pathsep(p, q))
15607 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608 }
15609
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015610 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015611 }
15612# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015613 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614# endif
15615#endif
15616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015617 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015618
15619#ifdef HAVE_READLINK
15620fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015621 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015623 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015624}
15625
15626/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015627 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015628 */
15629 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015630f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015631 typval_T *argvars;
15632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015633{
Bram Moolenaar33570922005-01-25 22:26:29 +000015634 list_T *l;
15635 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636
Bram Moolenaar0d660222005-01-07 21:51:51 +000015637 if (argvars[0].v_type != VAR_LIST)
15638 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015639 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015640 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015641 {
15642 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015643 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015644 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015645 while (li != NULL)
15646 {
15647 ni = li->li_prev;
15648 list_append(l, li);
15649 li = ni;
15650 }
15651 rettv->vval.v_list = l;
15652 rettv->v_type = VAR_LIST;
15653 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015654 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015656}
15657
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015658#define SP_NOMOVE 0x01 /* don't move cursor */
15659#define SP_REPEAT 0x02 /* repeat to find outer pair */
15660#define SP_RETCOUNT 0x04 /* return matchcount */
15661#define SP_SETPCMARK 0x08 /* set previous context mark */
15662#define SP_START 0x10 /* accept match at start position */
15663#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15664#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015665
Bram Moolenaar33570922005-01-25 22:26:29 +000015666static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015667
15668/*
15669 * Get flags for a search function.
15670 * Possibly sets "p_ws".
15671 * Returns BACKWARD, FORWARD or zero (for an error).
15672 */
15673 static int
15674get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015675 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015676 int *flagsp;
15677{
15678 int dir = FORWARD;
15679 char_u *flags;
15680 char_u nbuf[NUMBUFLEN];
15681 int mask;
15682
15683 if (varp->v_type != VAR_UNKNOWN)
15684 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015685 flags = get_tv_string_buf_chk(varp, nbuf);
15686 if (flags == NULL)
15687 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015688 while (*flags != NUL)
15689 {
15690 switch (*flags)
15691 {
15692 case 'b': dir = BACKWARD; break;
15693 case 'w': p_ws = TRUE; break;
15694 case 'W': p_ws = FALSE; break;
15695 default: mask = 0;
15696 if (flagsp != NULL)
15697 switch (*flags)
15698 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015699 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015700 case 'e': mask = SP_END; break;
15701 case 'm': mask = SP_RETCOUNT; break;
15702 case 'n': mask = SP_NOMOVE; break;
15703 case 'p': mask = SP_SUBPAT; break;
15704 case 'r': mask = SP_REPEAT; break;
15705 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015706 }
15707 if (mask == 0)
15708 {
15709 EMSG2(_(e_invarg2), flags);
15710 dir = 0;
15711 }
15712 else
15713 *flagsp |= mask;
15714 }
15715 if (dir == 0)
15716 break;
15717 ++flags;
15718 }
15719 }
15720 return dir;
15721}
15722
Bram Moolenaar071d4272004-06-13 20:20:40 +000015723/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015724 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015726 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015727search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015728 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015729 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015730 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015731{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015732 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733 char_u *pat;
15734 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015735 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015736 int save_p_ws = p_ws;
15737 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015738 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015739 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015740 proftime_T tm;
15741#ifdef FEAT_RELTIME
15742 long time_limit = 0;
15743#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015744 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015745 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015747 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015748 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015749 if (dir == 0)
15750 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015751 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015752 if (flags & SP_START)
15753 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015754 if (flags & SP_END)
15755 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015756
Bram Moolenaar76929292008-01-06 19:07:36 +000015757 /* Optional arguments: line number to stop searching and timeout. */
15758 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015759 {
15760 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15761 if (lnum_stop < 0)
15762 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015763#ifdef FEAT_RELTIME
15764 if (argvars[3].v_type != VAR_UNKNOWN)
15765 {
15766 time_limit = get_tv_number_chk(&argvars[3], NULL);
15767 if (time_limit < 0)
15768 goto theend;
15769 }
15770#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015771 }
15772
Bram Moolenaar76929292008-01-06 19:07:36 +000015773#ifdef FEAT_RELTIME
15774 /* Set the time limit, if there is one. */
15775 profile_setlimit(time_limit, &tm);
15776#endif
15777
Bram Moolenaar231334e2005-07-25 20:46:57 +000015778 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015779 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015780 * Check to make sure only those flags are set.
15781 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15782 * flags cannot be set. Check for that condition also.
15783 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015784 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015785 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015786 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015787 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015788 goto theend;
15789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015790
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015791 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015792 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015793 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015794 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015795 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015796 if (flags & SP_SUBPAT)
15797 retval = subpatnum;
15798 else
15799 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015800 if (flags & SP_SETPCMARK)
15801 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015803 if (match_pos != NULL)
15804 {
15805 /* Store the match cursor position */
15806 match_pos->lnum = pos.lnum;
15807 match_pos->col = pos.col + 1;
15808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 /* "/$" will put the cursor after the end of the line, may need to
15810 * correct that here */
15811 check_cursor();
15812 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015813
15814 /* If 'n' flag is used: restore cursor position. */
15815 if (flags & SP_NOMOVE)
15816 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015817 else
15818 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015819theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015821
15822 return retval;
15823}
15824
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015825#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015826
15827/*
15828 * round() is not in C90, use ceil() or floor() instead.
15829 */
15830 float_T
15831vim_round(f)
15832 float_T f;
15833{
15834 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15835}
15836
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015837/*
15838 * "round({float})" function
15839 */
15840 static void
15841f_round(argvars, rettv)
15842 typval_T *argvars;
15843 typval_T *rettv;
15844{
15845 float_T f;
15846
15847 rettv->v_type = VAR_FLOAT;
15848 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015849 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015850 else
15851 rettv->vval.v_float = 0.0;
15852}
15853#endif
15854
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015855/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015856 * "screenattr()" function
15857 */
15858 static void
15859f_screenattr(argvars, rettv)
15860 typval_T *argvars UNUSED;
15861 typval_T *rettv;
15862{
15863 int row;
15864 int col;
15865 int c;
15866
15867 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15868 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15869 if (row < 0 || row >= screen_Rows
15870 || col < 0 || col >= screen_Columns)
15871 c = -1;
15872 else
15873 c = ScreenAttrs[LineOffset[row] + col];
15874 rettv->vval.v_number = c;
15875}
15876
15877/*
15878 * "screenchar()" function
15879 */
15880 static void
15881f_screenchar(argvars, rettv)
15882 typval_T *argvars UNUSED;
15883 typval_T *rettv;
15884{
15885 int row;
15886 int col;
15887 int off;
15888 int c;
15889
15890 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15891 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15892 if (row < 0 || row >= screen_Rows
15893 || col < 0 || col >= screen_Columns)
15894 c = -1;
15895 else
15896 {
15897 off = LineOffset[row] + col;
15898#ifdef FEAT_MBYTE
15899 if (enc_utf8 && ScreenLinesUC[off] != 0)
15900 c = ScreenLinesUC[off];
15901 else
15902#endif
15903 c = ScreenLines[off];
15904 }
15905 rettv->vval.v_number = c;
15906}
15907
15908/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015909 * "screencol()" function
15910 *
15911 * First column is 1 to be consistent with virtcol().
15912 */
15913 static void
15914f_screencol(argvars, rettv)
15915 typval_T *argvars UNUSED;
15916 typval_T *rettv;
15917{
15918 rettv->vval.v_number = screen_screencol() + 1;
15919}
15920
15921/*
15922 * "screenrow()" function
15923 */
15924 static void
15925f_screenrow(argvars, rettv)
15926 typval_T *argvars UNUSED;
15927 typval_T *rettv;
15928{
15929 rettv->vval.v_number = screen_screenrow() + 1;
15930}
15931
15932/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015933 * "search()" function
15934 */
15935 static void
15936f_search(argvars, rettv)
15937 typval_T *argvars;
15938 typval_T *rettv;
15939{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015940 int flags = 0;
15941
15942 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015943}
15944
Bram Moolenaar071d4272004-06-13 20:20:40 +000015945/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015946 * "searchdecl()" function
15947 */
15948 static void
15949f_searchdecl(argvars, rettv)
15950 typval_T *argvars;
15951 typval_T *rettv;
15952{
15953 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015954 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015955 int error = FALSE;
15956 char_u *name;
15957
15958 rettv->vval.v_number = 1; /* default: FAIL */
15959
15960 name = get_tv_string_chk(&argvars[0]);
15961 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015962 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015963 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015964 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15965 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15966 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015967 if (!error && name != NULL)
15968 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015969 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015970}
15971
15972/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015973 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015974 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015975 static int
15976searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015977 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015978 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015979{
15980 char_u *spat, *mpat, *epat;
15981 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015983 int dir;
15984 int flags = 0;
15985 char_u nbuf1[NUMBUFLEN];
15986 char_u nbuf2[NUMBUFLEN];
15987 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015988 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015989 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015990 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991
Bram Moolenaar071d4272004-06-13 20:20:40 +000015992 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015993 spat = get_tv_string_chk(&argvars[0]);
15994 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15995 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15996 if (spat == NULL || mpat == NULL || epat == NULL)
15997 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998
Bram Moolenaar071d4272004-06-13 20:20:40 +000015999 /* Handle the optional fourth argument: flags */
16000 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016001 if (dir == 0)
16002 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016003
16004 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016005 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16006 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016007 if ((flags & (SP_END | SP_SUBPAT)) != 0
16008 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016009 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016010 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016011 goto theend;
16012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016013
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016014 /* Using 'r' implies 'W', otherwise it doesn't work. */
16015 if (flags & SP_REPEAT)
16016 p_ws = FALSE;
16017
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016018 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016019 if (argvars[3].v_type == VAR_UNKNOWN
16020 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016021 skip = (char_u *)"";
16022 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016023 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016024 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016025 if (argvars[5].v_type != VAR_UNKNOWN)
16026 {
16027 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16028 if (lnum_stop < 0)
16029 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016030#ifdef FEAT_RELTIME
16031 if (argvars[6].v_type != VAR_UNKNOWN)
16032 {
16033 time_limit = get_tv_number_chk(&argvars[6], NULL);
16034 if (time_limit < 0)
16035 goto theend;
16036 }
16037#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016038 }
16039 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016040 if (skip == NULL)
16041 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016042
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016043 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016044 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016045
16046theend:
16047 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016048
16049 return retval;
16050}
16051
16052/*
16053 * "searchpair()" function
16054 */
16055 static void
16056f_searchpair(argvars, rettv)
16057 typval_T *argvars;
16058 typval_T *rettv;
16059{
16060 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16061}
16062
16063/*
16064 * "searchpairpos()" function
16065 */
16066 static void
16067f_searchpairpos(argvars, rettv)
16068 typval_T *argvars;
16069 typval_T *rettv;
16070{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016071 pos_T match_pos;
16072 int lnum = 0;
16073 int col = 0;
16074
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016075 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016076 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016077
16078 if (searchpair_cmn(argvars, &match_pos) > 0)
16079 {
16080 lnum = match_pos.lnum;
16081 col = match_pos.col;
16082 }
16083
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016084 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16085 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016086}
16087
16088/*
16089 * Search for a start/middle/end thing.
16090 * Used by searchpair(), see its documentation for the details.
16091 * Returns 0 or -1 for no match,
16092 */
16093 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016094do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16095 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016096 char_u *spat; /* start pattern */
16097 char_u *mpat; /* middle pattern */
16098 char_u *epat; /* end pattern */
16099 int dir; /* BACKWARD or FORWARD */
16100 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016101 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016102 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016103 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016104 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016105{
16106 char_u *save_cpo;
16107 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16108 long retval = 0;
16109 pos_T pos;
16110 pos_T firstpos;
16111 pos_T foundpos;
16112 pos_T save_cursor;
16113 pos_T save_pos;
16114 int n;
16115 int r;
16116 int nest = 1;
16117 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016118 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016119 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016120
16121 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16122 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016123 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016124
Bram Moolenaar76929292008-01-06 19:07:36 +000016125#ifdef FEAT_RELTIME
16126 /* Set the time limit, if there is one. */
16127 profile_setlimit(time_limit, &tm);
16128#endif
16129
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016130 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16131 * start/middle/end (pat3, for the top pair). */
16132 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16133 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16134 if (pat2 == NULL || pat3 == NULL)
16135 goto theend;
16136 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16137 if (*mpat == NUL)
16138 STRCPY(pat3, pat2);
16139 else
16140 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16141 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016142 if (flags & SP_START)
16143 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016144
Bram Moolenaar071d4272004-06-13 20:20:40 +000016145 save_cursor = curwin->w_cursor;
16146 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016147 clearpos(&firstpos);
16148 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016149 pat = pat3;
16150 for (;;)
16151 {
16152 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016153 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016154 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16155 /* didn't find it or found the first match again: FAIL */
16156 break;
16157
16158 if (firstpos.lnum == 0)
16159 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016160 if (equalpos(pos, foundpos))
16161 {
16162 /* Found the same position again. Can happen with a pattern that
16163 * has "\zs" at the end and searching backwards. Advance one
16164 * character and try again. */
16165 if (dir == BACKWARD)
16166 decl(&pos);
16167 else
16168 incl(&pos);
16169 }
16170 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016171
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016172 /* clear the start flag to avoid getting stuck here */
16173 options &= ~SEARCH_START;
16174
Bram Moolenaar071d4272004-06-13 20:20:40 +000016175 /* If the skip pattern matches, ignore this match. */
16176 if (*skip != NUL)
16177 {
16178 save_pos = curwin->w_cursor;
16179 curwin->w_cursor = pos;
16180 r = eval_to_bool(skip, &err, NULL, FALSE);
16181 curwin->w_cursor = save_pos;
16182 if (err)
16183 {
16184 /* Evaluating {skip} caused an error, break here. */
16185 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016186 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016187 break;
16188 }
16189 if (r)
16190 continue;
16191 }
16192
16193 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16194 {
16195 /* Found end when searching backwards or start when searching
16196 * forward: nested pair. */
16197 ++nest;
16198 pat = pat2; /* nested, don't search for middle */
16199 }
16200 else
16201 {
16202 /* Found end when searching forward or start when searching
16203 * backward: end of (nested) pair; or found middle in outer pair. */
16204 if (--nest == 1)
16205 pat = pat3; /* outer level, search for middle */
16206 }
16207
16208 if (nest == 0)
16209 {
16210 /* Found the match: return matchcount or line number. */
16211 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016212 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016213 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016214 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016215 if (flags & SP_SETPCMARK)
16216 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016217 curwin->w_cursor = pos;
16218 if (!(flags & SP_REPEAT))
16219 break;
16220 nest = 1; /* search for next unmatched */
16221 }
16222 }
16223
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016224 if (match_pos != NULL)
16225 {
16226 /* Store the match cursor position */
16227 match_pos->lnum = curwin->w_cursor.lnum;
16228 match_pos->col = curwin->w_cursor.col + 1;
16229 }
16230
Bram Moolenaar071d4272004-06-13 20:20:40 +000016231 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016232 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016233 curwin->w_cursor = save_cursor;
16234
16235theend:
16236 vim_free(pat2);
16237 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016238 if (p_cpo == empty_option)
16239 p_cpo = save_cpo;
16240 else
16241 /* Darn, evaluating the {skip} expression changed the value. */
16242 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016243
16244 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016245}
16246
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016247/*
16248 * "searchpos()" function
16249 */
16250 static void
16251f_searchpos(argvars, rettv)
16252 typval_T *argvars;
16253 typval_T *rettv;
16254{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016255 pos_T match_pos;
16256 int lnum = 0;
16257 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016258 int n;
16259 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016260
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016261 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016262 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016263
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016264 n = search_cmn(argvars, &match_pos, &flags);
16265 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016266 {
16267 lnum = match_pos.lnum;
16268 col = match_pos.col;
16269 }
16270
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016271 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16272 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016273 if (flags & SP_SUBPAT)
16274 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016275}
16276
16277
Bram Moolenaar0d660222005-01-07 21:51:51 +000016278 static void
16279f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016280 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016281 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016283#ifdef FEAT_CLIENTSERVER
16284 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016285 char_u *server = get_tv_string_chk(&argvars[0]);
16286 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287
Bram Moolenaar0d660222005-01-07 21:51:51 +000016288 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016289 if (server == NULL || reply == NULL)
16290 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016291 if (check_restricted() || check_secure())
16292 return;
16293# ifdef FEAT_X11
16294 if (check_connection() == FAIL)
16295 return;
16296# endif
16297
16298 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016300 EMSG(_("E258: Unable to send to client"));
16301 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016302 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016303 rettv->vval.v_number = 0;
16304#else
16305 rettv->vval.v_number = -1;
16306#endif
16307}
16308
Bram Moolenaar0d660222005-01-07 21:51:51 +000016309 static void
16310f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016311 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016312 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016313{
16314 char_u *r = NULL;
16315
16316#ifdef FEAT_CLIENTSERVER
16317# ifdef WIN32
16318 r = serverGetVimNames();
16319# else
16320 make_connection();
16321 if (X_DISPLAY != NULL)
16322 r = serverGetVimNames(X_DISPLAY);
16323# endif
16324#endif
16325 rettv->v_type = VAR_STRING;
16326 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016327}
16328
16329/*
16330 * "setbufvar()" function
16331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016332 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016333f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016334 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016335 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016336{
16337 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016340 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341 char_u nbuf[NUMBUFLEN];
16342
16343 if (check_restricted() || check_secure())
16344 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016345 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16346 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016347 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016348 varp = &argvars[2];
16349
16350 if (buf != NULL && varname != NULL && varp != NULL)
16351 {
16352 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016354
16355 if (*varname == '&')
16356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016357 long numval;
16358 char_u *strval;
16359 int error = FALSE;
16360
Bram Moolenaar071d4272004-06-13 20:20:40 +000016361 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016362 numval = get_tv_number_chk(varp, &error);
16363 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016364 if (!error && strval != NULL)
16365 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016366 }
16367 else
16368 {
16369 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16370 if (bufvarname != NULL)
16371 {
16372 STRCPY(bufvarname, "b:");
16373 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016374 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016375 vim_free(bufvarname);
16376 }
16377 }
16378
16379 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016382}
16383
16384/*
16385 * "setcmdpos()" function
16386 */
16387 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016388f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016389 typval_T *argvars;
16390 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016391{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016392 int pos = (int)get_tv_number(&argvars[0]) - 1;
16393
16394 if (pos >= 0)
16395 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016396}
16397
16398/*
16399 * "setline()" function
16400 */
16401 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016402f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016403 typval_T *argvars;
16404 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016405{
16406 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016407 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016408 list_T *l = NULL;
16409 listitem_T *li = NULL;
16410 long added = 0;
16411 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016412
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016413 lnum = get_tv_lnum(&argvars[0]);
16414 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016415 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016416 l = argvars[1].vval.v_list;
16417 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016418 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016419 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016420 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016421
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016422 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016423 for (;;)
16424 {
16425 if (l != NULL)
16426 {
16427 /* list argument, get next string */
16428 if (li == NULL)
16429 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016430 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016431 li = li->li_next;
16432 }
16433
16434 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016435 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016436 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016437
16438 /* When coming here from Insert mode, sync undo, so that this can be
16439 * undone separately from what was previously inserted. */
16440 if (u_sync_once == 2)
16441 {
16442 u_sync_once = 1; /* notify that u_sync() was called */
16443 u_sync(TRUE);
16444 }
16445
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016446 if (lnum <= curbuf->b_ml.ml_line_count)
16447 {
16448 /* existing line, replace it */
16449 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16450 {
16451 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016452 if (lnum == curwin->w_cursor.lnum)
16453 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016454 rettv->vval.v_number = 0; /* OK */
16455 }
16456 }
16457 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16458 {
16459 /* lnum is one past the last line, append the line */
16460 ++added;
16461 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16462 rettv->vval.v_number = 0; /* OK */
16463 }
16464
16465 if (l == NULL) /* only one string argument */
16466 break;
16467 ++lnum;
16468 }
16469
16470 if (added > 0)
16471 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016472}
16473
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016474static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16475
Bram Moolenaar071d4272004-06-13 20:20:40 +000016476/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016477 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016478 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016479 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016480set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016481 win_T *wp UNUSED;
16482 typval_T *list_arg UNUSED;
16483 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016484 typval_T *rettv;
16485{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016486#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016487 char_u *act;
16488 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016489#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016490
Bram Moolenaar2641f772005-03-25 21:58:17 +000016491 rettv->vval.v_number = -1;
16492
16493#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016494 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016495 EMSG(_(e_listreq));
16496 else
16497 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016498 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016499
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016500 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016501 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016502 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016503 if (act == NULL)
16504 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016505 if (*act == 'a' || *act == 'r')
16506 action = *act;
16507 }
16508
Bram Moolenaar81484f42012-12-05 15:16:47 +010016509 if (l != NULL && set_errorlist(wp, l, action,
16510 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016511 rettv->vval.v_number = 0;
16512 }
16513#endif
16514}
16515
16516/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016517 * "setloclist()" function
16518 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016519 static void
16520f_setloclist(argvars, rettv)
16521 typval_T *argvars;
16522 typval_T *rettv;
16523{
16524 win_T *win;
16525
16526 rettv->vval.v_number = -1;
16527
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016528 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016529 if (win != NULL)
16530 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16531}
16532
16533/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016534 * "setmatches()" function
16535 */
16536 static void
16537f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016538 typval_T *argvars UNUSED;
16539 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016540{
16541#ifdef FEAT_SEARCH_EXTRA
16542 list_T *l;
16543 listitem_T *li;
16544 dict_T *d;
16545
16546 rettv->vval.v_number = -1;
16547 if (argvars[0].v_type != VAR_LIST)
16548 {
16549 EMSG(_(e_listreq));
16550 return;
16551 }
16552 if ((l = argvars[0].vval.v_list) != NULL)
16553 {
16554
16555 /* To some extent make sure that we are dealing with a list from
16556 * "getmatches()". */
16557 li = l->lv_first;
16558 while (li != NULL)
16559 {
16560 if (li->li_tv.v_type != VAR_DICT
16561 || (d = li->li_tv.vval.v_dict) == NULL)
16562 {
16563 EMSG(_(e_invarg));
16564 return;
16565 }
16566 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16567 && dict_find(d, (char_u *)"pattern", -1) != NULL
16568 && dict_find(d, (char_u *)"priority", -1) != NULL
16569 && dict_find(d, (char_u *)"id", -1) != NULL))
16570 {
16571 EMSG(_(e_invarg));
16572 return;
16573 }
16574 li = li->li_next;
16575 }
16576
16577 clear_matches(curwin);
16578 li = l->lv_first;
16579 while (li != NULL)
16580 {
16581 d = li->li_tv.vval.v_dict;
16582 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16583 get_dict_string(d, (char_u *)"pattern", FALSE),
16584 (int)get_dict_number(d, (char_u *)"priority"),
16585 (int)get_dict_number(d, (char_u *)"id"));
16586 li = li->li_next;
16587 }
16588 rettv->vval.v_number = 0;
16589 }
16590#endif
16591}
16592
16593/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016594 * "setpos()" function
16595 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016596 static void
16597f_setpos(argvars, rettv)
16598 typval_T *argvars;
16599 typval_T *rettv;
16600{
16601 pos_T pos;
16602 int fnum;
16603 char_u *name;
16604
Bram Moolenaar08250432008-02-13 11:42:46 +000016605 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016606 name = get_tv_string_chk(argvars);
16607 if (name != NULL)
16608 {
16609 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16610 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016611 if (--pos.col < 0)
16612 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016613 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016614 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016615 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016616 if (fnum == curbuf->b_fnum)
16617 {
16618 curwin->w_cursor = pos;
16619 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016620 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016621 }
16622 else
16623 EMSG(_(e_invarg));
16624 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016625 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16626 {
16627 /* set mark */
16628 if (setmark_pos(name[1], &pos, fnum) == OK)
16629 rettv->vval.v_number = 0;
16630 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016631 else
16632 EMSG(_(e_invarg));
16633 }
16634 }
16635}
16636
16637/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016638 * "setqflist()" function
16639 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016640 static void
16641f_setqflist(argvars, rettv)
16642 typval_T *argvars;
16643 typval_T *rettv;
16644{
16645 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16646}
16647
16648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016649 * "setreg()" function
16650 */
16651 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016652f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016653 typval_T *argvars;
16654 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016655{
16656 int regname;
16657 char_u *strregname;
16658 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016659 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016660 int append;
16661 char_u yank_type;
16662 long block_len;
16663
16664 block_len = -1;
16665 yank_type = MAUTO;
16666 append = FALSE;
16667
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016668 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016669 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016670
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016671 if (strregname == NULL)
16672 return; /* type error; errmsg already given */
16673 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016674 if (regname == 0 || regname == '@')
16675 regname = '"';
16676 else if (regname == '=')
16677 return;
16678
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016679 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016681 stropt = get_tv_string_chk(&argvars[2]);
16682 if (stropt == NULL)
16683 return; /* type error */
16684 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016685 switch (*stropt)
16686 {
16687 case 'a': case 'A': /* append */
16688 append = TRUE;
16689 break;
16690 case 'v': case 'c': /* character-wise selection */
16691 yank_type = MCHAR;
16692 break;
16693 case 'V': case 'l': /* line-wise selection */
16694 yank_type = MLINE;
16695 break;
16696#ifdef FEAT_VISUAL
16697 case 'b': case Ctrl_V: /* block-wise selection */
16698 yank_type = MBLOCK;
16699 if (VIM_ISDIGIT(stropt[1]))
16700 {
16701 ++stropt;
16702 block_len = getdigits(&stropt) - 1;
16703 --stropt;
16704 }
16705 break;
16706#endif
16707 }
16708 }
16709
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016710 strval = get_tv_string_chk(&argvars[1]);
16711 if (strval != NULL)
16712 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016713 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016714 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016715}
16716
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016717/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016718 * "settabvar()" function
16719 */
16720 static void
16721f_settabvar(argvars, rettv)
16722 typval_T *argvars;
16723 typval_T *rettv;
16724{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016725#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016726 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016727 tabpage_T *tp;
16728#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016729 char_u *varname, *tabvarname;
16730 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016731
16732 rettv->vval.v_number = 0;
16733
16734 if (check_restricted() || check_secure())
16735 return;
16736
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016737#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016738 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016739#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016740 varname = get_tv_string_chk(&argvars[1]);
16741 varp = &argvars[2];
16742
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016743 if (varname != NULL && varp != NULL
16744#ifdef FEAT_WINDOWS
16745 && tp != NULL
16746#endif
16747 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016748 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016749#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016750 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016751 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016752#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016753
16754 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16755 if (tabvarname != NULL)
16756 {
16757 STRCPY(tabvarname, "t:");
16758 STRCPY(tabvarname + 2, varname);
16759 set_var(tabvarname, varp, TRUE);
16760 vim_free(tabvarname);
16761 }
16762
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016763#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016764 /* Restore current tabpage */
16765 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016766 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016767#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016768 }
16769}
16770
16771/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016772 * "settabwinvar()" function
16773 */
16774 static void
16775f_settabwinvar(argvars, rettv)
16776 typval_T *argvars;
16777 typval_T *rettv;
16778{
16779 setwinvar(argvars, rettv, 1);
16780}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781
16782/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016783 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016786f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016787 typval_T *argvars;
16788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016789{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016790 setwinvar(argvars, rettv, 0);
16791}
16792
16793/*
16794 * "setwinvar()" and "settabwinvar()" functions
16795 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016796
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016797 static void
16798setwinvar(argvars, rettv, off)
16799 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016800 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016801 int off;
16802{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016803 win_T *win;
16804#ifdef FEAT_WINDOWS
16805 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016806 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016807#endif
16808 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016809 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016810 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016811 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016812
16813 if (check_restricted() || check_secure())
16814 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016815
16816#ifdef FEAT_WINDOWS
16817 if (off == 1)
16818 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16819 else
16820 tp = curtab;
16821#endif
16822 win = find_win_by_nr(&argvars[off], tp);
16823 varname = get_tv_string_chk(&argvars[off + 1]);
16824 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825
16826 if (win != NULL && varname != NULL && varp != NULL)
16827 {
16828#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016829 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016830 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016831#endif
16832
16833 if (*varname == '&')
16834 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016835 long numval;
16836 char_u *strval;
16837 int error = FALSE;
16838
Bram Moolenaar071d4272004-06-13 20:20:40 +000016839 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016840 numval = get_tv_number_chk(varp, &error);
16841 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016842 if (!error && strval != NULL)
16843 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016844 }
16845 else
16846 {
16847 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16848 if (winvarname != NULL)
16849 {
16850 STRCPY(winvarname, "w:");
16851 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016852 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 vim_free(winvarname);
16854 }
16855 }
16856
16857#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016858 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016859#endif
16860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016861}
16862
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016863#ifdef FEAT_CRYPT
16864/*
16865 * "sha256({string})" function
16866 */
16867 static void
16868f_sha256(argvars, rettv)
16869 typval_T *argvars;
16870 typval_T *rettv;
16871{
16872 char_u *p;
16873
16874 p = get_tv_string(&argvars[0]);
16875 rettv->vval.v_string = vim_strsave(
16876 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16877 rettv->v_type = VAR_STRING;
16878}
16879#endif /* FEAT_CRYPT */
16880
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016882 * "shellescape({string})" function
16883 */
16884 static void
16885f_shellescape(argvars, rettv)
16886 typval_T *argvars;
16887 typval_T *rettv;
16888{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016889 rettv->vval.v_string = vim_strsave_shellescape(
16890 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016891 rettv->v_type = VAR_STRING;
16892}
16893
16894/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016895 * shiftwidth() function
16896 */
16897 static void
16898f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016899 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016900 typval_T *rettv;
16901{
16902 rettv->vval.v_number = get_sw_value();
16903}
16904
16905/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016906 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016907 */
16908 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016909f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016910 typval_T *argvars;
16911 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016912{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016913 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016914
Bram Moolenaar0d660222005-01-07 21:51:51 +000016915 p = get_tv_string(&argvars[0]);
16916 rettv->vval.v_string = vim_strsave(p);
16917 simplify_filename(rettv->vval.v_string); /* simplify in place */
16918 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919}
16920
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016921#ifdef FEAT_FLOAT
16922/*
16923 * "sin()" function
16924 */
16925 static void
16926f_sin(argvars, rettv)
16927 typval_T *argvars;
16928 typval_T *rettv;
16929{
16930 float_T f;
16931
16932 rettv->v_type = VAR_FLOAT;
16933 if (get_float_arg(argvars, &f) == OK)
16934 rettv->vval.v_float = sin(f);
16935 else
16936 rettv->vval.v_float = 0.0;
16937}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016938
16939/*
16940 * "sinh()" function
16941 */
16942 static void
16943f_sinh(argvars, rettv)
16944 typval_T *argvars;
16945 typval_T *rettv;
16946{
16947 float_T f;
16948
16949 rettv->v_type = VAR_FLOAT;
16950 if (get_float_arg(argvars, &f) == OK)
16951 rettv->vval.v_float = sinh(f);
16952 else
16953 rettv->vval.v_float = 0.0;
16954}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016955#endif
16956
Bram Moolenaar0d660222005-01-07 21:51:51 +000016957static int
16958#ifdef __BORLANDC__
16959 _RTLENTRYF
16960#endif
16961 item_compare __ARGS((const void *s1, const void *s2));
16962static int
16963#ifdef __BORLANDC__
16964 _RTLENTRYF
16965#endif
16966 item_compare2 __ARGS((const void *s1, const void *s2));
16967
16968static int item_compare_ic;
16969static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016970static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016971static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972#define ITEM_COMPARE_FAIL 999
16973
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016977 static int
16978#ifdef __BORLANDC__
16979_RTLENTRYF
16980#endif
16981item_compare(s1, s2)
16982 const void *s1;
16983 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016984{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016985 char_u *p1, *p2;
16986 char_u *tofree1, *tofree2;
16987 int res;
16988 char_u numbuf1[NUMBUFLEN];
16989 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016991 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16992 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016993 if (p1 == NULL)
16994 p1 = (char_u *)"";
16995 if (p2 == NULL)
16996 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016997 if (item_compare_ic)
16998 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016999 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017000 res = STRCMP(p1, p2);
17001 vim_free(tofree1);
17002 vim_free(tofree2);
17003 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017004}
17005
17006 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017007#ifdef __BORLANDC__
17008_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017009#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017010item_compare2(s1, s2)
17011 const void *s1;
17012 const void *s2;
17013{
17014 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017015 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017016 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017017 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017019 /* shortcut after failure in previous call; compare all items equal */
17020 if (item_compare_func_err)
17021 return 0;
17022
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017023 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17024 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017025 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17026 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017027
17028 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017029 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017030 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17031 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017032 clear_tv(&argv[0]);
17033 clear_tv(&argv[1]);
17034
17035 if (res == FAIL)
17036 res = ITEM_COMPARE_FAIL;
17037 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017038 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17039 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017040 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017041 clear_tv(&rettv);
17042 return res;
17043}
17044
17045/*
17046 * "sort({list})" function
17047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017048 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017049f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017050 typval_T *argvars;
17051 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017052{
Bram Moolenaar33570922005-01-25 22:26:29 +000017053 list_T *l;
17054 listitem_T *li;
17055 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017056 long len;
17057 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017058
Bram Moolenaar0d660222005-01-07 21:51:51 +000017059 if (argvars[0].v_type != VAR_LIST)
17060 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017061 else
17062 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017063 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017064 if (l == NULL || tv_check_lock(l->lv_lock,
17065 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017066 return;
17067 rettv->vval.v_list = l;
17068 rettv->v_type = VAR_LIST;
17069 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070
Bram Moolenaar0d660222005-01-07 21:51:51 +000017071 len = list_len(l);
17072 if (len <= 1)
17073 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074
Bram Moolenaar0d660222005-01-07 21:51:51 +000017075 item_compare_ic = FALSE;
17076 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017077 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017078 if (argvars[1].v_type != VAR_UNKNOWN)
17079 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017080 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017081 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017082 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017083 else
17084 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017085 int error = FALSE;
17086
17087 i = get_tv_number_chk(&argvars[1], &error);
17088 if (error)
17089 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017090 if (i == 1)
17091 item_compare_ic = TRUE;
17092 else
17093 item_compare_func = get_tv_string(&argvars[1]);
17094 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017095
17096 if (argvars[2].v_type != VAR_UNKNOWN)
17097 {
17098 /* optional third argument: {dict} */
17099 if (argvars[2].v_type != VAR_DICT)
17100 {
17101 EMSG(_(e_dictreq));
17102 return;
17103 }
17104 item_compare_selfdict = argvars[2].vval.v_dict;
17105 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017107
Bram Moolenaar0d660222005-01-07 21:51:51 +000017108 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017109 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017110 if (ptrs == NULL)
17111 return;
17112 i = 0;
17113 for (li = l->lv_first; li != NULL; li = li->li_next)
17114 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017115
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017116 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017117 /* test the compare function */
17118 if (item_compare_func != NULL
17119 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17120 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017121 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017122 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017123 {
17124 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017125 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017126 item_compare_func == NULL ? item_compare : item_compare2);
17127
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017128 if (!item_compare_func_err)
17129 {
17130 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017131 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017132 l->lv_len = 0;
17133 for (i = 0; i < len; ++i)
17134 list_append(l, ptrs[i]);
17135 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017136 }
17137
17138 vim_free(ptrs);
17139 }
17140}
17141
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017142/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017143 * "soundfold({word})" function
17144 */
17145 static void
17146f_soundfold(argvars, rettv)
17147 typval_T *argvars;
17148 typval_T *rettv;
17149{
17150 char_u *s;
17151
17152 rettv->v_type = VAR_STRING;
17153 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017154#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017155 rettv->vval.v_string = eval_soundfold(s);
17156#else
17157 rettv->vval.v_string = vim_strsave(s);
17158#endif
17159}
17160
17161/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017162 * "spellbadword()" function
17163 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017164 static void
17165f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017166 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017167 typval_T *rettv;
17168{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017169 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017170 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017171 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017172
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017173 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017174 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017175
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017176#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017177 if (argvars[0].v_type == VAR_UNKNOWN)
17178 {
17179 /* Find the start and length of the badly spelled word. */
17180 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17181 if (len != 0)
17182 word = ml_get_cursor();
17183 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017184 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017185 {
17186 char_u *str = get_tv_string_chk(&argvars[0]);
17187 int capcol = -1;
17188
17189 if (str != NULL)
17190 {
17191 /* Check the argument for spelling. */
17192 while (*str != NUL)
17193 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017194 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017195 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017196 {
17197 word = str;
17198 break;
17199 }
17200 str += len;
17201 }
17202 }
17203 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017204#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017205
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017206 list_append_string(rettv->vval.v_list, word, len);
17207 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017208 attr == HLF_SPB ? "bad" :
17209 attr == HLF_SPR ? "rare" :
17210 attr == HLF_SPL ? "local" :
17211 attr == HLF_SPC ? "caps" :
17212 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017213}
17214
17215/*
17216 * "spellsuggest()" function
17217 */
17218 static void
17219f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017220 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017221 typval_T *rettv;
17222{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017223#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017224 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017225 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017226 int maxcount;
17227 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017228 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017229 listitem_T *li;
17230 int need_capital = FALSE;
17231#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017232
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017233 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017234 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017235
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017236#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017237 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017238 {
17239 str = get_tv_string(&argvars[0]);
17240 if (argvars[1].v_type != VAR_UNKNOWN)
17241 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017242 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017243 if (maxcount <= 0)
17244 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017245 if (argvars[2].v_type != VAR_UNKNOWN)
17246 {
17247 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17248 if (typeerr)
17249 return;
17250 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017251 }
17252 else
17253 maxcount = 25;
17254
Bram Moolenaar4770d092006-01-12 23:22:24 +000017255 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017256
17257 for (i = 0; i < ga.ga_len; ++i)
17258 {
17259 str = ((char_u **)ga.ga_data)[i];
17260
17261 li = listitem_alloc();
17262 if (li == NULL)
17263 vim_free(str);
17264 else
17265 {
17266 li->li_tv.v_type = VAR_STRING;
17267 li->li_tv.v_lock = 0;
17268 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017269 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017270 }
17271 }
17272 ga_clear(&ga);
17273 }
17274#endif
17275}
17276
Bram Moolenaar0d660222005-01-07 21:51:51 +000017277 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017278f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017279 typval_T *argvars;
17280 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017281{
17282 char_u *str;
17283 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017284 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017285 regmatch_T regmatch;
17286 char_u patbuf[NUMBUFLEN];
17287 char_u *save_cpo;
17288 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017289 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017290 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017291 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017292
17293 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17294 save_cpo = p_cpo;
17295 p_cpo = (char_u *)"";
17296
17297 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017298 if (argvars[1].v_type != VAR_UNKNOWN)
17299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017300 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17301 if (pat == NULL)
17302 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017303 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017304 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017305 }
17306 if (pat == NULL || *pat == NUL)
17307 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017308
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017309 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017311 if (typeerr)
17312 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017313
Bram Moolenaar0d660222005-01-07 21:51:51 +000017314 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17315 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017317 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017318 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017319 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017320 if (*str == NUL)
17321 match = FALSE; /* empty item at the end */
17322 else
17323 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017324 if (match)
17325 end = regmatch.startp[0];
17326 else
17327 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017328 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17329 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017330 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017331 if (list_append_string(rettv->vval.v_list, str,
17332 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017333 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017334 }
17335 if (!match)
17336 break;
17337 /* Advance to just after the match. */
17338 if (regmatch.endp[0] > str)
17339 col = 0;
17340 else
17341 {
17342 /* Don't get stuck at the same match. */
17343#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017344 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017345#else
17346 col = 1;
17347#endif
17348 }
17349 str = regmatch.endp[0];
17350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017351
Bram Moolenaar473de612013-06-08 18:19:48 +020017352 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354
Bram Moolenaar0d660222005-01-07 21:51:51 +000017355 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356}
17357
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017358#ifdef FEAT_FLOAT
17359/*
17360 * "sqrt()" function
17361 */
17362 static void
17363f_sqrt(argvars, rettv)
17364 typval_T *argvars;
17365 typval_T *rettv;
17366{
17367 float_T f;
17368
17369 rettv->v_type = VAR_FLOAT;
17370 if (get_float_arg(argvars, &f) == OK)
17371 rettv->vval.v_float = sqrt(f);
17372 else
17373 rettv->vval.v_float = 0.0;
17374}
17375
17376/*
17377 * "str2float()" function
17378 */
17379 static void
17380f_str2float(argvars, rettv)
17381 typval_T *argvars;
17382 typval_T *rettv;
17383{
17384 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17385
17386 if (*p == '+')
17387 p = skipwhite(p + 1);
17388 (void)string2float(p, &rettv->vval.v_float);
17389 rettv->v_type = VAR_FLOAT;
17390}
17391#endif
17392
Bram Moolenaar2c932302006-03-18 21:42:09 +000017393/*
17394 * "str2nr()" function
17395 */
17396 static void
17397f_str2nr(argvars, rettv)
17398 typval_T *argvars;
17399 typval_T *rettv;
17400{
17401 int base = 10;
17402 char_u *p;
17403 long n;
17404
17405 if (argvars[1].v_type != VAR_UNKNOWN)
17406 {
17407 base = get_tv_number(&argvars[1]);
17408 if (base != 8 && base != 10 && base != 16)
17409 {
17410 EMSG(_(e_invarg));
17411 return;
17412 }
17413 }
17414
17415 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017416 if (*p == '+')
17417 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017418 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17419 rettv->vval.v_number = n;
17420}
17421
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422#ifdef HAVE_STRFTIME
17423/*
17424 * "strftime({format}[, {time}])" function
17425 */
17426 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017427f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017428 typval_T *argvars;
17429 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430{
17431 char_u result_buf[256];
17432 struct tm *curtime;
17433 time_t seconds;
17434 char_u *p;
17435
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017436 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017438 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017439 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440 seconds = time(NULL);
17441 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017442 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017443 curtime = localtime(&seconds);
17444 /* MSVC returns NULL for an invalid value of seconds. */
17445 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017446 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017447 else
17448 {
17449# ifdef FEAT_MBYTE
17450 vimconv_T conv;
17451 char_u *enc;
17452
17453 conv.vc_type = CONV_NONE;
17454 enc = enc_locale();
17455 convert_setup(&conv, p_enc, enc);
17456 if (conv.vc_type != CONV_NONE)
17457 p = string_convert(&conv, p, NULL);
17458# endif
17459 if (p != NULL)
17460 (void)strftime((char *)result_buf, sizeof(result_buf),
17461 (char *)p, curtime);
17462 else
17463 result_buf[0] = NUL;
17464
17465# ifdef FEAT_MBYTE
17466 if (conv.vc_type != CONV_NONE)
17467 vim_free(p);
17468 convert_setup(&conv, enc, p_enc);
17469 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017470 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 else
17472# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017473 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474
17475# ifdef FEAT_MBYTE
17476 /* Release conversion descriptors */
17477 convert_setup(&conv, NULL, NULL);
17478 vim_free(enc);
17479# endif
17480 }
17481}
17482#endif
17483
17484/*
17485 * "stridx()" function
17486 */
17487 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017488f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017489 typval_T *argvars;
17490 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491{
17492 char_u buf[NUMBUFLEN];
17493 char_u *needle;
17494 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017495 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017496 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017497 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017499 needle = get_tv_string_chk(&argvars[1]);
17500 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017501 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017502 if (needle == NULL || haystack == NULL)
17503 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017504
Bram Moolenaar33570922005-01-25 22:26:29 +000017505 if (argvars[2].v_type != VAR_UNKNOWN)
17506 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017507 int error = FALSE;
17508
17509 start_idx = get_tv_number_chk(&argvars[2], &error);
17510 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017511 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017512 if (start_idx >= 0)
17513 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017514 }
17515
17516 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17517 if (pos != NULL)
17518 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017519}
17520
17521/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017522 * "string()" function
17523 */
17524 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017525f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017526 typval_T *argvars;
17527 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017528{
17529 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017530 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017532 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017533 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017534 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017535 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017536 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017537}
17538
17539/*
17540 * "strlen()" function
17541 */
17542 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017543f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017544 typval_T *argvars;
17545 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017547 rettv->vval.v_number = (varnumber_T)(STRLEN(
17548 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017549}
17550
17551/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017552 * "strchars()" function
17553 */
17554 static void
17555f_strchars(argvars, rettv)
17556 typval_T *argvars;
17557 typval_T *rettv;
17558{
17559 char_u *s = get_tv_string(&argvars[0]);
17560#ifdef FEAT_MBYTE
17561 varnumber_T len = 0;
17562
17563 while (*s != NUL)
17564 {
17565 mb_cptr2char_adv(&s);
17566 ++len;
17567 }
17568 rettv->vval.v_number = len;
17569#else
17570 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17571#endif
17572}
17573
17574/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017575 * "strdisplaywidth()" function
17576 */
17577 static void
17578f_strdisplaywidth(argvars, rettv)
17579 typval_T *argvars;
17580 typval_T *rettv;
17581{
17582 char_u *s = get_tv_string(&argvars[0]);
17583 int col = 0;
17584
17585 if (argvars[1].v_type != VAR_UNKNOWN)
17586 col = get_tv_number(&argvars[1]);
17587
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017588 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017589}
17590
17591/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017592 * "strwidth()" function
17593 */
17594 static void
17595f_strwidth(argvars, rettv)
17596 typval_T *argvars;
17597 typval_T *rettv;
17598{
17599 char_u *s = get_tv_string(&argvars[0]);
17600
17601 rettv->vval.v_number = (varnumber_T)(
17602#ifdef FEAT_MBYTE
17603 mb_string2cells(s, -1)
17604#else
17605 STRLEN(s)
17606#endif
17607 );
17608}
17609
17610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017611 * "strpart()" function
17612 */
17613 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017614f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017615 typval_T *argvars;
17616 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017617{
17618 char_u *p;
17619 int n;
17620 int len;
17621 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017622 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017624 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 slen = (int)STRLEN(p);
17626
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017627 n = get_tv_number_chk(&argvars[1], &error);
17628 if (error)
17629 len = 0;
17630 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017631 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017632 else
17633 len = slen - n; /* default len: all bytes that are available. */
17634
17635 /*
17636 * Only return the overlap between the specified part and the actual
17637 * string.
17638 */
17639 if (n < 0)
17640 {
17641 len += n;
17642 n = 0;
17643 }
17644 else if (n > slen)
17645 n = slen;
17646 if (len < 0)
17647 len = 0;
17648 else if (n + len > slen)
17649 len = slen - n;
17650
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017651 rettv->v_type = VAR_STRING;
17652 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653}
17654
17655/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017656 * "strridx()" function
17657 */
17658 static void
17659f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017660 typval_T *argvars;
17661 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017662{
17663 char_u buf[NUMBUFLEN];
17664 char_u *needle;
17665 char_u *haystack;
17666 char_u *rest;
17667 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017668 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017670 needle = get_tv_string_chk(&argvars[1]);
17671 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017672
17673 rettv->vval.v_number = -1;
17674 if (needle == NULL || haystack == NULL)
17675 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017676
17677 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017678 if (argvars[2].v_type != VAR_UNKNOWN)
17679 {
17680 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017681 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017682 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017683 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017684 }
17685 else
17686 end_idx = haystack_len;
17687
Bram Moolenaar0d660222005-01-07 21:51:51 +000017688 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017689 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017690 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017691 lastmatch = haystack + end_idx;
17692 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017693 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017694 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017695 for (rest = haystack; *rest != '\0'; ++rest)
17696 {
17697 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017698 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017699 break;
17700 lastmatch = rest;
17701 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017702 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017703
17704 if (lastmatch == NULL)
17705 rettv->vval.v_number = -1;
17706 else
17707 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17708}
17709
17710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711 * "strtrans()" function
17712 */
17713 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017714f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017715 typval_T *argvars;
17716 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017718 rettv->v_type = VAR_STRING;
17719 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017720}
17721
17722/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017723 * "submatch()" function
17724 */
17725 static void
17726f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017727 typval_T *argvars;
17728 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017729{
17730 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017731 rettv->vval.v_string =
17732 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017733}
17734
17735/*
17736 * "substitute()" function
17737 */
17738 static void
17739f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017740 typval_T *argvars;
17741 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017742{
17743 char_u patbuf[NUMBUFLEN];
17744 char_u subbuf[NUMBUFLEN];
17745 char_u flagsbuf[NUMBUFLEN];
17746
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017747 char_u *str = get_tv_string_chk(&argvars[0]);
17748 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17749 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17750 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17751
Bram Moolenaar0d660222005-01-07 21:51:51 +000017752 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017753 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17754 rettv->vval.v_string = NULL;
17755 else
17756 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017757}
17758
17759/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017760 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017763f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017764 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017765 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766{
17767 int id = 0;
17768#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017769 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770 long col;
17771 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017772 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017774 lnum = get_tv_lnum(argvars); /* -1 on type error */
17775 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17776 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017778 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017779 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017780 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017781#endif
17782
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017783 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784}
17785
17786/*
17787 * "synIDattr(id, what [, mode])" function
17788 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017790f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017791 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017792 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017793{
17794 char_u *p = NULL;
17795#ifdef FEAT_SYN_HL
17796 int id;
17797 char_u *what;
17798 char_u *mode;
17799 char_u modebuf[NUMBUFLEN];
17800 int modec;
17801
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017802 id = get_tv_number(&argvars[0]);
17803 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017804 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017806 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017808 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017809 modec = 0; /* replace invalid with current */
17810 }
17811 else
17812 {
17813#ifdef FEAT_GUI
17814 if (gui.in_use)
17815 modec = 'g';
17816 else
17817#endif
17818 if (t_colors > 1)
17819 modec = 'c';
17820 else
17821 modec = 't';
17822 }
17823
17824
17825 switch (TOLOWER_ASC(what[0]))
17826 {
17827 case 'b':
17828 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17829 p = highlight_color(id, what, modec);
17830 else /* bold */
17831 p = highlight_has_attr(id, HL_BOLD, modec);
17832 break;
17833
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017834 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017835 p = highlight_color(id, what, modec);
17836 break;
17837
17838 case 'i':
17839 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17840 p = highlight_has_attr(id, HL_INVERSE, modec);
17841 else /* italic */
17842 p = highlight_has_attr(id, HL_ITALIC, modec);
17843 break;
17844
17845 case 'n': /* name */
17846 p = get_highlight_name(NULL, id - 1);
17847 break;
17848
17849 case 'r': /* reverse */
17850 p = highlight_has_attr(id, HL_INVERSE, modec);
17851 break;
17852
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017853 case 's':
17854 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17855 p = highlight_color(id, what, modec);
17856 else /* standout */
17857 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858 break;
17859
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017860 case 'u':
17861 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17862 /* underline */
17863 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17864 else
17865 /* undercurl */
17866 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017867 break;
17868 }
17869
17870 if (p != NULL)
17871 p = vim_strsave(p);
17872#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017873 rettv->v_type = VAR_STRING;
17874 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017875}
17876
17877/*
17878 * "synIDtrans(id)" function
17879 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017880 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017881f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017882 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017884{
17885 int id;
17886
17887#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017888 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017889
17890 if (id > 0)
17891 id = syn_get_final_id(id);
17892 else
17893#endif
17894 id = 0;
17895
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017896 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017897}
17898
17899/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017900 * "synconcealed(lnum, col)" function
17901 */
17902 static void
17903f_synconcealed(argvars, rettv)
17904 typval_T *argvars UNUSED;
17905 typval_T *rettv;
17906{
17907#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17908 long lnum;
17909 long col;
17910 int syntax_flags = 0;
17911 int cchar;
17912 int matchid = 0;
17913 char_u str[NUMBUFLEN];
17914#endif
17915
17916 rettv->v_type = VAR_LIST;
17917 rettv->vval.v_list = NULL;
17918
17919#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17920 lnum = get_tv_lnum(argvars); /* -1 on type error */
17921 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17922
17923 vim_memset(str, NUL, sizeof(str));
17924
17925 if (rettv_list_alloc(rettv) != FAIL)
17926 {
17927 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17928 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17929 && curwin->w_p_cole > 0)
17930 {
17931 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17932 syntax_flags = get_syntax_info(&matchid);
17933
17934 /* get the conceal character */
17935 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17936 {
17937 cchar = syn_get_sub_char();
17938 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17939 cchar = lcs_conceal;
17940 if (cchar != NUL)
17941 {
17942# ifdef FEAT_MBYTE
17943 if (has_mbyte)
17944 (*mb_char2bytes)(cchar, str);
17945 else
17946# endif
17947 str[0] = cchar;
17948 }
17949 }
17950 }
17951
17952 list_append_number(rettv->vval.v_list,
17953 (syntax_flags & HL_CONCEAL) != 0);
17954 /* -1 to auto-determine strlen */
17955 list_append_string(rettv->vval.v_list, str, -1);
17956 list_append_number(rettv->vval.v_list, matchid);
17957 }
17958#endif
17959}
17960
17961/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017962 * "synstack(lnum, col)" function
17963 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017964 static void
17965f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017966 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017967 typval_T *rettv;
17968{
17969#ifdef FEAT_SYN_HL
17970 long lnum;
17971 long col;
17972 int i;
17973 int id;
17974#endif
17975
17976 rettv->v_type = VAR_LIST;
17977 rettv->vval.v_list = NULL;
17978
17979#ifdef FEAT_SYN_HL
17980 lnum = get_tv_lnum(argvars); /* -1 on type error */
17981 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17982
17983 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017984 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017985 && rettv_list_alloc(rettv) != FAIL)
17986 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017987 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017988 for (i = 0; ; ++i)
17989 {
17990 id = syn_get_stack_item(i);
17991 if (id < 0)
17992 break;
17993 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17994 break;
17995 }
17996 }
17997#endif
17998}
17999
18000/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018001 * "system()" function
18002 */
18003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018004f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018005 typval_T *argvars;
18006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018007{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018008 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018010 char_u *infile = NULL;
18011 char_u buf[NUMBUFLEN];
18012 int err = FALSE;
18013 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018014
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018015 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018016 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018017
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018018 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018019 {
18020 /*
18021 * Write the string to a temp file, to be used for input of the shell
18022 * command.
18023 */
18024 if ((infile = vim_tempname('i')) == NULL)
18025 {
18026 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018027 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018028 }
18029
18030 fd = mch_fopen((char *)infile, WRITEBIN);
18031 if (fd == NULL)
18032 {
18033 EMSG2(_(e_notopen), infile);
18034 goto done;
18035 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018036 p = get_tv_string_buf_chk(&argvars[1], buf);
18037 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018038 {
18039 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018040 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018041 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018042 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18043 err = TRUE;
18044 if (fclose(fd) != 0)
18045 err = TRUE;
18046 if (err)
18047 {
18048 EMSG(_("E677: Error writing temp file"));
18049 goto done;
18050 }
18051 }
18052
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018053 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18054 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018055
Bram Moolenaar071d4272004-06-13 20:20:40 +000018056#ifdef USE_CR
18057 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018058 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018059 {
18060 char_u *s;
18061
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018062 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018063 {
18064 if (*s == CAR)
18065 *s = NL;
18066 }
18067 }
18068#else
18069# ifdef USE_CRNL
18070 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018071 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018072 {
18073 char_u *s, *d;
18074
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018075 d = res;
18076 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018077 {
18078 if (s[0] == CAR && s[1] == NL)
18079 ++s;
18080 *d++ = *s;
18081 }
18082 *d = NUL;
18083 }
18084# endif
18085#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018086
18087done:
18088 if (infile != NULL)
18089 {
18090 mch_remove(infile);
18091 vim_free(infile);
18092 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018093 rettv->v_type = VAR_STRING;
18094 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018095}
18096
18097/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018098 * "tabpagebuflist()" function
18099 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018100 static void
18101f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018102 typval_T *argvars UNUSED;
18103 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018104{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018105#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018106 tabpage_T *tp;
18107 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018108
18109 if (argvars[0].v_type == VAR_UNKNOWN)
18110 wp = firstwin;
18111 else
18112 {
18113 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18114 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018115 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018116 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018117 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018118 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018119 for (; wp != NULL; wp = wp->w_next)
18120 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018121 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018122 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018123 }
18124#endif
18125}
18126
18127
18128/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018129 * "tabpagenr()" function
18130 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018131 static void
18132f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018133 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018134 typval_T *rettv;
18135{
18136 int nr = 1;
18137#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018138 char_u *arg;
18139
18140 if (argvars[0].v_type != VAR_UNKNOWN)
18141 {
18142 arg = get_tv_string_chk(&argvars[0]);
18143 nr = 0;
18144 if (arg != NULL)
18145 {
18146 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018147 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018148 else
18149 EMSG2(_(e_invexpr2), arg);
18150 }
18151 }
18152 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018153 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018154#endif
18155 rettv->vval.v_number = nr;
18156}
18157
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018158
18159#ifdef FEAT_WINDOWS
18160static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18161
18162/*
18163 * Common code for tabpagewinnr() and winnr().
18164 */
18165 static int
18166get_winnr(tp, argvar)
18167 tabpage_T *tp;
18168 typval_T *argvar;
18169{
18170 win_T *twin;
18171 int nr = 1;
18172 win_T *wp;
18173 char_u *arg;
18174
18175 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18176 if (argvar->v_type != VAR_UNKNOWN)
18177 {
18178 arg = get_tv_string_chk(argvar);
18179 if (arg == NULL)
18180 nr = 0; /* type error; errmsg already given */
18181 else if (STRCMP(arg, "$") == 0)
18182 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18183 else if (STRCMP(arg, "#") == 0)
18184 {
18185 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18186 if (twin == NULL)
18187 nr = 0;
18188 }
18189 else
18190 {
18191 EMSG2(_(e_invexpr2), arg);
18192 nr = 0;
18193 }
18194 }
18195
18196 if (nr > 0)
18197 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18198 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018199 {
18200 if (wp == NULL)
18201 {
18202 /* didn't find it in this tabpage */
18203 nr = 0;
18204 break;
18205 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018206 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018207 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018208 return nr;
18209}
18210#endif
18211
18212/*
18213 * "tabpagewinnr()" function
18214 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018215 static void
18216f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018217 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018218 typval_T *rettv;
18219{
18220 int nr = 1;
18221#ifdef FEAT_WINDOWS
18222 tabpage_T *tp;
18223
18224 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18225 if (tp == NULL)
18226 nr = 0;
18227 else
18228 nr = get_winnr(tp, &argvars[1]);
18229#endif
18230 rettv->vval.v_number = nr;
18231}
18232
18233
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018234/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018235 * "tagfiles()" function
18236 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018237 static void
18238f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018239 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018240 typval_T *rettv;
18241{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018242 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018243 tagname_T tn;
18244 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018245
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018246 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018247 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018248 fname = alloc(MAXPATHL);
18249 if (fname == NULL)
18250 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018251
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018252 for (first = TRUE; ; first = FALSE)
18253 if (get_tagfname(&tn, first, fname) == FAIL
18254 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018255 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018256 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018257 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018258}
18259
18260/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018261 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018262 */
18263 static void
18264f_taglist(argvars, rettv)
18265 typval_T *argvars;
18266 typval_T *rettv;
18267{
18268 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018269
18270 tag_pattern = get_tv_string(&argvars[0]);
18271
18272 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018273 if (*tag_pattern == NUL)
18274 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018275
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018276 if (rettv_list_alloc(rettv) == OK)
18277 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018278}
18279
18280/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018281 * "tempname()" function
18282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018283 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018284f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018285 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018286 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018287{
18288 static int x = 'A';
18289
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018290 rettv->v_type = VAR_STRING;
18291 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018292
18293 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18294 * names. Skip 'I' and 'O', they are used for shell redirection. */
18295 do
18296 {
18297 if (x == 'Z')
18298 x = '0';
18299 else if (x == '9')
18300 x = 'A';
18301 else
18302 {
18303#ifdef EBCDIC
18304 if (x == 'I')
18305 x = 'J';
18306 else if (x == 'R')
18307 x = 'S';
18308 else
18309#endif
18310 ++x;
18311 }
18312 } while (x == 'I' || x == 'O');
18313}
18314
18315/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018316 * "test(list)" function: Just checking the walls...
18317 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018318 static void
18319f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018320 typval_T *argvars UNUSED;
18321 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018322{
18323 /* Used for unit testing. Change the code below to your liking. */
18324#if 0
18325 listitem_T *li;
18326 list_T *l;
18327 char_u *bad, *good;
18328
18329 if (argvars[0].v_type != VAR_LIST)
18330 return;
18331 l = argvars[0].vval.v_list;
18332 if (l == NULL)
18333 return;
18334 li = l->lv_first;
18335 if (li == NULL)
18336 return;
18337 bad = get_tv_string(&li->li_tv);
18338 li = li->li_next;
18339 if (li == NULL)
18340 return;
18341 good = get_tv_string(&li->li_tv);
18342 rettv->vval.v_number = test_edit_score(bad, good);
18343#endif
18344}
18345
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018346#ifdef FEAT_FLOAT
18347/*
18348 * "tan()" function
18349 */
18350 static void
18351f_tan(argvars, rettv)
18352 typval_T *argvars;
18353 typval_T *rettv;
18354{
18355 float_T f;
18356
18357 rettv->v_type = VAR_FLOAT;
18358 if (get_float_arg(argvars, &f) == OK)
18359 rettv->vval.v_float = tan(f);
18360 else
18361 rettv->vval.v_float = 0.0;
18362}
18363
18364/*
18365 * "tanh()" function
18366 */
18367 static void
18368f_tanh(argvars, rettv)
18369 typval_T *argvars;
18370 typval_T *rettv;
18371{
18372 float_T f;
18373
18374 rettv->v_type = VAR_FLOAT;
18375 if (get_float_arg(argvars, &f) == OK)
18376 rettv->vval.v_float = tanh(f);
18377 else
18378 rettv->vval.v_float = 0.0;
18379}
18380#endif
18381
Bram Moolenaard52d9742005-08-21 22:20:28 +000018382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018383 * "tolower(string)" function
18384 */
18385 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018386f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018387 typval_T *argvars;
18388 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389{
18390 char_u *p;
18391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018392 p = vim_strsave(get_tv_string(&argvars[0]));
18393 rettv->v_type = VAR_STRING;
18394 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018395
18396 if (p != NULL)
18397 while (*p != NUL)
18398 {
18399#ifdef FEAT_MBYTE
18400 int l;
18401
18402 if (enc_utf8)
18403 {
18404 int c, lc;
18405
18406 c = utf_ptr2char(p);
18407 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018408 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409 /* TODO: reallocate string when byte count changes. */
18410 if (utf_char2len(lc) == l)
18411 utf_char2bytes(lc, p);
18412 p += l;
18413 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018414 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415 p += l; /* skip multi-byte character */
18416 else
18417#endif
18418 {
18419 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18420 ++p;
18421 }
18422 }
18423}
18424
18425/*
18426 * "toupper(string)" function
18427 */
18428 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018429f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018430 typval_T *argvars;
18431 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018432{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018433 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018434 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018435}
18436
18437/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018438 * "tr(string, fromstr, tostr)" function
18439 */
18440 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018441f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018442 typval_T *argvars;
18443 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018444{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018445 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018446 char_u *fromstr;
18447 char_u *tostr;
18448 char_u *p;
18449#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018450 int inlen;
18451 int fromlen;
18452 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018453 int idx;
18454 char_u *cpstr;
18455 int cplen;
18456 int first = TRUE;
18457#endif
18458 char_u buf[NUMBUFLEN];
18459 char_u buf2[NUMBUFLEN];
18460 garray_T ga;
18461
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018462 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018463 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18464 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018465
18466 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018467 rettv->v_type = VAR_STRING;
18468 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018469 if (fromstr == NULL || tostr == NULL)
18470 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018471 ga_init2(&ga, (int)sizeof(char), 80);
18472
18473#ifdef FEAT_MBYTE
18474 if (!has_mbyte)
18475#endif
18476 /* not multi-byte: fromstr and tostr must be the same length */
18477 if (STRLEN(fromstr) != STRLEN(tostr))
18478 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018479#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018480error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018481#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018482 EMSG2(_(e_invarg2), fromstr);
18483 ga_clear(&ga);
18484 return;
18485 }
18486
18487 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018488 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018489 {
18490#ifdef FEAT_MBYTE
18491 if (has_mbyte)
18492 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018493 inlen = (*mb_ptr2len)(in_str);
18494 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018495 cplen = inlen;
18496 idx = 0;
18497 for (p = fromstr; *p != NUL; p += fromlen)
18498 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018499 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018500 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018501 {
18502 for (p = tostr; *p != NUL; p += tolen)
18503 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018504 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018505 if (idx-- == 0)
18506 {
18507 cplen = tolen;
18508 cpstr = p;
18509 break;
18510 }
18511 }
18512 if (*p == NUL) /* tostr is shorter than fromstr */
18513 goto error;
18514 break;
18515 }
18516 ++idx;
18517 }
18518
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018519 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018520 {
18521 /* Check that fromstr and tostr have the same number of
18522 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018523 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018524 first = FALSE;
18525 for (p = tostr; *p != NUL; p += tolen)
18526 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018527 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018528 --idx;
18529 }
18530 if (idx != 0)
18531 goto error;
18532 }
18533
18534 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018535 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018536 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018537
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018538 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018539 }
18540 else
18541#endif
18542 {
18543 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018544 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018545 if (p != NULL)
18546 ga_append(&ga, tostr[p - fromstr]);
18547 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018548 ga_append(&ga, *in_str);
18549 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018550 }
18551 }
18552
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018553 /* add a terminating NUL */
18554 ga_grow(&ga, 1);
18555 ga_append(&ga, NUL);
18556
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018557 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018558}
18559
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018560#ifdef FEAT_FLOAT
18561/*
18562 * "trunc({float})" function
18563 */
18564 static void
18565f_trunc(argvars, rettv)
18566 typval_T *argvars;
18567 typval_T *rettv;
18568{
18569 float_T f;
18570
18571 rettv->v_type = VAR_FLOAT;
18572 if (get_float_arg(argvars, &f) == OK)
18573 /* trunc() is not in C90, use floor() or ceil() instead. */
18574 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18575 else
18576 rettv->vval.v_float = 0.0;
18577}
18578#endif
18579
Bram Moolenaar8299df92004-07-10 09:47:34 +000018580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018581 * "type(expr)" function
18582 */
18583 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018584f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018585 typval_T *argvars;
18586 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018587{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018588 int n;
18589
18590 switch (argvars[0].v_type)
18591 {
18592 case VAR_NUMBER: n = 0; break;
18593 case VAR_STRING: n = 1; break;
18594 case VAR_FUNC: n = 2; break;
18595 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018596 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018597#ifdef FEAT_FLOAT
18598 case VAR_FLOAT: n = 5; break;
18599#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018600 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18601 }
18602 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603}
18604
18605/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018606 * "undofile(name)" function
18607 */
18608 static void
18609f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018610 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018611 typval_T *rettv;
18612{
18613 rettv->v_type = VAR_STRING;
18614#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018615 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018616 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018617
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018618 if (*fname == NUL)
18619 {
18620 /* If there is no file name there will be no undo file. */
18621 rettv->vval.v_string = NULL;
18622 }
18623 else
18624 {
18625 char_u *ffname = FullName_save(fname, FALSE);
18626
18627 if (ffname != NULL)
18628 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18629 vim_free(ffname);
18630 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018631 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018632#else
18633 rettv->vval.v_string = NULL;
18634#endif
18635}
18636
18637/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018638 * "undotree()" function
18639 */
18640 static void
18641f_undotree(argvars, rettv)
18642 typval_T *argvars UNUSED;
18643 typval_T *rettv;
18644{
18645 if (rettv_dict_alloc(rettv) == OK)
18646 {
18647 dict_T *dict = rettv->vval.v_dict;
18648 list_T *list;
18649
Bram Moolenaar730cde92010-06-27 05:18:54 +020018650 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018651 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018652 dict_add_nr_str(dict, "save_last",
18653 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018654 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18655 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018656 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018657
18658 list = list_alloc();
18659 if (list != NULL)
18660 {
18661 u_eval_tree(curbuf->b_u_oldhead, list);
18662 dict_add_list(dict, "entries", list);
18663 }
18664 }
18665}
18666
18667/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018668 * "values(dict)" function
18669 */
18670 static void
18671f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018672 typval_T *argvars;
18673 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018674{
18675 dict_list(argvars, rettv, 1);
18676}
18677
18678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018679 * "virtcol(string)" function
18680 */
18681 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018682f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018683 typval_T *argvars;
18684 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018685{
18686 colnr_T vcol = 0;
18687 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018688 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018689
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018690 fp = var2fpos(&argvars[0], FALSE, &fnum);
18691 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18692 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018693 {
18694 getvvcol(curwin, fp, NULL, NULL, &vcol);
18695 ++vcol;
18696 }
18697
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018698 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018699}
18700
18701/*
18702 * "visualmode()" function
18703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018704 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018705f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018706 typval_T *argvars UNUSED;
18707 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018708{
18709#ifdef FEAT_VISUAL
18710 char_u str[2];
18711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018712 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713 str[0] = curbuf->b_visual_mode_eval;
18714 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018715 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018716
18717 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018718 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018720#endif
18721}
18722
18723/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018724 * "wildmenumode()" function
18725 */
18726 static void
18727f_wildmenumode(argvars, rettv)
18728 typval_T *argvars UNUSED;
18729 typval_T *rettv UNUSED;
18730{
18731#ifdef FEAT_WILDMENU
18732 if (wild_menu_showing)
18733 rettv->vval.v_number = 1;
18734#endif
18735}
18736
18737/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 * "winbufnr(nr)" function
18739 */
18740 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018741f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018742 typval_T *argvars;
18743 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018744{
18745 win_T *wp;
18746
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018747 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018749 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018750 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018751 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018752}
18753
18754/*
18755 * "wincol()" function
18756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018757 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018758f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018759 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018760 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761{
18762 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018763 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018764}
18765
18766/*
18767 * "winheight(nr)" function
18768 */
18769 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018770f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018771 typval_T *argvars;
18772 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018773{
18774 win_T *wp;
18775
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018776 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018778 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018780 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018781}
18782
18783/*
18784 * "winline()" function
18785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018787f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018788 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018790{
18791 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018792 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018793}
18794
18795/*
18796 * "winnr()" function
18797 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018798 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018799f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018800 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018801 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018802{
18803 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018804
Bram Moolenaar071d4272004-06-13 20:20:40 +000018805#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018806 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018808 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809}
18810
18811/*
18812 * "winrestcmd()" function
18813 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018815f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018816 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018817 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018818{
18819#ifdef FEAT_WINDOWS
18820 win_T *wp;
18821 int winnr = 1;
18822 garray_T ga;
18823 char_u buf[50];
18824
18825 ga_init2(&ga, (int)sizeof(char), 70);
18826 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18827 {
18828 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18829 ga_concat(&ga, buf);
18830# ifdef FEAT_VERTSPLIT
18831 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18832 ga_concat(&ga, buf);
18833# endif
18834 ++winnr;
18835 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018836 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018837
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018838 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018840 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018842 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843}
18844
18845/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018846 * "winrestview()" function
18847 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018848 static void
18849f_winrestview(argvars, rettv)
18850 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018851 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018852{
18853 dict_T *dict;
18854
18855 if (argvars[0].v_type != VAR_DICT
18856 || (dict = argvars[0].vval.v_dict) == NULL)
18857 EMSG(_(e_invarg));
18858 else
18859 {
18860 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18861 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18862#ifdef FEAT_VIRTUALEDIT
18863 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18864#endif
18865 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018866 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018867
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018868 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018869#ifdef FEAT_DIFF
18870 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18871#endif
18872 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18873 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18874
18875 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018876 win_new_height(curwin, curwin->w_height);
18877# ifdef FEAT_VERTSPLIT
18878 win_new_width(curwin, W_WIDTH(curwin));
18879# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018880 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018881
18882 if (curwin->w_topline == 0)
18883 curwin->w_topline = 1;
18884 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18885 curwin->w_topline = curbuf->b_ml.ml_line_count;
18886#ifdef FEAT_DIFF
18887 check_topfill(curwin, TRUE);
18888#endif
18889 }
18890}
18891
18892/*
18893 * "winsaveview()" function
18894 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018895 static void
18896f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018897 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018898 typval_T *rettv;
18899{
18900 dict_T *dict;
18901
Bram Moolenaara800b422010-06-27 01:15:55 +020018902 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018903 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018904 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018905
18906 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18907 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18908#ifdef FEAT_VIRTUALEDIT
18909 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18910#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018911 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018912 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18913
18914 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18915#ifdef FEAT_DIFF
18916 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18917#endif
18918 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18919 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18920}
18921
18922/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018923 * "winwidth(nr)" function
18924 */
18925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018926f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018927 typval_T *argvars;
18928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018929{
18930 win_T *wp;
18931
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018932 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018933 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018934 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935 else
18936#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018937 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018938#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018939 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018940#endif
18941}
18942
Bram Moolenaar071d4272004-06-13 20:20:40 +000018943/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018944 * "writefile()" function
18945 */
18946 static void
18947f_writefile(argvars, rettv)
18948 typval_T *argvars;
18949 typval_T *rettv;
18950{
18951 int binary = FALSE;
18952 char_u *fname;
18953 FILE *fd;
18954 listitem_T *li;
18955 char_u *s;
18956 int ret = 0;
18957 int c;
18958
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018959 if (check_restricted() || check_secure())
18960 return;
18961
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018962 if (argvars[0].v_type != VAR_LIST)
18963 {
18964 EMSG2(_(e_listarg), "writefile()");
18965 return;
18966 }
18967 if (argvars[0].vval.v_list == NULL)
18968 return;
18969
18970 if (argvars[2].v_type != VAR_UNKNOWN
18971 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18972 binary = TRUE;
18973
18974 /* Always open the file in binary mode, library functions have a mind of
18975 * their own about CR-LF conversion. */
18976 fname = get_tv_string(&argvars[1]);
18977 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18978 {
18979 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18980 ret = -1;
18981 }
18982 else
18983 {
18984 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18985 li = li->li_next)
18986 {
18987 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18988 {
18989 if (*s == '\n')
18990 c = putc(NUL, fd);
18991 else
18992 c = putc(*s, fd);
18993 if (c == EOF)
18994 {
18995 ret = -1;
18996 break;
18997 }
18998 }
18999 if (!binary || li->li_next != NULL)
19000 if (putc('\n', fd) == EOF)
19001 {
19002 ret = -1;
19003 break;
19004 }
19005 if (ret < 0)
19006 {
19007 EMSG(_(e_write));
19008 break;
19009 }
19010 }
19011 fclose(fd);
19012 }
19013
19014 rettv->vval.v_number = ret;
19015}
19016
19017/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019018 * "xor(expr, expr)" function
19019 */
19020 static void
19021f_xor(argvars, rettv)
19022 typval_T *argvars;
19023 typval_T *rettv;
19024{
19025 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19026 ^ get_tv_number_chk(&argvars[1], NULL);
19027}
19028
19029
19030/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019032 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019033 */
19034 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019035var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019036 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019037 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019038 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019039{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019040 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019041 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019042 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019043
Bram Moolenaara5525202006-03-02 22:52:09 +000019044 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019045 if (varp->v_type == VAR_LIST)
19046 {
19047 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019048 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019049 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019050 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019051
19052 l = varp->vval.v_list;
19053 if (l == NULL)
19054 return NULL;
19055
19056 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019057 pos.lnum = list_find_nr(l, 0L, &error);
19058 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019059 return NULL; /* invalid line number */
19060
19061 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019062 pos.col = list_find_nr(l, 1L, &error);
19063 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019064 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019065 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019066
19067 /* We accept "$" for the column number: last column. */
19068 li = list_find(l, 1L);
19069 if (li != NULL && li->li_tv.v_type == VAR_STRING
19070 && li->li_tv.vval.v_string != NULL
19071 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19072 pos.col = len + 1;
19073
Bram Moolenaara5525202006-03-02 22:52:09 +000019074 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019075 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019076 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019077 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019078
Bram Moolenaara5525202006-03-02 22:52:09 +000019079#ifdef FEAT_VIRTUALEDIT
19080 /* Get the virtual offset. Defaults to zero. */
19081 pos.coladd = list_find_nr(l, 2L, &error);
19082 if (error)
19083 pos.coladd = 0;
19084#endif
19085
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019086 return &pos;
19087 }
19088
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019089 name = get_tv_string_chk(varp);
19090 if (name == NULL)
19091 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019092 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019093 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019094#ifdef FEAT_VISUAL
19095 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19096 {
19097 if (VIsual_active)
19098 return &VIsual;
19099 return &curwin->w_cursor;
19100 }
19101#endif
19102 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019103 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019104 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19106 return NULL;
19107 return pp;
19108 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019109
19110#ifdef FEAT_VIRTUALEDIT
19111 pos.coladd = 0;
19112#endif
19113
Bram Moolenaar477933c2007-07-17 14:32:23 +000019114 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019115 {
19116 pos.col = 0;
19117 if (name[1] == '0') /* "w0": first visible line */
19118 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019119 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019120 pos.lnum = curwin->w_topline;
19121 return &pos;
19122 }
19123 else if (name[1] == '$') /* "w$": last visible line */
19124 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019125 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019126 pos.lnum = curwin->w_botline - 1;
19127 return &pos;
19128 }
19129 }
19130 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019132 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133 {
19134 pos.lnum = curbuf->b_ml.ml_line_count;
19135 pos.col = 0;
19136 }
19137 else
19138 {
19139 pos.lnum = curwin->w_cursor.lnum;
19140 pos.col = (colnr_T)STRLEN(ml_get_curline());
19141 }
19142 return &pos;
19143 }
19144 return NULL;
19145}
19146
19147/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019148 * Convert list in "arg" into a position and optional file number.
19149 * When "fnump" is NULL there is no file number, only 3 items.
19150 * Note that the column is passed on as-is, the caller may want to decrement
19151 * it to use 1 for the first column.
19152 * Return FAIL when conversion is not possible, doesn't check the position for
19153 * validity.
19154 */
19155 static int
19156list2fpos(arg, posp, fnump)
19157 typval_T *arg;
19158 pos_T *posp;
19159 int *fnump;
19160{
19161 list_T *l = arg->vval.v_list;
19162 long i = 0;
19163 long n;
19164
Bram Moolenaarbde35262006-07-23 20:12:24 +000019165 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19166 * when "fnump" isn't NULL and "coladd" is optional. */
19167 if (arg->v_type != VAR_LIST
19168 || l == NULL
19169 || l->lv_len < (fnump == NULL ? 2 : 3)
19170 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019171 return FAIL;
19172
19173 if (fnump != NULL)
19174 {
19175 n = list_find_nr(l, i++, NULL); /* fnum */
19176 if (n < 0)
19177 return FAIL;
19178 if (n == 0)
19179 n = curbuf->b_fnum; /* current buffer */
19180 *fnump = n;
19181 }
19182
19183 n = list_find_nr(l, i++, NULL); /* lnum */
19184 if (n < 0)
19185 return FAIL;
19186 posp->lnum = n;
19187
19188 n = list_find_nr(l, i++, NULL); /* col */
19189 if (n < 0)
19190 return FAIL;
19191 posp->col = n;
19192
19193#ifdef FEAT_VIRTUALEDIT
19194 n = list_find_nr(l, i, NULL);
19195 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019196 posp->coladd = 0;
19197 else
19198 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019199#endif
19200
19201 return OK;
19202}
19203
19204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205 * Get the length of an environment variable name.
19206 * Advance "arg" to the first character after the name.
19207 * Return 0 for error.
19208 */
19209 static int
19210get_env_len(arg)
19211 char_u **arg;
19212{
19213 char_u *p;
19214 int len;
19215
19216 for (p = *arg; vim_isIDc(*p); ++p)
19217 ;
19218 if (p == *arg) /* no name found */
19219 return 0;
19220
19221 len = (int)(p - *arg);
19222 *arg = p;
19223 return len;
19224}
19225
19226/*
19227 * Get the length of the name of a function or internal variable.
19228 * "arg" is advanced to the first non-white character after the name.
19229 * Return 0 if something is wrong.
19230 */
19231 static int
19232get_id_len(arg)
19233 char_u **arg;
19234{
19235 char_u *p;
19236 int len;
19237
19238 /* Find the end of the name. */
19239 for (p = *arg; eval_isnamec(*p); ++p)
19240 ;
19241 if (p == *arg) /* no name found */
19242 return 0;
19243
19244 len = (int)(p - *arg);
19245 *arg = skipwhite(p);
19246
19247 return len;
19248}
19249
19250/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019251 * Get the length of the name of a variable or function.
19252 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019254 * Return -1 if curly braces expansion failed.
19255 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019256 * If the name contains 'magic' {}'s, expand them and return the
19257 * expanded name in an allocated string via 'alias' - caller must free.
19258 */
19259 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019260get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019261 char_u **arg;
19262 char_u **alias;
19263 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019264 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265{
19266 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019267 char_u *p;
19268 char_u *expr_start;
19269 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270
19271 *alias = NULL; /* default to no alias */
19272
19273 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19274 && (*arg)[2] == (int)KE_SNR)
19275 {
19276 /* hard coded <SNR>, already translated */
19277 *arg += 3;
19278 return get_id_len(arg) + 3;
19279 }
19280 len = eval_fname_script(*arg);
19281 if (len > 0)
19282 {
19283 /* literal "<SID>", "s:" or "<SNR>" */
19284 *arg += len;
19285 }
19286
Bram Moolenaar071d4272004-06-13 20:20:40 +000019287 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019288 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019289 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019290 p = find_name_end(*arg, &expr_start, &expr_end,
19291 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019292 if (expr_start != NULL)
19293 {
19294 char_u *temp_string;
19295
19296 if (!evaluate)
19297 {
19298 len += (int)(p - *arg);
19299 *arg = skipwhite(p);
19300 return len;
19301 }
19302
19303 /*
19304 * Include any <SID> etc in the expanded string:
19305 * Thus the -len here.
19306 */
19307 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19308 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019309 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019310 *alias = temp_string;
19311 *arg = skipwhite(p);
19312 return (int)STRLEN(temp_string);
19313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019314
19315 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019316 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019317 EMSG2(_(e_invexpr2), *arg);
19318
19319 return len;
19320}
19321
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019322/*
19323 * Find the end of a variable or function name, taking care of magic braces.
19324 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19325 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019326 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019327 * Return a pointer to just after the name. Equal to "arg" if there is no
19328 * valid name.
19329 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019330 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019331find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019332 char_u *arg;
19333 char_u **expr_start;
19334 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019335 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019336{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019337 int mb_nest = 0;
19338 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019339 char_u *p;
19340
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019341 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019343 *expr_start = NULL;
19344 *expr_end = NULL;
19345 }
19346
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019347 /* Quick check for valid starting character. */
19348 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19349 return arg;
19350
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019351 for (p = arg; *p != NUL
19352 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019353 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019354 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019355 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019356 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019357 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019358 if (*p == '\'')
19359 {
19360 /* skip over 'string' to avoid counting [ and ] inside it. */
19361 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19362 ;
19363 if (*p == NUL)
19364 break;
19365 }
19366 else if (*p == '"')
19367 {
19368 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19369 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19370 if (*p == '\\' && p[1] != NUL)
19371 ++p;
19372 if (*p == NUL)
19373 break;
19374 }
19375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019376 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019377 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019378 if (*p == '[')
19379 ++br_nest;
19380 else if (*p == ']')
19381 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019383
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019384 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019386 if (*p == '{')
19387 {
19388 mb_nest++;
19389 if (expr_start != NULL && *expr_start == NULL)
19390 *expr_start = p;
19391 }
19392 else if (*p == '}')
19393 {
19394 mb_nest--;
19395 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19396 *expr_end = p;
19397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019399 }
19400
19401 return p;
19402}
19403
19404/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019405 * Expands out the 'magic' {}'s in a variable/function name.
19406 * Note that this can call itself recursively, to deal with
19407 * constructs like foo{bar}{baz}{bam}
19408 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19409 * "in_start" ^
19410 * "expr_start" ^
19411 * "expr_end" ^
19412 * "in_end" ^
19413 *
19414 * Returns a new allocated string, which the caller must free.
19415 * Returns NULL for failure.
19416 */
19417 static char_u *
19418make_expanded_name(in_start, expr_start, expr_end, in_end)
19419 char_u *in_start;
19420 char_u *expr_start;
19421 char_u *expr_end;
19422 char_u *in_end;
19423{
19424 char_u c1;
19425 char_u *retval = NULL;
19426 char_u *temp_result;
19427 char_u *nextcmd = NULL;
19428
19429 if (expr_end == NULL || in_end == NULL)
19430 return NULL;
19431 *expr_start = NUL;
19432 *expr_end = NUL;
19433 c1 = *in_end;
19434 *in_end = NUL;
19435
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019436 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019437 if (temp_result != NULL && nextcmd == NULL)
19438 {
19439 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19440 + (in_end - expr_end) + 1));
19441 if (retval != NULL)
19442 {
19443 STRCPY(retval, in_start);
19444 STRCAT(retval, temp_result);
19445 STRCAT(retval, expr_end + 1);
19446 }
19447 }
19448 vim_free(temp_result);
19449
19450 *in_end = c1; /* put char back for error messages */
19451 *expr_start = '{';
19452 *expr_end = '}';
19453
19454 if (retval != NULL)
19455 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019456 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019457 if (expr_start != NULL)
19458 {
19459 /* Further expansion! */
19460 temp_result = make_expanded_name(retval, expr_start,
19461 expr_end, temp_result);
19462 vim_free(retval);
19463 retval = temp_result;
19464 }
19465 }
19466
19467 return retval;
19468}
19469
19470/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019471 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019472 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473 */
19474 static int
19475eval_isnamec(c)
19476 int c;
19477{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019478 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19479}
19480
19481/*
19482 * Return TRUE if character "c" can be used as the first character in a
19483 * variable or function name (excluding '{' and '}').
19484 */
19485 static int
19486eval_isnamec1(c)
19487 int c;
19488{
19489 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490}
19491
19492/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493 * Set number v: variable to "val".
19494 */
19495 void
19496set_vim_var_nr(idx, val)
19497 int idx;
19498 long val;
19499{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019500 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019501}
19502
19503/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019504 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505 */
19506 long
19507get_vim_var_nr(idx)
19508 int idx;
19509{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019510 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019511}
19512
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019513/*
19514 * Get string v: variable value. Uses a static buffer, can only be used once.
19515 */
19516 char_u *
19517get_vim_var_str(idx)
19518 int idx;
19519{
19520 return get_tv_string(&vimvars[idx].vv_tv);
19521}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019522
Bram Moolenaar071d4272004-06-13 20:20:40 +000019523/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019524 * Get List v: variable value. Caller must take care of reference count when
19525 * needed.
19526 */
19527 list_T *
19528get_vim_var_list(idx)
19529 int idx;
19530{
19531 return vimvars[idx].vv_list;
19532}
19533
19534/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019535 * Set v:char to character "c".
19536 */
19537 void
19538set_vim_var_char(c)
19539 int c;
19540{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019541 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019542
19543#ifdef FEAT_MBYTE
19544 if (has_mbyte)
19545 buf[(*mb_char2bytes)(c, buf)] = NUL;
19546 else
19547#endif
19548 {
19549 buf[0] = c;
19550 buf[1] = NUL;
19551 }
19552 set_vim_var_string(VV_CHAR, buf, -1);
19553}
19554
19555/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019556 * Set v:count to "count" and v:count1 to "count1".
19557 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 */
19559 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019560set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561 long count;
19562 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019563 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019564{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019565 if (set_prevcount)
19566 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019567 vimvars[VV_COUNT].vv_nr = count;
19568 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569}
19570
19571/*
19572 * Set string v: variable to a copy of "val".
19573 */
19574 void
19575set_vim_var_string(idx, val, len)
19576 int idx;
19577 char_u *val;
19578 int len; /* length of "val" to use or -1 (whole string) */
19579{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019580 /* Need to do this (at least) once, since we can't initialize a union.
19581 * Will always be invoked when "v:progname" is set. */
19582 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19583
Bram Moolenaare9a41262005-01-15 22:18:47 +000019584 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019585 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019586 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019588 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019589 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019590 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591}
19592
19593/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019594 * Set List v: variable to "val".
19595 */
19596 void
19597set_vim_var_list(idx, val)
19598 int idx;
19599 list_T *val;
19600{
19601 list_unref(vimvars[idx].vv_list);
19602 vimvars[idx].vv_list = val;
19603 if (val != NULL)
19604 ++val->lv_refcount;
19605}
19606
19607/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 * Set v:register if needed.
19609 */
19610 void
19611set_reg_var(c)
19612 int c;
19613{
19614 char_u regname;
19615
19616 if (c == 0 || c == ' ')
19617 regname = '"';
19618 else
19619 regname = c;
19620 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019621 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622 set_vim_var_string(VV_REG, &regname, 1);
19623}
19624
19625/*
19626 * Get or set v:exception. If "oldval" == NULL, return the current value.
19627 * Otherwise, restore the value to "oldval" and return NULL.
19628 * Must always be called in pairs to save and restore v:exception! Does not
19629 * take care of memory allocations.
19630 */
19631 char_u *
19632v_exception(oldval)
19633 char_u *oldval;
19634{
19635 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019636 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637
Bram Moolenaare9a41262005-01-15 22:18:47 +000019638 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 return NULL;
19640}
19641
19642/*
19643 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19644 * Otherwise, restore the value to "oldval" and return NULL.
19645 * Must always be called in pairs to save and restore v:throwpoint! Does not
19646 * take care of memory allocations.
19647 */
19648 char_u *
19649v_throwpoint(oldval)
19650 char_u *oldval;
19651{
19652 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019653 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019654
Bram Moolenaare9a41262005-01-15 22:18:47 +000019655 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019656 return NULL;
19657}
19658
19659#if defined(FEAT_AUTOCMD) || defined(PROTO)
19660/*
19661 * Set v:cmdarg.
19662 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19663 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19664 * Must always be called in pairs!
19665 */
19666 char_u *
19667set_cmdarg(eap, oldarg)
19668 exarg_T *eap;
19669 char_u *oldarg;
19670{
19671 char_u *oldval;
19672 char_u *newval;
19673 unsigned len;
19674
Bram Moolenaare9a41262005-01-15 22:18:47 +000019675 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019676 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019678 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019679 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019680 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681 }
19682
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019683 if (eap->force_bin == FORCE_BIN)
19684 len = 6;
19685 else if (eap->force_bin == FORCE_NOBIN)
19686 len = 8;
19687 else
19688 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019689
19690 if (eap->read_edit)
19691 len += 7;
19692
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019693 if (eap->force_ff != 0)
19694 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19695# ifdef FEAT_MBYTE
19696 if (eap->force_enc != 0)
19697 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019698 if (eap->bad_char != 0)
19699 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019700# endif
19701
19702 newval = alloc(len + 1);
19703 if (newval == NULL)
19704 return NULL;
19705
19706 if (eap->force_bin == FORCE_BIN)
19707 sprintf((char *)newval, " ++bin");
19708 else if (eap->force_bin == FORCE_NOBIN)
19709 sprintf((char *)newval, " ++nobin");
19710 else
19711 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019712
19713 if (eap->read_edit)
19714 STRCAT(newval, " ++edit");
19715
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019716 if (eap->force_ff != 0)
19717 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19718 eap->cmd + eap->force_ff);
19719# ifdef FEAT_MBYTE
19720 if (eap->force_enc != 0)
19721 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19722 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019723 if (eap->bad_char == BAD_KEEP)
19724 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19725 else if (eap->bad_char == BAD_DROP)
19726 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19727 else if (eap->bad_char != 0)
19728 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019729# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019730 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019731 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732}
19733#endif
19734
19735/*
19736 * Get the value of internal variable "name".
19737 * Return OK or FAIL.
19738 */
19739 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019740get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019741 char_u *name;
19742 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019743 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019744 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019745{
19746 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019747 typval_T *tv = NULL;
19748 typval_T atv;
19749 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019751
19752 /* truncate the name, so that we can use strcmp() */
19753 cc = name[len];
19754 name[len] = NUL;
19755
19756 /*
19757 * Check for "b:changedtick".
19758 */
19759 if (STRCMP(name, "b:changedtick") == 0)
19760 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019761 atv.v_type = VAR_NUMBER;
19762 atv.vval.v_number = curbuf->b_changedtick;
19763 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 }
19765
19766 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019767 * Check for user-defined variables.
19768 */
19769 else
19770 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019771 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019772 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019773 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 }
19775
Bram Moolenaare9a41262005-01-15 22:18:47 +000019776 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019777 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019778 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019779 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780 ret = FAIL;
19781 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019782 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019783 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784
19785 name[len] = cc;
19786
19787 return ret;
19788}
19789
19790/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019791 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19792 * Also handle function call with Funcref variable: func(expr)
19793 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19794 */
19795 static int
19796handle_subscript(arg, rettv, evaluate, verbose)
19797 char_u **arg;
19798 typval_T *rettv;
19799 int evaluate; /* do more than finding the end */
19800 int verbose; /* give error messages */
19801{
19802 int ret = OK;
19803 dict_T *selfdict = NULL;
19804 char_u *s;
19805 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019806 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019807
19808 while (ret == OK
19809 && (**arg == '['
19810 || (**arg == '.' && rettv->v_type == VAR_DICT)
19811 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19812 && !vim_iswhite(*(*arg - 1)))
19813 {
19814 if (**arg == '(')
19815 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019816 /* need to copy the funcref so that we can clear rettv */
19817 functv = *rettv;
19818 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019819
19820 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019821 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019822 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019823 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19824 &len, evaluate, selfdict);
19825
19826 /* Clear the funcref afterwards, so that deleting it while
19827 * evaluating the arguments is possible (see test55). */
19828 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019829
19830 /* Stop the expression evaluation when immediately aborting on
19831 * error, or when an interrupt occurred or an exception was thrown
19832 * but not caught. */
19833 if (aborting())
19834 {
19835 if (ret == OK)
19836 clear_tv(rettv);
19837 ret = FAIL;
19838 }
19839 dict_unref(selfdict);
19840 selfdict = NULL;
19841 }
19842 else /* **arg == '[' || **arg == '.' */
19843 {
19844 dict_unref(selfdict);
19845 if (rettv->v_type == VAR_DICT)
19846 {
19847 selfdict = rettv->vval.v_dict;
19848 if (selfdict != NULL)
19849 ++selfdict->dv_refcount;
19850 }
19851 else
19852 selfdict = NULL;
19853 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19854 {
19855 clear_tv(rettv);
19856 ret = FAIL;
19857 }
19858 }
19859 }
19860 dict_unref(selfdict);
19861 return ret;
19862}
19863
19864/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019865 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019866 * value).
19867 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019868 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019869alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019870{
Bram Moolenaar33570922005-01-25 22:26:29 +000019871 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019872}
19873
19874/*
19875 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019876 * The string "s" must have been allocated, it is consumed.
19877 * Return NULL for out of memory, the variable otherwise.
19878 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019879 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019880alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 char_u *s;
19882{
Bram Moolenaar33570922005-01-25 22:26:29 +000019883 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019885 rettv = alloc_tv();
19886 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019887 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019888 rettv->v_type = VAR_STRING;
19889 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019890 }
19891 else
19892 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019893 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019894}
19895
19896/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019897 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019898 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019899 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019900free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019901 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902{
19903 if (varp != NULL)
19904 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019905 switch (varp->v_type)
19906 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019907 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019908 func_unref(varp->vval.v_string);
19909 /*FALLTHROUGH*/
19910 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019911 vim_free(varp->vval.v_string);
19912 break;
19913 case VAR_LIST:
19914 list_unref(varp->vval.v_list);
19915 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019916 case VAR_DICT:
19917 dict_unref(varp->vval.v_dict);
19918 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019919 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019920#ifdef FEAT_FLOAT
19921 case VAR_FLOAT:
19922#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019923 case VAR_UNKNOWN:
19924 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019925 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019926 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019927 break;
19928 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019929 vim_free(varp);
19930 }
19931}
19932
19933/*
19934 * Free the memory for a variable value and set the value to NULL or 0.
19935 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019936 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019937clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019938 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019939{
19940 if (varp != NULL)
19941 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019942 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019943 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019944 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019945 func_unref(varp->vval.v_string);
19946 /*FALLTHROUGH*/
19947 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019948 vim_free(varp->vval.v_string);
19949 varp->vval.v_string = NULL;
19950 break;
19951 case VAR_LIST:
19952 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019953 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019954 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019955 case VAR_DICT:
19956 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019957 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019958 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019959 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019960 varp->vval.v_number = 0;
19961 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019962#ifdef FEAT_FLOAT
19963 case VAR_FLOAT:
19964 varp->vval.v_float = 0.0;
19965 break;
19966#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019967 case VAR_UNKNOWN:
19968 break;
19969 default:
19970 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019972 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973 }
19974}
19975
19976/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019977 * Set the value of a variable to NULL without freeing items.
19978 */
19979 static void
19980init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019981 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019982{
19983 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019984 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019985}
19986
19987/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988 * Get the number value of a variable.
19989 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019990 * For incompatible types, return 0.
19991 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19992 * caller of incompatible types: it sets *denote to TRUE if "denote"
19993 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019994 */
19995 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019996get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019997 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019999 int error = FALSE;
20000
20001 return get_tv_number_chk(varp, &error); /* return 0L on error */
20002}
20003
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020004 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020005get_tv_number_chk(varp, denote)
20006 typval_T *varp;
20007 int *denote;
20008{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020009 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020010
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020011 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020012 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020013 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020014 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020015#ifdef FEAT_FLOAT
20016 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020017 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020018 break;
20019#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020020 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020021 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020022 break;
20023 case VAR_STRING:
20024 if (varp->vval.v_string != NULL)
20025 vim_str2nr(varp->vval.v_string, NULL, NULL,
20026 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020027 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020028 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020029 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020030 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020031 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020032 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020033 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020034 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020035 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020036 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020037 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020038 if (denote == NULL) /* useful for values that must be unsigned */
20039 n = -1;
20040 else
20041 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020042 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043}
20044
20045/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020046 * Get the lnum from the first argument.
20047 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020048 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 */
20050 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020051get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020052 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020053{
Bram Moolenaar33570922005-01-25 22:26:29 +000020054 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020055 linenr_T lnum;
20056
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020057 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020058 if (lnum == 0) /* no valid number, try using line() */
20059 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020060 rettv.v_type = VAR_NUMBER;
20061 f_line(argvars, &rettv);
20062 lnum = rettv.vval.v_number;
20063 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064 }
20065 return lnum;
20066}
20067
20068/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020069 * Get the lnum from the first argument.
20070 * Also accepts "$", then "buf" is used.
20071 * Returns 0 on error.
20072 */
20073 static linenr_T
20074get_tv_lnum_buf(argvars, buf)
20075 typval_T *argvars;
20076 buf_T *buf;
20077{
20078 if (argvars[0].v_type == VAR_STRING
20079 && argvars[0].vval.v_string != NULL
20080 && argvars[0].vval.v_string[0] == '$'
20081 && buf != NULL)
20082 return buf->b_ml.ml_line_count;
20083 return get_tv_number_chk(&argvars[0], NULL);
20084}
20085
20086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020087 * Get the string value of a variable.
20088 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020089 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20090 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 * If the String variable has never been set, return an empty string.
20092 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020093 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20094 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095 */
20096 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020097get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020098 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020099{
20100 static char_u mybuf[NUMBUFLEN];
20101
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020102 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020103}
20104
20105 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020106get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020107 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020108 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020109{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020110 char_u *res = get_tv_string_buf_chk(varp, buf);
20111
20112 return res != NULL ? res : (char_u *)"";
20113}
20114
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020115 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020116get_tv_string_chk(varp)
20117 typval_T *varp;
20118{
20119 static char_u mybuf[NUMBUFLEN];
20120
20121 return get_tv_string_buf_chk(varp, mybuf);
20122}
20123
20124 static char_u *
20125get_tv_string_buf_chk(varp, buf)
20126 typval_T *varp;
20127 char_u *buf;
20128{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020129 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020130 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020131 case VAR_NUMBER:
20132 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20133 return buf;
20134 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020135 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020136 break;
20137 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020138 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020139 break;
20140 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020141 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020142 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020143#ifdef FEAT_FLOAT
20144 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020145 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020146 break;
20147#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020148 case VAR_STRING:
20149 if (varp->vval.v_string != NULL)
20150 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020151 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020152 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020153 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020154 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020155 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020156 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020157}
20158
20159/*
20160 * Find variable "name" in the list of variables.
20161 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020162 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020163 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020164 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020165 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020166 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020167find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020169 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020171 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020172 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020173
Bram Moolenaara7043832005-01-21 11:56:39 +000020174 ht = find_var_ht(name, &varname);
20175 if (htp != NULL)
20176 *htp = ht;
20177 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020178 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020179 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020180}
20181
20182/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020183 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020184 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020185 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020186 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020187find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020188 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020189 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020190 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020191 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020192{
Bram Moolenaar33570922005-01-25 22:26:29 +000020193 hashitem_T *hi;
20194
20195 if (*varname == NUL)
20196 {
20197 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020198 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020199 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020200 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020201 case 'g': return &globvars_var;
20202 case 'v': return &vimvars_var;
20203 case 'b': return &curbuf->b_bufvar;
20204 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020205#ifdef FEAT_WINDOWS
20206 case 't': return &curtab->tp_winvar;
20207#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020208 case 'l': return current_funccal == NULL
20209 ? NULL : &current_funccal->l_vars_var;
20210 case 'a': return current_funccal == NULL
20211 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020212 }
20213 return NULL;
20214 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020215
20216 hi = hash_find(ht, varname);
20217 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020218 {
20219 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020220 * worked find the variable again. Don't auto-load a script if it was
20221 * loaded already, otherwise it would be loaded every time when
20222 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020223 if (ht == &globvarht && !writing)
20224 {
20225 /* Note: script_autoload() may make "hi" invalid. It must either
20226 * be obtained again or not used. */
20227 if (!script_autoload(varname, FALSE) || aborting())
20228 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020229 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020230 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020231 if (HASHITEM_EMPTY(hi))
20232 return NULL;
20233 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020234 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020235}
20236
20237/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020238 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020239 * Set "varname" to the start of name without ':'.
20240 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020241 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020242find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020243 char_u *name;
20244 char_u **varname;
20245{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020246 hashitem_T *hi;
20247
Bram Moolenaar071d4272004-06-13 20:20:40 +000020248 if (name[1] != ':')
20249 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020250 /* The name must not start with a colon or #. */
20251 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 return NULL;
20253 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020254
20255 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020256 hi = hash_find(&compat_hashtab, name);
20257 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020258 return &compat_hashtab;
20259
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020261 return &globvarht; /* global variable */
20262 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020263 }
20264 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020265 if (*name == 'g') /* global variable */
20266 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020267 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20268 */
20269 if (vim_strchr(name + 2, ':') != NULL
20270 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020271 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020272 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020273 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020274 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020275 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020276#ifdef FEAT_WINDOWS
20277 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020278 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020279#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020280 if (*name == 'v') /* v: variable */
20281 return &vimvarht;
20282 if (*name == 'a' && current_funccal != NULL) /* function argument */
20283 return &current_funccal->l_avars.dv_hashtab;
20284 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20285 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020286 if (*name == 's' /* script variable */
20287 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20288 return &SCRIPT_VARS(current_SID);
20289 return NULL;
20290}
20291
20292/*
20293 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020294 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295 * Returns NULL when it doesn't exist.
20296 */
20297 char_u *
20298get_var_value(name)
20299 char_u *name;
20300{
Bram Moolenaar33570922005-01-25 22:26:29 +000020301 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302
Bram Moolenaara7043832005-01-21 11:56:39 +000020303 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020304 if (v == NULL)
20305 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020306 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020307}
20308
20309/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020310 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311 * sourcing this script and when executing functions defined in the script.
20312 */
20313 void
20314new_script_vars(id)
20315 scid_T id;
20316{
Bram Moolenaara7043832005-01-21 11:56:39 +000020317 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020318 hashtab_T *ht;
20319 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020320
Bram Moolenaar071d4272004-06-13 20:20:40 +000020321 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20322 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020323 /* Re-allocating ga_data means that an ht_array pointing to
20324 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020325 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020326 for (i = 1; i <= ga_scripts.ga_len; ++i)
20327 {
20328 ht = &SCRIPT_VARS(i);
20329 if (ht->ht_mask == HT_INIT_SIZE - 1)
20330 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020331 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020332 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020333 }
20334
Bram Moolenaar071d4272004-06-13 20:20:40 +000020335 while (ga_scripts.ga_len < id)
20336 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020337 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020338 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020339 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020341 }
20342 }
20343}
20344
20345/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020346 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20347 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348 */
20349 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020350init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020351 dict_T *dict;
20352 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020353 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020354{
Bram Moolenaar33570922005-01-25 22:26:29 +000020355 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020356 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020357 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020358 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020359 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020360 dict_var->di_tv.vval.v_dict = dict;
20361 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020362 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020363 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20364 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020365}
20366
20367/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020368 * Unreference a dictionary initialized by init_var_dict().
20369 */
20370 void
20371unref_var_dict(dict)
20372 dict_T *dict;
20373{
20374 /* Now the dict needs to be freed if no one else is using it, go back to
20375 * normal reference counting. */
20376 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20377 dict_unref(dict);
20378}
20379
20380/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020381 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020382 * Frees all allocated variables and the value they contain.
20383 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020384 */
20385 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020386vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020387 hashtab_T *ht;
20388{
20389 vars_clear_ext(ht, TRUE);
20390}
20391
20392/*
20393 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20394 */
20395 static void
20396vars_clear_ext(ht, free_val)
20397 hashtab_T *ht;
20398 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020399{
Bram Moolenaara7043832005-01-21 11:56:39 +000020400 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020401 hashitem_T *hi;
20402 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020403
Bram Moolenaar33570922005-01-25 22:26:29 +000020404 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020405 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020406 for (hi = ht->ht_array; todo > 0; ++hi)
20407 {
20408 if (!HASHITEM_EMPTY(hi))
20409 {
20410 --todo;
20411
Bram Moolenaar33570922005-01-25 22:26:29 +000020412 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020413 * ht_array might change then. hash_clear() takes care of it
20414 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020415 v = HI2DI(hi);
20416 if (free_val)
20417 clear_tv(&v->di_tv);
20418 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20419 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020420 }
20421 }
20422 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020423 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020424}
20425
Bram Moolenaara7043832005-01-21 11:56:39 +000020426/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020427 * Delete a variable from hashtab "ht" at item "hi".
20428 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020429 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020430 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020431delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020432 hashtab_T *ht;
20433 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020434{
Bram Moolenaar33570922005-01-25 22:26:29 +000020435 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020436
20437 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020438 clear_tv(&di->di_tv);
20439 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020440}
20441
20442/*
20443 * List the value of one internal variable.
20444 */
20445 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020446list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020447 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020448 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020449 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020450{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020451 char_u *tofree;
20452 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020453 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020454
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020455 current_copyID += COPYID_INC;
20456 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020457 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020458 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020459 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020460}
20461
Bram Moolenaar071d4272004-06-13 20:20:40 +000020462 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020463list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020464 char_u *prefix;
20465 char_u *name;
20466 int type;
20467 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020468 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469{
Bram Moolenaar31859182007-08-14 20:41:13 +000020470 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20471 msg_start();
20472 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473 if (name != NULL) /* "a:" vars don't have a name stored */
20474 msg_puts(name);
20475 msg_putchar(' ');
20476 msg_advance(22);
20477 if (type == VAR_NUMBER)
20478 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020479 else if (type == VAR_FUNC)
20480 msg_putchar('*');
20481 else if (type == VAR_LIST)
20482 {
20483 msg_putchar('[');
20484 if (*string == '[')
20485 ++string;
20486 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020487 else if (type == VAR_DICT)
20488 {
20489 msg_putchar('{');
20490 if (*string == '{')
20491 ++string;
20492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020493 else
20494 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020495
Bram Moolenaar071d4272004-06-13 20:20:40 +000020496 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020497
20498 if (type == VAR_FUNC)
20499 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020500 if (*first)
20501 {
20502 msg_clr_eos();
20503 *first = FALSE;
20504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020505}
20506
20507/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020508 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020509 * If the variable already exists, the value is updated.
20510 * Otherwise the variable is created.
20511 */
20512 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020513set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020514 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020515 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020516 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020517{
Bram Moolenaar33570922005-01-25 22:26:29 +000020518 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020519 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020520 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020522 ht = find_var_ht(name, &varname);
20523 if (ht == NULL || *varname == NUL)
20524 {
20525 EMSG2(_(e_illvar), name);
20526 return;
20527 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020528 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020529
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020530 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20531 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020532
Bram Moolenaar33570922005-01-25 22:26:29 +000020533 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020534 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020535 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020536 if (var_check_ro(v->di_flags, name)
20537 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020538 return;
20539 if (v->di_tv.v_type != tv->v_type
20540 && !((v->di_tv.v_type == VAR_STRING
20541 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020542 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020543 || tv->v_type == VAR_NUMBER))
20544#ifdef FEAT_FLOAT
20545 && !((v->di_tv.v_type == VAR_NUMBER
20546 || v->di_tv.v_type == VAR_FLOAT)
20547 && (tv->v_type == VAR_NUMBER
20548 || tv->v_type == VAR_FLOAT))
20549#endif
20550 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020551 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020552 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020553 return;
20554 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020555
20556 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020557 * Handle setting internal v: variables separately: we don't change
20558 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020559 */
20560 if (ht == &vimvarht)
20561 {
20562 if (v->di_tv.v_type == VAR_STRING)
20563 {
20564 vim_free(v->di_tv.vval.v_string);
20565 if (copy || tv->v_type != VAR_STRING)
20566 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20567 else
20568 {
20569 /* Take over the string to avoid an extra alloc/free. */
20570 v->di_tv.vval.v_string = tv->vval.v_string;
20571 tv->vval.v_string = NULL;
20572 }
20573 }
20574 else if (v->di_tv.v_type != VAR_NUMBER)
20575 EMSG2(_(e_intern2), "set_var()");
20576 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020577 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020578 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020579 if (STRCMP(varname, "searchforward") == 0)
20580 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20581 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020582 return;
20583 }
20584
20585 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020586 }
20587 else /* add a new variable */
20588 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020589 /* Can't add "v:" variable. */
20590 if (ht == &vimvarht)
20591 {
20592 EMSG2(_(e_illvar), name);
20593 return;
20594 }
20595
Bram Moolenaar92124a32005-06-17 22:03:40 +000020596 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020597 if (!valid_varname(varname))
20598 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020599
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020600 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20601 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020602 if (v == NULL)
20603 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020604 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020605 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020606 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020607 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608 return;
20609 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020610 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020612
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020613 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020614 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020615 else
20616 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020617 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020618 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020619 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621}
20622
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020623/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020624 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020625 * Also give an error message.
20626 */
20627 static int
20628var_check_ro(flags, name)
20629 int flags;
20630 char_u *name;
20631{
20632 if (flags & DI_FLAGS_RO)
20633 {
20634 EMSG2(_(e_readonlyvar), name);
20635 return TRUE;
20636 }
20637 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20638 {
20639 EMSG2(_(e_readonlysbx), name);
20640 return TRUE;
20641 }
20642 return FALSE;
20643}
20644
20645/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020646 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20647 * Also give an error message.
20648 */
20649 static int
20650var_check_fixed(flags, name)
20651 int flags;
20652 char_u *name;
20653{
20654 if (flags & DI_FLAGS_FIX)
20655 {
20656 EMSG2(_("E795: Cannot delete variable %s"), name);
20657 return TRUE;
20658 }
20659 return FALSE;
20660}
20661
20662/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020663 * Check if a funcref is assigned to a valid variable name.
20664 * Return TRUE and give an error if not.
20665 */
20666 static int
20667var_check_func_name(name, new_var)
20668 char_u *name; /* points to start of variable name */
20669 int new_var; /* TRUE when creating the variable */
20670{
20671 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20672 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20673 ? name[2] : name[0]))
20674 {
20675 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20676 name);
20677 return TRUE;
20678 }
20679 /* Don't allow hiding a function. When "v" is not NULL we might be
20680 * assigning another function to the same var, the type is checked
20681 * below. */
20682 if (new_var && function_exists(name))
20683 {
20684 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20685 name);
20686 return TRUE;
20687 }
20688 return FALSE;
20689}
20690
20691/*
20692 * Check if a variable name is valid.
20693 * Return FALSE and give an error if not.
20694 */
20695 static int
20696valid_varname(varname)
20697 char_u *varname;
20698{
20699 char_u *p;
20700
20701 for (p = varname; *p != NUL; ++p)
20702 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20703 && *p != AUTOLOAD_CHAR)
20704 {
20705 EMSG2(_(e_illvar), varname);
20706 return FALSE;
20707 }
20708 return TRUE;
20709}
20710
20711/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020712 * Return TRUE if typeval "tv" is set to be locked (immutable).
20713 * Also give an error message, using "name".
20714 */
20715 static int
20716tv_check_lock(lock, name)
20717 int lock;
20718 char_u *name;
20719{
20720 if (lock & VAR_LOCKED)
20721 {
20722 EMSG2(_("E741: Value is locked: %s"),
20723 name == NULL ? (char_u *)_("Unknown") : name);
20724 return TRUE;
20725 }
20726 if (lock & VAR_FIXED)
20727 {
20728 EMSG2(_("E742: Cannot change value of %s"),
20729 name == NULL ? (char_u *)_("Unknown") : name);
20730 return TRUE;
20731 }
20732 return FALSE;
20733}
20734
20735/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020736 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020737 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020738 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020739 * It is OK for "from" and "to" to point to the same item. This is used to
20740 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020741 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020742 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020743copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020744 typval_T *from;
20745 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020746{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020747 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020748 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020749 switch (from->v_type)
20750 {
20751 case VAR_NUMBER:
20752 to->vval.v_number = from->vval.v_number;
20753 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020754#ifdef FEAT_FLOAT
20755 case VAR_FLOAT:
20756 to->vval.v_float = from->vval.v_float;
20757 break;
20758#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020759 case VAR_STRING:
20760 case VAR_FUNC:
20761 if (from->vval.v_string == NULL)
20762 to->vval.v_string = NULL;
20763 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020764 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020765 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020766 if (from->v_type == VAR_FUNC)
20767 func_ref(to->vval.v_string);
20768 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020769 break;
20770 case VAR_LIST:
20771 if (from->vval.v_list == NULL)
20772 to->vval.v_list = NULL;
20773 else
20774 {
20775 to->vval.v_list = from->vval.v_list;
20776 ++to->vval.v_list->lv_refcount;
20777 }
20778 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020779 case VAR_DICT:
20780 if (from->vval.v_dict == NULL)
20781 to->vval.v_dict = NULL;
20782 else
20783 {
20784 to->vval.v_dict = from->vval.v_dict;
20785 ++to->vval.v_dict->dv_refcount;
20786 }
20787 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020788 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020789 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020790 break;
20791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020792}
20793
20794/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020795 * Make a copy of an item.
20796 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020797 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20798 * reference to an already copied list/dict can be used.
20799 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020800 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020801 static int
20802item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020803 typval_T *from;
20804 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020805 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020806 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020807{
20808 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020809 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020810
Bram Moolenaar33570922005-01-25 22:26:29 +000020811 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020812 {
20813 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020814 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020815 }
20816 ++recurse;
20817
20818 switch (from->v_type)
20819 {
20820 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020821#ifdef FEAT_FLOAT
20822 case VAR_FLOAT:
20823#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020824 case VAR_STRING:
20825 case VAR_FUNC:
20826 copy_tv(from, to);
20827 break;
20828 case VAR_LIST:
20829 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020830 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020831 if (from->vval.v_list == NULL)
20832 to->vval.v_list = NULL;
20833 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20834 {
20835 /* use the copy made earlier */
20836 to->vval.v_list = from->vval.v_list->lv_copylist;
20837 ++to->vval.v_list->lv_refcount;
20838 }
20839 else
20840 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20841 if (to->vval.v_list == NULL)
20842 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020843 break;
20844 case VAR_DICT:
20845 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020846 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020847 if (from->vval.v_dict == NULL)
20848 to->vval.v_dict = NULL;
20849 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20850 {
20851 /* use the copy made earlier */
20852 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20853 ++to->vval.v_dict->dv_refcount;
20854 }
20855 else
20856 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20857 if (to->vval.v_dict == NULL)
20858 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020859 break;
20860 default:
20861 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020862 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020863 }
20864 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020865 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020866}
20867
20868/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020869 * ":echo expr1 ..." print each argument separated with a space, add a
20870 * newline at the end.
20871 * ":echon expr1 ..." print each argument plain.
20872 */
20873 void
20874ex_echo(eap)
20875 exarg_T *eap;
20876{
20877 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020878 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020879 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020880 char_u *p;
20881 int needclr = TRUE;
20882 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020883 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020884
20885 if (eap->skip)
20886 ++emsg_skip;
20887 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20888 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020889 /* If eval1() causes an error message the text from the command may
20890 * still need to be cleared. E.g., "echo 22,44". */
20891 need_clr_eos = needclr;
20892
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020894 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 {
20896 /*
20897 * Report the invalid expression unless the expression evaluation
20898 * has been cancelled due to an aborting error, an interrupt, or an
20899 * exception.
20900 */
20901 if (!aborting())
20902 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020903 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 break;
20905 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020906 need_clr_eos = FALSE;
20907
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908 if (!eap->skip)
20909 {
20910 if (atstart)
20911 {
20912 atstart = FALSE;
20913 /* Call msg_start() after eval1(), evaluating the expression
20914 * may cause a message to appear. */
20915 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020916 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020917 /* Mark the saved text as finishing the line, so that what
20918 * follows is displayed on a new line when scrolling back
20919 * at the more prompt. */
20920 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923 }
20924 else if (eap->cmdidx == CMD_echo)
20925 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020926 current_copyID += COPYID_INC;
20927 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020928 if (p != NULL)
20929 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020931 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020933 if (*p != TAB && needclr)
20934 {
20935 /* remove any text still there from the command */
20936 msg_clr_eos();
20937 needclr = FALSE;
20938 }
20939 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020940 }
20941 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020942 {
20943#ifdef FEAT_MBYTE
20944 if (has_mbyte)
20945 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020946 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020947
20948 (void)msg_outtrans_len_attr(p, i, echo_attr);
20949 p += i - 1;
20950 }
20951 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020952#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020953 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20954 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020956 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020958 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020959 arg = skipwhite(arg);
20960 }
20961 eap->nextcmd = check_nextcmd(arg);
20962
20963 if (eap->skip)
20964 --emsg_skip;
20965 else
20966 {
20967 /* remove text that may still be there from the command */
20968 if (needclr)
20969 msg_clr_eos();
20970 if (eap->cmdidx == CMD_echo)
20971 msg_end();
20972 }
20973}
20974
20975/*
20976 * ":echohl {name}".
20977 */
20978 void
20979ex_echohl(eap)
20980 exarg_T *eap;
20981{
20982 int id;
20983
20984 id = syn_name2id(eap->arg);
20985 if (id == 0)
20986 echo_attr = 0;
20987 else
20988 echo_attr = syn_id2attr(id);
20989}
20990
20991/*
20992 * ":execute expr1 ..." execute the result of an expression.
20993 * ":echomsg expr1 ..." Print a message
20994 * ":echoerr expr1 ..." Print an error
20995 * Each gets spaces around each argument and a newline at the end for
20996 * echo commands
20997 */
20998 void
20999ex_execute(eap)
21000 exarg_T *eap;
21001{
21002 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021003 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004 int ret = OK;
21005 char_u *p;
21006 garray_T ga;
21007 int len;
21008 int save_did_emsg;
21009
21010 ga_init2(&ga, 1, 80);
21011
21012 if (eap->skip)
21013 ++emsg_skip;
21014 while (*arg != NUL && *arg != '|' && *arg != '\n')
21015 {
21016 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021017 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 {
21019 /*
21020 * Report the invalid expression unless the expression evaluation
21021 * has been cancelled due to an aborting error, an interrupt, or an
21022 * exception.
21023 */
21024 if (!aborting())
21025 EMSG2(_(e_invexpr2), p);
21026 ret = FAIL;
21027 break;
21028 }
21029
21030 if (!eap->skip)
21031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021032 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033 len = (int)STRLEN(p);
21034 if (ga_grow(&ga, len + 2) == FAIL)
21035 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021036 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021037 ret = FAIL;
21038 break;
21039 }
21040 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043 ga.ga_len += len;
21044 }
21045
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021046 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047 arg = skipwhite(arg);
21048 }
21049
21050 if (ret != FAIL && ga.ga_data != NULL)
21051 {
21052 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021053 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021054 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021055 out_flush();
21056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021057 else if (eap->cmdidx == CMD_echoerr)
21058 {
21059 /* We don't want to abort following commands, restore did_emsg. */
21060 save_did_emsg = did_emsg;
21061 EMSG((char_u *)ga.ga_data);
21062 if (!force_abort)
21063 did_emsg = save_did_emsg;
21064 }
21065 else if (eap->cmdidx == CMD_execute)
21066 do_cmdline((char_u *)ga.ga_data,
21067 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21068 }
21069
21070 ga_clear(&ga);
21071
21072 if (eap->skip)
21073 --emsg_skip;
21074
21075 eap->nextcmd = check_nextcmd(arg);
21076}
21077
21078/*
21079 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21080 * "arg" points to the "&" or '+' when called, to "option" when returning.
21081 * Returns NULL when no option name found. Otherwise pointer to the char
21082 * after the option name.
21083 */
21084 static char_u *
21085find_option_end(arg, opt_flags)
21086 char_u **arg;
21087 int *opt_flags;
21088{
21089 char_u *p = *arg;
21090
21091 ++p;
21092 if (*p == 'g' && p[1] == ':')
21093 {
21094 *opt_flags = OPT_GLOBAL;
21095 p += 2;
21096 }
21097 else if (*p == 'l' && p[1] == ':')
21098 {
21099 *opt_flags = OPT_LOCAL;
21100 p += 2;
21101 }
21102 else
21103 *opt_flags = 0;
21104
21105 if (!ASCII_ISALPHA(*p))
21106 return NULL;
21107 *arg = p;
21108
21109 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21110 p += 4; /* termcap option */
21111 else
21112 while (ASCII_ISALPHA(*p))
21113 ++p;
21114 return p;
21115}
21116
21117/*
21118 * ":function"
21119 */
21120 void
21121ex_function(eap)
21122 exarg_T *eap;
21123{
21124 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021125 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021126 int j;
21127 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021128 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021129 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021130 char_u *name = NULL;
21131 char_u *p;
21132 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021133 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021134 garray_T newargs;
21135 garray_T newlines;
21136 int varargs = FALSE;
21137 int mustend = FALSE;
21138 int flags = 0;
21139 ufunc_T *fp;
21140 int indent;
21141 int nesting;
21142 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021143 dictitem_T *v;
21144 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021145 static int func_nr = 0; /* number for nameless function */
21146 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021147 hashtab_T *ht;
21148 int todo;
21149 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021150 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151
21152 /*
21153 * ":function" without argument: list functions.
21154 */
21155 if (ends_excmd(*eap->arg))
21156 {
21157 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021158 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021159 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021160 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021161 {
21162 if (!HASHITEM_EMPTY(hi))
21163 {
21164 --todo;
21165 fp = HI2UF(hi);
21166 if (!isdigit(*fp->uf_name))
21167 list_func_head(fp, FALSE);
21168 }
21169 }
21170 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171 eap->nextcmd = check_nextcmd(eap->arg);
21172 return;
21173 }
21174
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021175 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021176 * ":function /pat": list functions matching pattern.
21177 */
21178 if (*eap->arg == '/')
21179 {
21180 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21181 if (!eap->skip)
21182 {
21183 regmatch_T regmatch;
21184
21185 c = *p;
21186 *p = NUL;
21187 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21188 *p = c;
21189 if (regmatch.regprog != NULL)
21190 {
21191 regmatch.rm_ic = p_ic;
21192
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021193 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021194 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21195 {
21196 if (!HASHITEM_EMPTY(hi))
21197 {
21198 --todo;
21199 fp = HI2UF(hi);
21200 if (!isdigit(*fp->uf_name)
21201 && vim_regexec(&regmatch, fp->uf_name, 0))
21202 list_func_head(fp, FALSE);
21203 }
21204 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021205 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021206 }
21207 }
21208 if (*p == '/')
21209 ++p;
21210 eap->nextcmd = check_nextcmd(p);
21211 return;
21212 }
21213
21214 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021215 * Get the function name. There are these situations:
21216 * func normal function name
21217 * "name" == func, "fudi.fd_dict" == NULL
21218 * dict.func new dictionary entry
21219 * "name" == NULL, "fudi.fd_dict" set,
21220 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21221 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021222 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021223 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21224 * dict.func existing dict entry that's not a Funcref
21225 * "name" == NULL, "fudi.fd_dict" set,
21226 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21227 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021229 name = trans_function_name(&p, eap->skip, 0, &fudi);
21230 paren = (vim_strchr(p, '(') != NULL);
21231 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021232 {
21233 /*
21234 * Return on an invalid expression in braces, unless the expression
21235 * evaluation has been cancelled due to an aborting error, an
21236 * interrupt, or an exception.
21237 */
21238 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021239 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021240 if (!eap->skip && fudi.fd_newkey != NULL)
21241 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021242 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021245 else
21246 eap->skip = TRUE;
21247 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021248
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 /* An error in a function call during evaluation of an expression in magic
21250 * braces should not cause the function not to be defined. */
21251 saved_did_emsg = did_emsg;
21252 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253
21254 /*
21255 * ":function func" with only function name: list function.
21256 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021257 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258 {
21259 if (!ends_excmd(*skipwhite(p)))
21260 {
21261 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021262 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021263 }
21264 eap->nextcmd = check_nextcmd(p);
21265 if (eap->nextcmd != NULL)
21266 *p = NUL;
21267 if (!eap->skip && !got_int)
21268 {
21269 fp = find_func(name);
21270 if (fp != NULL)
21271 {
21272 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021273 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021274 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021275 if (FUNCLINE(fp, j) == NULL)
21276 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277 msg_putchar('\n');
21278 msg_outnum((long)(j + 1));
21279 if (j < 9)
21280 msg_putchar(' ');
21281 if (j < 99)
21282 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021283 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 out_flush(); /* show a line at a time */
21285 ui_breakcheck();
21286 }
21287 if (!got_int)
21288 {
21289 msg_putchar('\n');
21290 msg_puts((char_u *)" endfunction");
21291 }
21292 }
21293 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021294 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021295 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021296 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021297 }
21298
21299 /*
21300 * ":function name(arg1, arg2)" Define function.
21301 */
21302 p = skipwhite(p);
21303 if (*p != '(')
21304 {
21305 if (!eap->skip)
21306 {
21307 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021308 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 }
21310 /* attempt to continue by skipping some text */
21311 if (vim_strchr(p, '(') != NULL)
21312 p = vim_strchr(p, '(');
21313 }
21314 p = skipwhite(p + 1);
21315
21316 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21317 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21318
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021319 if (!eap->skip)
21320 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021321 /* Check the name of the function. Unless it's a dictionary function
21322 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021323 if (name != NULL)
21324 arg = name;
21325 else
21326 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021327 if (arg != NULL && (fudi.fd_di == NULL
21328 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021329 {
21330 if (*arg == K_SPECIAL)
21331 j = 3;
21332 else
21333 j = 0;
21334 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21335 : eval_isnamec(arg[j])))
21336 ++j;
21337 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021338 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021339 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021340 /* Disallow using the g: dict. */
21341 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21342 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021343 }
21344
Bram Moolenaar071d4272004-06-13 20:20:40 +000021345 /*
21346 * Isolate the arguments: "arg1, arg2, ...)"
21347 */
21348 while (*p != ')')
21349 {
21350 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21351 {
21352 varargs = TRUE;
21353 p += 3;
21354 mustend = TRUE;
21355 }
21356 else
21357 {
21358 arg = p;
21359 while (ASCII_ISALNUM(*p) || *p == '_')
21360 ++p;
21361 if (arg == p || isdigit(*arg)
21362 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21363 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21364 {
21365 if (!eap->skip)
21366 EMSG2(_("E125: Illegal argument: %s"), arg);
21367 break;
21368 }
21369 if (ga_grow(&newargs, 1) == FAIL)
21370 goto erret;
21371 c = *p;
21372 *p = NUL;
21373 arg = vim_strsave(arg);
21374 if (arg == NULL)
21375 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021376
21377 /* Check for duplicate argument name. */
21378 for (i = 0; i < newargs.ga_len; ++i)
21379 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21380 {
21381 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21382 goto erret;
21383 }
21384
Bram Moolenaar071d4272004-06-13 20:20:40 +000021385 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21386 *p = c;
21387 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388 if (*p == ',')
21389 ++p;
21390 else
21391 mustend = TRUE;
21392 }
21393 p = skipwhite(p);
21394 if (mustend && *p != ')')
21395 {
21396 if (!eap->skip)
21397 EMSG2(_(e_invarg2), eap->arg);
21398 break;
21399 }
21400 }
21401 ++p; /* skip the ')' */
21402
Bram Moolenaare9a41262005-01-15 22:18:47 +000021403 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 for (;;)
21405 {
21406 p = skipwhite(p);
21407 if (STRNCMP(p, "range", 5) == 0)
21408 {
21409 flags |= FC_RANGE;
21410 p += 5;
21411 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021412 else if (STRNCMP(p, "dict", 4) == 0)
21413 {
21414 flags |= FC_DICT;
21415 p += 4;
21416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417 else if (STRNCMP(p, "abort", 5) == 0)
21418 {
21419 flags |= FC_ABORT;
21420 p += 5;
21421 }
21422 else
21423 break;
21424 }
21425
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021426 /* When there is a line break use what follows for the function body.
21427 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21428 if (*p == '\n')
21429 line_arg = p + 1;
21430 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 EMSG(_(e_trailing));
21432
21433 /*
21434 * Read the body of the function, until ":endfunction" is found.
21435 */
21436 if (KeyTyped)
21437 {
21438 /* Check if the function already exists, don't let the user type the
21439 * whole function before telling him it doesn't work! For a script we
21440 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021441 if (!eap->skip && !eap->forceit)
21442 {
21443 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21444 EMSG(_(e_funcdict));
21445 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021446 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021449 if (!eap->skip && did_emsg)
21450 goto erret;
21451
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 msg_putchar('\n'); /* don't overwrite the function name */
21453 cmdline_row = msg_row;
21454 }
21455
21456 indent = 2;
21457 nesting = 0;
21458 for (;;)
21459 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021460 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021461 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021462 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021463 saved_wait_return = FALSE;
21464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021465 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021466 sourcing_lnum_off = sourcing_lnum;
21467
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021468 if (line_arg != NULL)
21469 {
21470 /* Use eap->arg, split up in parts by line breaks. */
21471 theline = line_arg;
21472 p = vim_strchr(theline, '\n');
21473 if (p == NULL)
21474 line_arg += STRLEN(line_arg);
21475 else
21476 {
21477 *p = NUL;
21478 line_arg = p + 1;
21479 }
21480 }
21481 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 theline = getcmdline(':', 0L, indent);
21483 else
21484 theline = eap->getline(':', eap->cookie, indent);
21485 if (KeyTyped)
21486 lines_left = Rows - 1;
21487 if (theline == NULL)
21488 {
21489 EMSG(_("E126: Missing :endfunction"));
21490 goto erret;
21491 }
21492
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021493 /* Detect line continuation: sourcing_lnum increased more than one. */
21494 if (sourcing_lnum > sourcing_lnum_off + 1)
21495 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21496 else
21497 sourcing_lnum_off = 0;
21498
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 if (skip_until != NULL)
21500 {
21501 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21502 * don't check for ":endfunc". */
21503 if (STRCMP(theline, skip_until) == 0)
21504 {
21505 vim_free(skip_until);
21506 skip_until = NULL;
21507 }
21508 }
21509 else
21510 {
21511 /* skip ':' and blanks*/
21512 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21513 ;
21514
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021515 /* Check for "endfunction". */
21516 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021517 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021518 if (line_arg == NULL)
21519 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520 break;
21521 }
21522
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021523 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 * at "end". */
21525 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21526 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021527 else if (STRNCMP(p, "if", 2) == 0
21528 || STRNCMP(p, "wh", 2) == 0
21529 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021530 || STRNCMP(p, "try", 3) == 0)
21531 indent += 2;
21532
21533 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021534 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021536 if (*p == '!')
21537 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 p += eval_fname_script(p);
21539 if (ASCII_ISALPHA(*p))
21540 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021541 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542 if (*skipwhite(p) == '(')
21543 {
21544 ++nesting;
21545 indent += 2;
21546 }
21547 }
21548 }
21549
21550 /* Check for ":append" or ":insert". */
21551 p = skip_range(p, NULL);
21552 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21553 || (p[0] == 'i'
21554 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21555 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21556 skip_until = vim_strsave((char_u *)".");
21557
21558 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21559 arg = skipwhite(skiptowhite(p));
21560 if (arg[0] == '<' && arg[1] =='<'
21561 && ((p[0] == 'p' && p[1] == 'y'
21562 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21563 || (p[0] == 'p' && p[1] == 'e'
21564 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21565 || (p[0] == 't' && p[1] == 'c'
21566 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021567 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21568 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021569 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21570 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021571 || (p[0] == 'm' && p[1] == 'z'
21572 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573 ))
21574 {
21575 /* ":python <<" continues until a dot, like ":append" */
21576 p = skipwhite(arg + 2);
21577 if (*p == NUL)
21578 skip_until = vim_strsave((char_u *)".");
21579 else
21580 skip_until = vim_strsave(p);
21581 }
21582 }
21583
21584 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021585 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021586 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021587 if (line_arg == NULL)
21588 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021590 }
21591
21592 /* Copy the line to newly allocated memory. get_one_sourceline()
21593 * allocates 250 bytes per line, this saves 80% on average. The cost
21594 * is an extra alloc/free. */
21595 p = vim_strsave(theline);
21596 if (p != NULL)
21597 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021598 if (line_arg == NULL)
21599 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021600 theline = p;
21601 }
21602
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021603 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21604
21605 /* Add NULL lines for continuation lines, so that the line count is
21606 * equal to the index in the growarray. */
21607 while (sourcing_lnum_off-- > 0)
21608 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021609
21610 /* Check for end of eap->arg. */
21611 if (line_arg != NULL && *line_arg == NUL)
21612 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613 }
21614
21615 /* Don't define the function when skipping commands or when an error was
21616 * detected. */
21617 if (eap->skip || did_emsg)
21618 goto erret;
21619
21620 /*
21621 * If there are no errors, add the function
21622 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021623 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021624 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021625 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021626 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021627 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021628 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021629 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021630 goto erret;
21631 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021632
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021633 fp = find_func(name);
21634 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021635 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021636 if (!eap->forceit)
21637 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021638 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021639 goto erret;
21640 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021641 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021642 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021643 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021644 name);
21645 goto erret;
21646 }
21647 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021648 ga_clear_strings(&(fp->uf_args));
21649 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021650 vim_free(name);
21651 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653 }
21654 else
21655 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021656 char numbuf[20];
21657
21658 fp = NULL;
21659 if (fudi.fd_newkey == NULL && !eap->forceit)
21660 {
21661 EMSG(_(e_funcdict));
21662 goto erret;
21663 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021664 if (fudi.fd_di == NULL)
21665 {
21666 /* Can't add a function to a locked dictionary */
21667 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21668 goto erret;
21669 }
21670 /* Can't change an existing function if it is locked */
21671 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21672 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021673
21674 /* Give the function a sequential number. Can only be used with a
21675 * Funcref! */
21676 vim_free(name);
21677 sprintf(numbuf, "%d", ++func_nr);
21678 name = vim_strsave((char_u *)numbuf);
21679 if (name == NULL)
21680 goto erret;
21681 }
21682
21683 if (fp == NULL)
21684 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021685 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021686 {
21687 int slen, plen;
21688 char_u *scriptname;
21689
21690 /* Check that the autoload name matches the script name. */
21691 j = FAIL;
21692 if (sourcing_name != NULL)
21693 {
21694 scriptname = autoload_name(name);
21695 if (scriptname != NULL)
21696 {
21697 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021698 plen = (int)STRLEN(p);
21699 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021700 if (slen > plen && fnamecmp(p,
21701 sourcing_name + slen - plen) == 0)
21702 j = OK;
21703 vim_free(scriptname);
21704 }
21705 }
21706 if (j == FAIL)
21707 {
21708 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21709 goto erret;
21710 }
21711 }
21712
21713 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714 if (fp == NULL)
21715 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021716
21717 if (fudi.fd_dict != NULL)
21718 {
21719 if (fudi.fd_di == NULL)
21720 {
21721 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021722 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021723 if (fudi.fd_di == NULL)
21724 {
21725 vim_free(fp);
21726 goto erret;
21727 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021728 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21729 {
21730 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021731 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021732 goto erret;
21733 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021734 }
21735 else
21736 /* overwrite existing dict entry */
21737 clear_tv(&fudi.fd_di->di_tv);
21738 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021739 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021740 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021741 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021742
21743 /* behave like "dict" was used */
21744 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021745 }
21746
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021748 STRCPY(fp->uf_name, name);
21749 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021751 fp->uf_args = newargs;
21752 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021753#ifdef FEAT_PROFILE
21754 fp->uf_tml_count = NULL;
21755 fp->uf_tml_total = NULL;
21756 fp->uf_tml_self = NULL;
21757 fp->uf_profiling = FALSE;
21758 if (prof_def_func())
21759 func_do_profile(fp);
21760#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021761 fp->uf_varargs = varargs;
21762 fp->uf_flags = flags;
21763 fp->uf_calls = 0;
21764 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021765 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021766
21767erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 ga_clear_strings(&newargs);
21769 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021770ret_free:
21771 vim_free(skip_until);
21772 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021775 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776}
21777
21778/*
21779 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021780 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021782 * flags:
21783 * TFN_INT: internal function name OK
21784 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 * Advances "pp" to just after the function name (if no error).
21786 */
21787 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021788trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789 char_u **pp;
21790 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021791 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021792 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021793{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021794 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 char_u *start;
21796 char_u *end;
21797 int lead;
21798 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021799 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021800 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021801
21802 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021803 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021804 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021805
21806 /* Check for hard coded <SNR>: already translated function ID (from a user
21807 * command). */
21808 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21809 && (*pp)[2] == (int)KE_SNR)
21810 {
21811 *pp += 3;
21812 len = get_id_len(pp) + 3;
21813 return vim_strnsave(start, len);
21814 }
21815
21816 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21817 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021818 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021819 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021820 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021821
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021822 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21823 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021824 if (end == start)
21825 {
21826 if (!skip)
21827 EMSG(_("E129: Function name required"));
21828 goto theend;
21829 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021830 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021831 {
21832 /*
21833 * Report an invalid expression in braces, unless the expression
21834 * evaluation has been cancelled due to an aborting error, an
21835 * interrupt, or an exception.
21836 */
21837 if (!aborting())
21838 {
21839 if (end != NULL)
21840 EMSG2(_(e_invarg2), start);
21841 }
21842 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021843 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021844 goto theend;
21845 }
21846
21847 if (lv.ll_tv != NULL)
21848 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021849 if (fdp != NULL)
21850 {
21851 fdp->fd_dict = lv.ll_dict;
21852 fdp->fd_newkey = lv.ll_newkey;
21853 lv.ll_newkey = NULL;
21854 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021855 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021856 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21857 {
21858 name = vim_strsave(lv.ll_tv->vval.v_string);
21859 *pp = end;
21860 }
21861 else
21862 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021863 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21864 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021865 EMSG(_(e_funcref));
21866 else
21867 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021868 name = NULL;
21869 }
21870 goto theend;
21871 }
21872
21873 if (lv.ll_name == NULL)
21874 {
21875 /* Error found, but continue after the function name. */
21876 *pp = end;
21877 goto theend;
21878 }
21879
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021880 /* Check if the name is a Funcref. If so, use the value. */
21881 if (lv.ll_exp_name != NULL)
21882 {
21883 len = (int)STRLEN(lv.ll_exp_name);
21884 name = deref_func_name(lv.ll_exp_name, &len);
21885 if (name == lv.ll_exp_name)
21886 name = NULL;
21887 }
21888 else
21889 {
21890 len = (int)(end - *pp);
21891 name = deref_func_name(*pp, &len);
21892 if (name == *pp)
21893 name = NULL;
21894 }
21895 if (name != NULL)
21896 {
21897 name = vim_strsave(name);
21898 *pp = end;
21899 goto theend;
21900 }
21901
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021902 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021903 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021904 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021905 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21906 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21907 {
21908 /* When there was "s:" already or the name expanded to get a
21909 * leading "s:" then remove it. */
21910 lv.ll_name += 2;
21911 len -= 2;
21912 lead = 2;
21913 }
21914 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021915 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021916 {
21917 if (lead == 2) /* skip over "s:" */
21918 lv.ll_name += 2;
21919 len = (int)(end - lv.ll_name);
21920 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021921
21922 /*
21923 * Copy the function name to allocated memory.
21924 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21925 * Accept <SNR>123_name() outside a script.
21926 */
21927 if (skip)
21928 lead = 0; /* do nothing */
21929 else if (lead > 0)
21930 {
21931 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021932 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21933 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021934 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021935 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021936 if (current_SID <= 0)
21937 {
21938 EMSG(_(e_usingsid));
21939 goto theend;
21940 }
21941 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21942 lead += (int)STRLEN(sid_buf);
21943 }
21944 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021945 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021946 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021947 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021948 goto theend;
21949 }
21950 name = alloc((unsigned)(len + lead + 1));
21951 if (name != NULL)
21952 {
21953 if (lead > 0)
21954 {
21955 name[0] = K_SPECIAL;
21956 name[1] = KS_EXTRA;
21957 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021958 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021959 STRCPY(name + 3, sid_buf);
21960 }
21961 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21962 name[len + lead] = NUL;
21963 }
21964 *pp = end;
21965
21966theend:
21967 clear_lval(&lv);
21968 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021969}
21970
21971/*
21972 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21973 * Return 2 if "p" starts with "s:".
21974 * Return 0 otherwise.
21975 */
21976 static int
21977eval_fname_script(p)
21978 char_u *p;
21979{
21980 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21981 || STRNICMP(p + 1, "SNR>", 4) == 0))
21982 return 5;
21983 if (p[0] == 's' && p[1] == ':')
21984 return 2;
21985 return 0;
21986}
21987
21988/*
21989 * Return TRUE if "p" starts with "<SID>" or "s:".
21990 * Only works if eval_fname_script() returned non-zero for "p"!
21991 */
21992 static int
21993eval_fname_sid(p)
21994 char_u *p;
21995{
21996 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21997}
21998
21999/*
22000 * List the head of the function: "name(arg1, arg2)".
22001 */
22002 static void
22003list_func_head(fp, indent)
22004 ufunc_T *fp;
22005 int indent;
22006{
22007 int j;
22008
22009 msg_start();
22010 if (indent)
22011 MSG_PUTS(" ");
22012 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022013 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014 {
22015 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022016 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022017 }
22018 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022019 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022021 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022 {
22023 if (j)
22024 MSG_PUTS(", ");
22025 msg_puts(FUNCARG(fp, j));
22026 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022027 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028 {
22029 if (j)
22030 MSG_PUTS(", ");
22031 MSG_PUTS("...");
22032 }
22033 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022034 if (fp->uf_flags & FC_ABORT)
22035 MSG_PUTS(" abort");
22036 if (fp->uf_flags & FC_RANGE)
22037 MSG_PUTS(" range");
22038 if (fp->uf_flags & FC_DICT)
22039 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022040 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022041 if (p_verbose > 0)
22042 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043}
22044
22045/*
22046 * Find a function by name, return pointer to it in ufuncs.
22047 * Return NULL for unknown function.
22048 */
22049 static ufunc_T *
22050find_func(name)
22051 char_u *name;
22052{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022053 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022055 hi = hash_find(&func_hashtab, name);
22056 if (!HASHITEM_EMPTY(hi))
22057 return HI2UF(hi);
22058 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022059}
22060
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022061#if defined(EXITFREE) || defined(PROTO)
22062 void
22063free_all_functions()
22064{
22065 hashitem_T *hi;
22066
22067 /* Need to start all over every time, because func_free() may change the
22068 * hash table. */
22069 while (func_hashtab.ht_used > 0)
22070 for (hi = func_hashtab.ht_array; ; ++hi)
22071 if (!HASHITEM_EMPTY(hi))
22072 {
22073 func_free(HI2UF(hi));
22074 break;
22075 }
22076}
22077#endif
22078
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022079 int
22080translated_function_exists(name)
22081 char_u *name;
22082{
22083 if (builtin_function(name))
22084 return find_internal_func(name) >= 0;
22085 return find_func(name) != NULL;
22086}
22087
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022088/*
22089 * Return TRUE if a function "name" exists.
22090 */
22091 static int
22092function_exists(name)
22093 char_u *name;
22094{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022095 char_u *nm = name;
22096 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022097 int n = FALSE;
22098
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022099 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022100 nm = skipwhite(nm);
22101
22102 /* Only accept "funcname", "funcname ", "funcname (..." and
22103 * "funcname(...", not "funcname!...". */
22104 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022105 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022106 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022107 return n;
22108}
22109
Bram Moolenaara1544c02013-05-30 12:35:52 +020022110 char_u *
22111get_expanded_name(name, check)
22112 char_u *name;
22113 int check;
22114{
22115 char_u *nm = name;
22116 char_u *p;
22117
22118 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22119
22120 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022121 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022122 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022123
Bram Moolenaara1544c02013-05-30 12:35:52 +020022124 vim_free(p);
22125 return NULL;
22126}
22127
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022128/*
22129 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022130 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022131 */
22132 static int
22133builtin_function(name)
22134 char_u *name;
22135{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022136 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22137 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022138}
22139
Bram Moolenaar05159a02005-02-26 23:04:13 +000022140#if defined(FEAT_PROFILE) || defined(PROTO)
22141/*
22142 * Start profiling function "fp".
22143 */
22144 static void
22145func_do_profile(fp)
22146 ufunc_T *fp;
22147{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022148 int len = fp->uf_lines.ga_len;
22149
22150 if (len == 0)
22151 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022152 fp->uf_tm_count = 0;
22153 profile_zero(&fp->uf_tm_self);
22154 profile_zero(&fp->uf_tm_total);
22155 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022156 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022157 if (fp->uf_tml_total == NULL)
22158 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022159 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022160 if (fp->uf_tml_self == NULL)
22161 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022162 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022163 fp->uf_tml_idx = -1;
22164 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22165 || fp->uf_tml_self == NULL)
22166 return; /* out of memory */
22167
22168 fp->uf_profiling = TRUE;
22169}
22170
22171/*
22172 * Dump the profiling results for all functions in file "fd".
22173 */
22174 void
22175func_dump_profile(fd)
22176 FILE *fd;
22177{
22178 hashitem_T *hi;
22179 int todo;
22180 ufunc_T *fp;
22181 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022182 ufunc_T **sorttab;
22183 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022184
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022185 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022186 if (todo == 0)
22187 return; /* nothing to dump */
22188
Bram Moolenaar73830342005-02-28 22:48:19 +000022189 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22190
Bram Moolenaar05159a02005-02-26 23:04:13 +000022191 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22192 {
22193 if (!HASHITEM_EMPTY(hi))
22194 {
22195 --todo;
22196 fp = HI2UF(hi);
22197 if (fp->uf_profiling)
22198 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022199 if (sorttab != NULL)
22200 sorttab[st_len++] = fp;
22201
Bram Moolenaar05159a02005-02-26 23:04:13 +000022202 if (fp->uf_name[0] == K_SPECIAL)
22203 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22204 else
22205 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22206 if (fp->uf_tm_count == 1)
22207 fprintf(fd, "Called 1 time\n");
22208 else
22209 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22210 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22211 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22212 fprintf(fd, "\n");
22213 fprintf(fd, "count total (s) self (s)\n");
22214
22215 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22216 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022217 if (FUNCLINE(fp, i) == NULL)
22218 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022219 prof_func_line(fd, fp->uf_tml_count[i],
22220 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022221 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22222 }
22223 fprintf(fd, "\n");
22224 }
22225 }
22226 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022227
22228 if (sorttab != NULL && st_len > 0)
22229 {
22230 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22231 prof_total_cmp);
22232 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22233 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22234 prof_self_cmp);
22235 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22236 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022237
22238 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022239}
Bram Moolenaar73830342005-02-28 22:48:19 +000022240
22241 static void
22242prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22243 FILE *fd;
22244 ufunc_T **sorttab;
22245 int st_len;
22246 char *title;
22247 int prefer_self; /* when equal print only self time */
22248{
22249 int i;
22250 ufunc_T *fp;
22251
22252 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22253 fprintf(fd, "count total (s) self (s) function\n");
22254 for (i = 0; i < 20 && i < st_len; ++i)
22255 {
22256 fp = sorttab[i];
22257 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22258 prefer_self);
22259 if (fp->uf_name[0] == K_SPECIAL)
22260 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22261 else
22262 fprintf(fd, " %s()\n", fp->uf_name);
22263 }
22264 fprintf(fd, "\n");
22265}
22266
22267/*
22268 * Print the count and times for one function or function line.
22269 */
22270 static void
22271prof_func_line(fd, count, total, self, prefer_self)
22272 FILE *fd;
22273 int count;
22274 proftime_T *total;
22275 proftime_T *self;
22276 int prefer_self; /* when equal print only self time */
22277{
22278 if (count > 0)
22279 {
22280 fprintf(fd, "%5d ", count);
22281 if (prefer_self && profile_equal(total, self))
22282 fprintf(fd, " ");
22283 else
22284 fprintf(fd, "%s ", profile_msg(total));
22285 if (!prefer_self && profile_equal(total, self))
22286 fprintf(fd, " ");
22287 else
22288 fprintf(fd, "%s ", profile_msg(self));
22289 }
22290 else
22291 fprintf(fd, " ");
22292}
22293
22294/*
22295 * Compare function for total time sorting.
22296 */
22297 static int
22298#ifdef __BORLANDC__
22299_RTLENTRYF
22300#endif
22301prof_total_cmp(s1, s2)
22302 const void *s1;
22303 const void *s2;
22304{
22305 ufunc_T *p1, *p2;
22306
22307 p1 = *(ufunc_T **)s1;
22308 p2 = *(ufunc_T **)s2;
22309 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22310}
22311
22312/*
22313 * Compare function for self time sorting.
22314 */
22315 static int
22316#ifdef __BORLANDC__
22317_RTLENTRYF
22318#endif
22319prof_self_cmp(s1, s2)
22320 const void *s1;
22321 const void *s2;
22322{
22323 ufunc_T *p1, *p2;
22324
22325 p1 = *(ufunc_T **)s1;
22326 p2 = *(ufunc_T **)s2;
22327 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22328}
22329
Bram Moolenaar05159a02005-02-26 23:04:13 +000022330#endif
22331
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022332/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022333 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022334 * Return TRUE if a package was loaded.
22335 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022336 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022337script_autoload(name, reload)
22338 char_u *name;
22339 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022340{
22341 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022342 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022343 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022344 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022345
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022346 /* Return quickly when autoload disabled. */
22347 if (no_autoload)
22348 return FALSE;
22349
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022350 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022351 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022352 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022353 return FALSE;
22354
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022355 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022356
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022357 /* Find the name in the list of previously loaded package names. Skip
22358 * "autoload/", it's always the same. */
22359 for (i = 0; i < ga_loaded.ga_len; ++i)
22360 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22361 break;
22362 if (!reload && i < ga_loaded.ga_len)
22363 ret = FALSE; /* was loaded already */
22364 else
22365 {
22366 /* Remember the name if it wasn't loaded already. */
22367 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22368 {
22369 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22370 tofree = NULL;
22371 }
22372
22373 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022374 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022375 ret = TRUE;
22376 }
22377
22378 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022379 return ret;
22380}
22381
22382/*
22383 * Return the autoload script name for a function or variable name.
22384 * Returns NULL when out of memory.
22385 */
22386 static char_u *
22387autoload_name(name)
22388 char_u *name;
22389{
22390 char_u *p;
22391 char_u *scriptname;
22392
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022393 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022394 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22395 if (scriptname == NULL)
22396 return FALSE;
22397 STRCPY(scriptname, "autoload/");
22398 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022399 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022400 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022401 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022402 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022403 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022404}
22405
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22407
22408/*
22409 * Function given to ExpandGeneric() to obtain the list of user defined
22410 * function names.
22411 */
22412 char_u *
22413get_user_func_name(xp, idx)
22414 expand_T *xp;
22415 int idx;
22416{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022417 static long_u done;
22418 static hashitem_T *hi;
22419 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420
22421 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022423 done = 0;
22424 hi = func_hashtab.ht_array;
22425 }
22426 if (done < func_hashtab.ht_used)
22427 {
22428 if (done++ > 0)
22429 ++hi;
22430 while (HASHITEM_EMPTY(hi))
22431 ++hi;
22432 fp = HI2UF(hi);
22433
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022434 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022435 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022436
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022437 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22438 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439
22440 cat_func_name(IObuff, fp);
22441 if (xp->xp_context != EXPAND_USER_FUNC)
22442 {
22443 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022444 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 STRCAT(IObuff, ")");
22446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447 return IObuff;
22448 }
22449 return NULL;
22450}
22451
22452#endif /* FEAT_CMDL_COMPL */
22453
22454/*
22455 * Copy the function name of "fp" to buffer "buf".
22456 * "buf" must be able to hold the function name plus three bytes.
22457 * Takes care of script-local function names.
22458 */
22459 static void
22460cat_func_name(buf, fp)
22461 char_u *buf;
22462 ufunc_T *fp;
22463{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022464 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 {
22466 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022467 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468 }
22469 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022470 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471}
22472
22473/*
22474 * ":delfunction {name}"
22475 */
22476 void
22477ex_delfunction(eap)
22478 exarg_T *eap;
22479{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022480 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481 char_u *p;
22482 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022483 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484
22485 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022486 name = trans_function_name(&p, eap->skip, 0, &fudi);
22487 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022489 {
22490 if (fudi.fd_dict != NULL && !eap->skip)
22491 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494 if (!ends_excmd(*skipwhite(p)))
22495 {
22496 vim_free(name);
22497 EMSG(_(e_trailing));
22498 return;
22499 }
22500 eap->nextcmd = check_nextcmd(p);
22501 if (eap->nextcmd != NULL)
22502 *p = NUL;
22503
22504 if (!eap->skip)
22505 fp = find_func(name);
22506 vim_free(name);
22507
22508 if (!eap->skip)
22509 {
22510 if (fp == NULL)
22511 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022512 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 return;
22514 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022515 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022516 {
22517 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22518 return;
22519 }
22520
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022521 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022523 /* Delete the dict item that refers to the function, it will
22524 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022525 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022527 else
22528 func_free(fp);
22529 }
22530}
22531
22532/*
22533 * Free a function and remove it from the list of functions.
22534 */
22535 static void
22536func_free(fp)
22537 ufunc_T *fp;
22538{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022539 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022540
22541 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022542 ga_clear_strings(&(fp->uf_args));
22543 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022544#ifdef FEAT_PROFILE
22545 vim_free(fp->uf_tml_count);
22546 vim_free(fp->uf_tml_total);
22547 vim_free(fp->uf_tml_self);
22548#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022549
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022550 /* remove the function from the function hashtable */
22551 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22552 if (HASHITEM_EMPTY(hi))
22553 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022554 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022555 hash_remove(&func_hashtab, hi);
22556
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022557 vim_free(fp);
22558}
22559
22560/*
22561 * Unreference a Function: decrement the reference count and free it when it
22562 * becomes zero. Only for numbered functions.
22563 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022564 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022565func_unref(name)
22566 char_u *name;
22567{
22568 ufunc_T *fp;
22569
22570 if (name != NULL && isdigit(*name))
22571 {
22572 fp = find_func(name);
22573 if (fp == NULL)
22574 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022575 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022576 {
22577 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022578 * when "uf_calls" becomes zero. */
22579 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022580 func_free(fp);
22581 }
22582 }
22583}
22584
22585/*
22586 * Count a reference to a Function.
22587 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022588 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022589func_ref(name)
22590 char_u *name;
22591{
22592 ufunc_T *fp;
22593
22594 if (name != NULL && isdigit(*name))
22595 {
22596 fp = find_func(name);
22597 if (fp == NULL)
22598 EMSG2(_(e_intern2), "func_ref()");
22599 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022600 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601 }
22602}
22603
22604/*
22605 * Call a user function.
22606 */
22607 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022608call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609 ufunc_T *fp; /* pointer to function */
22610 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022611 typval_T *argvars; /* arguments */
22612 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 linenr_T firstline; /* first line of range */
22614 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022615 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616{
Bram Moolenaar33570922005-01-25 22:26:29 +000022617 char_u *save_sourcing_name;
22618 linenr_T save_sourcing_lnum;
22619 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022620 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022621 int save_did_emsg;
22622 static int depth = 0;
22623 dictitem_T *v;
22624 int fixvar_idx = 0; /* index in fixvar[] */
22625 int i;
22626 int ai;
22627 char_u numbuf[NUMBUFLEN];
22628 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022629#ifdef FEAT_PROFILE
22630 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022631 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022632#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633
22634 /* If depth of calling is getting too high, don't execute the function */
22635 if (depth >= p_mfd)
22636 {
22637 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022638 rettv->v_type = VAR_NUMBER;
22639 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640 return;
22641 }
22642 ++depth;
22643
22644 line_breakcheck(); /* check for CTRL-C hit */
22645
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022646 fc = (funccall_T *)alloc(sizeof(funccall_T));
22647 fc->caller = current_funccal;
22648 current_funccal = fc;
22649 fc->func = fp;
22650 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022651 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022652 fc->linenr = 0;
22653 fc->returned = FALSE;
22654 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022656 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22657 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658
Bram Moolenaar33570922005-01-25 22:26:29 +000022659 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022660 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022661 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22662 * each argument variable and saves a lot of time.
22663 */
22664 /*
22665 * Init l: variables.
22666 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022667 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022668 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022669 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022670 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22671 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022672 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022673 name = v->di_key;
22674 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022675 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022676 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022677 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022678 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022679 v->di_tv.vval.v_dict = selfdict;
22680 ++selfdict->dv_refcount;
22681 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022682
Bram Moolenaar33570922005-01-25 22:26:29 +000022683 /*
22684 * Init a: variables.
22685 * Set a:0 to "argcount".
22686 * Set a:000 to a list with room for the "..." arguments.
22687 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022688 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022689 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022690 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022691 /* Use "name" to avoid a warning from some compiler that checks the
22692 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022693 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022694 name = v->di_key;
22695 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022696 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022697 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022698 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022699 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022700 v->di_tv.vval.v_list = &fc->l_varlist;
22701 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22702 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22703 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022704
22705 /*
22706 * Set a:firstline to "firstline" and a:lastline to "lastline".
22707 * Set a:name to named arguments.
22708 * Set a:N to the "..." arguments.
22709 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022710 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022711 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022712 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022713 (varnumber_T)lastline);
22714 for (i = 0; i < argcount; ++i)
22715 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022716 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022717 if (ai < 0)
22718 /* named argument a:name */
22719 name = FUNCARG(fp, i);
22720 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022721 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022722 /* "..." argument a:1, a:2, etc. */
22723 sprintf((char *)numbuf, "%d", ai + 1);
22724 name = numbuf;
22725 }
22726 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22727 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022728 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022729 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22730 }
22731 else
22732 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022733 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22734 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022735 if (v == NULL)
22736 break;
22737 v->di_flags = DI_FLAGS_RO;
22738 }
22739 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022740 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022741
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022742 /* Note: the values are copied directly to avoid alloc/free.
22743 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022744 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022745 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022746
22747 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22748 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022749 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22750 fc->l_listitems[ai].li_tv = argvars[i];
22751 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022752 }
22753 }
22754
Bram Moolenaar071d4272004-06-13 20:20:40 +000022755 /* Don't redraw while executing the function. */
22756 ++RedrawingDisabled;
22757 save_sourcing_name = sourcing_name;
22758 save_sourcing_lnum = sourcing_lnum;
22759 sourcing_lnum = 1;
22760 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022761 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022762 if (sourcing_name != NULL)
22763 {
22764 if (save_sourcing_name != NULL
22765 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22766 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22767 else
22768 STRCPY(sourcing_name, "function ");
22769 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22770
22771 if (p_verbose >= 12)
22772 {
22773 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022774 verbose_enter_scroll();
22775
Bram Moolenaar555b2802005-05-19 21:08:39 +000022776 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777 if (p_verbose >= 14)
22778 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022779 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022780 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022781 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022782 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022783
22784 msg_puts((char_u *)"(");
22785 for (i = 0; i < argcount; ++i)
22786 {
22787 if (i > 0)
22788 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022789 if (argvars[i].v_type == VAR_NUMBER)
22790 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022791 else
22792 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022793 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22794 if (s != NULL)
22795 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022796 if (vim_strsize(s) > MSG_BUF_CLEN)
22797 {
22798 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22799 s = buf;
22800 }
22801 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022802 vim_free(tofree);
22803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804 }
22805 }
22806 msg_puts((char_u *)")");
22807 }
22808 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022809
22810 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 --no_wait_return;
22812 }
22813 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022814#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022815 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022816 {
22817 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22818 func_do_profile(fp);
22819 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022820 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022821 {
22822 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022823 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022824 profile_zero(&fp->uf_tm_children);
22825 }
22826 script_prof_save(&wait_start);
22827 }
22828#endif
22829
Bram Moolenaar071d4272004-06-13 20:20:40 +000022830 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022831 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832 save_did_emsg = did_emsg;
22833 did_emsg = FALSE;
22834
22835 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022836 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022837 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22838
22839 --RedrawingDisabled;
22840
22841 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022842 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022843 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022844 clear_tv(rettv);
22845 rettv->v_type = VAR_NUMBER;
22846 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022847 }
22848
Bram Moolenaar05159a02005-02-26 23:04:13 +000022849#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022850 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022851 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022852 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022853 profile_end(&call_start);
22854 profile_sub_wait(&wait_start, &call_start);
22855 profile_add(&fp->uf_tm_total, &call_start);
22856 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022857 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022858 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022859 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22860 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022861 }
22862 }
22863#endif
22864
Bram Moolenaar071d4272004-06-13 20:20:40 +000022865 /* when being verbose, mention the return value */
22866 if (p_verbose >= 12)
22867 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022868 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022869 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870
Bram Moolenaar071d4272004-06-13 20:20:40 +000022871 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022872 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022873 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022874 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022875 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022876 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022877 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022878 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022879 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022880 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022881 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022882
Bram Moolenaar555b2802005-05-19 21:08:39 +000022883 /* The value may be very long. Skip the middle part, so that we
22884 * have some idea how it starts and ends. smsg() would always
22885 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022886 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022887 if (s != NULL)
22888 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022889 if (vim_strsize(s) > MSG_BUF_CLEN)
22890 {
22891 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22892 s = buf;
22893 }
22894 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022895 vim_free(tofree);
22896 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022897 }
22898 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022899
22900 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022901 --no_wait_return;
22902 }
22903
22904 vim_free(sourcing_name);
22905 sourcing_name = save_sourcing_name;
22906 sourcing_lnum = save_sourcing_lnum;
22907 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022908#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022909 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022910 script_prof_restore(&wait_start);
22911#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022912
22913 if (p_verbose >= 12 && sourcing_name != NULL)
22914 {
22915 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022916 verbose_enter_scroll();
22917
Bram Moolenaar555b2802005-05-19 21:08:39 +000022918 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022920
22921 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022922 --no_wait_return;
22923 }
22924
22925 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022926 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022928
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022929 /* If the a:000 list and the l: and a: dicts are not referenced we can
22930 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022931 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22932 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22933 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22934 {
22935 free_funccal(fc, FALSE);
22936 }
22937 else
22938 {
22939 hashitem_T *hi;
22940 listitem_T *li;
22941 int todo;
22942
22943 /* "fc" is still in use. This can happen when returning "a:000" or
22944 * assigning "l:" to a global variable.
22945 * Link "fc" in the list for garbage collection later. */
22946 fc->caller = previous_funccal;
22947 previous_funccal = fc;
22948
22949 /* Make a copy of the a: variables, since we didn't do that above. */
22950 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22951 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22952 {
22953 if (!HASHITEM_EMPTY(hi))
22954 {
22955 --todo;
22956 v = HI2DI(hi);
22957 copy_tv(&v->di_tv, &v->di_tv);
22958 }
22959 }
22960
22961 /* Make a copy of the a:000 items, since we didn't do that above. */
22962 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22963 copy_tv(&li->li_tv, &li->li_tv);
22964 }
22965}
22966
22967/*
22968 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022969 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022970 */
22971 static int
22972can_free_funccal(fc, copyID)
22973 funccall_T *fc;
22974 int copyID;
22975{
22976 return (fc->l_varlist.lv_copyID != copyID
22977 && fc->l_vars.dv_copyID != copyID
22978 && fc->l_avars.dv_copyID != copyID);
22979}
22980
22981/*
22982 * Free "fc" and what it contains.
22983 */
22984 static void
22985free_funccal(fc, free_val)
22986 funccall_T *fc;
22987 int free_val; /* a: vars were allocated */
22988{
22989 listitem_T *li;
22990
22991 /* The a: variables typevals may not have been allocated, only free the
22992 * allocated variables. */
22993 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22994
22995 /* free all l: variables */
22996 vars_clear(&fc->l_vars.dv_hashtab);
22997
22998 /* Free the a:000 variables if they were allocated. */
22999 if (free_val)
23000 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23001 clear_tv(&li->li_tv);
23002
23003 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023004}
23005
23006/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023007 * Add a number variable "name" to dict "dp" with value "nr".
23008 */
23009 static void
23010add_nr_var(dp, v, name, nr)
23011 dict_T *dp;
23012 dictitem_T *v;
23013 char *name;
23014 varnumber_T nr;
23015{
23016 STRCPY(v->di_key, name);
23017 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23018 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23019 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023020 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023021 v->di_tv.vval.v_number = nr;
23022}
23023
23024/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023025 * ":return [expr]"
23026 */
23027 void
23028ex_return(eap)
23029 exarg_T *eap;
23030{
23031 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023032 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023033 int returning = FALSE;
23034
23035 if (current_funccal == NULL)
23036 {
23037 EMSG(_("E133: :return not inside a function"));
23038 return;
23039 }
23040
23041 if (eap->skip)
23042 ++emsg_skip;
23043
23044 eap->nextcmd = NULL;
23045 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023046 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047 {
23048 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023049 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023051 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023052 }
23053 /* It's safer to return also on error. */
23054 else if (!eap->skip)
23055 {
23056 /*
23057 * Return unless the expression evaluation has been cancelled due to an
23058 * aborting error, an interrupt, or an exception.
23059 */
23060 if (!aborting())
23061 returning = do_return(eap, FALSE, TRUE, NULL);
23062 }
23063
23064 /* When skipping or the return gets pending, advance to the next command
23065 * in this line (!returning). Otherwise, ignore the rest of the line.
23066 * Following lines will be ignored by get_func_line(). */
23067 if (returning)
23068 eap->nextcmd = NULL;
23069 else if (eap->nextcmd == NULL) /* no argument */
23070 eap->nextcmd = check_nextcmd(arg);
23071
23072 if (eap->skip)
23073 --emsg_skip;
23074}
23075
23076/*
23077 * Return from a function. Possibly makes the return pending. Also called
23078 * for a pending return at the ":endtry" or after returning from an extra
23079 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023080 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023081 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 * FALSE when the return gets pending.
23083 */
23084 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023085do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023086 exarg_T *eap;
23087 int reanimate;
23088 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023089 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090{
23091 int idx;
23092 struct condstack *cstack = eap->cstack;
23093
23094 if (reanimate)
23095 /* Undo the return. */
23096 current_funccal->returned = FALSE;
23097
23098 /*
23099 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23100 * not in its finally clause (which then is to be executed next) is found.
23101 * In this case, make the ":return" pending for execution at the ":endtry".
23102 * Otherwise, return normally.
23103 */
23104 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23105 if (idx >= 0)
23106 {
23107 cstack->cs_pending[idx] = CSTP_RETURN;
23108
23109 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023110 /* A pending return again gets pending. "rettv" points to an
23111 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023112 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023113 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 else
23115 {
23116 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023117 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023119 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023120
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023121 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023122 {
23123 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023124 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023125 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 else
23127 EMSG(_(e_outofmem));
23128 }
23129 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023130 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023131
23132 if (reanimate)
23133 {
23134 /* The pending return value could be overwritten by a ":return"
23135 * without argument in a finally clause; reset the default
23136 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023137 current_funccal->rettv->v_type = VAR_NUMBER;
23138 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139 }
23140 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023141 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023142 }
23143 else
23144 {
23145 current_funccal->returned = TRUE;
23146
23147 /* If the return is carried out now, store the return value. For
23148 * a return immediately after reanimation, the value is already
23149 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023150 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023152 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023153 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023154 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023155 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023156 }
23157 }
23158
23159 return idx < 0;
23160}
23161
23162/*
23163 * Free the variable with a pending return value.
23164 */
23165 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023166discard_pending_return(rettv)
23167 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023168{
Bram Moolenaar33570922005-01-25 22:26:29 +000023169 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170}
23171
23172/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023173 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174 * is an allocated string. Used by report_pending() for verbose messages.
23175 */
23176 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023177get_return_cmd(rettv)
23178 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023179{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023180 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023181 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023182 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023184 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023185 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023186 if (s == NULL)
23187 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023188
23189 STRCPY(IObuff, ":return ");
23190 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23191 if (STRLEN(s) + 8 >= IOSIZE)
23192 STRCPY(IObuff + IOSIZE - 4, "...");
23193 vim_free(tofree);
23194 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195}
23196
23197/*
23198 * Get next function line.
23199 * Called by do_cmdline() to get the next line.
23200 * Returns allocated string, or NULL for end of function.
23201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 char_u *
23203get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023204 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023205 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023206 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023207{
Bram Moolenaar33570922005-01-25 22:26:29 +000023208 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023209 ufunc_T *fp = fcp->func;
23210 char_u *retval;
23211 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023212
23213 /* If breakpoints have been added/deleted need to check for it. */
23214 if (fcp->dbg_tick != debug_tick)
23215 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023216 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217 sourcing_lnum);
23218 fcp->dbg_tick = debug_tick;
23219 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023220#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023221 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023222 func_line_end(cookie);
23223#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224
Bram Moolenaar05159a02005-02-26 23:04:13 +000023225 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023226 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23227 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023228 retval = NULL;
23229 else
23230 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023231 /* Skip NULL lines (continuation lines). */
23232 while (fcp->linenr < gap->ga_len
23233 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23234 ++fcp->linenr;
23235 if (fcp->linenr >= gap->ga_len)
23236 retval = NULL;
23237 else
23238 {
23239 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23240 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023241#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023242 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023243 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023244#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023246 }
23247
23248 /* Did we encounter a breakpoint? */
23249 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23250 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023251 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023253 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023254 sourcing_lnum);
23255 fcp->dbg_tick = debug_tick;
23256 }
23257
23258 return retval;
23259}
23260
Bram Moolenaar05159a02005-02-26 23:04:13 +000023261#if defined(FEAT_PROFILE) || defined(PROTO)
23262/*
23263 * Called when starting to read a function line.
23264 * "sourcing_lnum" must be correct!
23265 * When skipping lines it may not actually be executed, but we won't find out
23266 * until later and we need to store the time now.
23267 */
23268 void
23269func_line_start(cookie)
23270 void *cookie;
23271{
23272 funccall_T *fcp = (funccall_T *)cookie;
23273 ufunc_T *fp = fcp->func;
23274
23275 if (fp->uf_profiling && sourcing_lnum >= 1
23276 && sourcing_lnum <= fp->uf_lines.ga_len)
23277 {
23278 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023279 /* Skip continuation lines. */
23280 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23281 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023282 fp->uf_tml_execed = FALSE;
23283 profile_start(&fp->uf_tml_start);
23284 profile_zero(&fp->uf_tml_children);
23285 profile_get_wait(&fp->uf_tml_wait);
23286 }
23287}
23288
23289/*
23290 * Called when actually executing a function line.
23291 */
23292 void
23293func_line_exec(cookie)
23294 void *cookie;
23295{
23296 funccall_T *fcp = (funccall_T *)cookie;
23297 ufunc_T *fp = fcp->func;
23298
23299 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23300 fp->uf_tml_execed = TRUE;
23301}
23302
23303/*
23304 * Called when done with a function line.
23305 */
23306 void
23307func_line_end(cookie)
23308 void *cookie;
23309{
23310 funccall_T *fcp = (funccall_T *)cookie;
23311 ufunc_T *fp = fcp->func;
23312
23313 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23314 {
23315 if (fp->uf_tml_execed)
23316 {
23317 ++fp->uf_tml_count[fp->uf_tml_idx];
23318 profile_end(&fp->uf_tml_start);
23319 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023320 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023321 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23322 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023323 }
23324 fp->uf_tml_idx = -1;
23325 }
23326}
23327#endif
23328
Bram Moolenaar071d4272004-06-13 20:20:40 +000023329/*
23330 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023331 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023332 */
23333 int
23334func_has_ended(cookie)
23335 void *cookie;
23336{
Bram Moolenaar33570922005-01-25 22:26:29 +000023337 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023338
23339 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23340 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023341 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023342 || fcp->returned);
23343}
23344
23345/*
23346 * return TRUE if cookie indicates a function which "abort"s on errors.
23347 */
23348 int
23349func_has_abort(cookie)
23350 void *cookie;
23351{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023352 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023353}
23354
23355#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23356typedef enum
23357{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023358 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23359 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23360 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361} var_flavour_T;
23362
23363static var_flavour_T var_flavour __ARGS((char_u *varname));
23364
23365 static var_flavour_T
23366var_flavour(varname)
23367 char_u *varname;
23368{
23369 char_u *p = varname;
23370
23371 if (ASCII_ISUPPER(*p))
23372 {
23373 while (*(++p))
23374 if (ASCII_ISLOWER(*p))
23375 return VAR_FLAVOUR_SESSION;
23376 return VAR_FLAVOUR_VIMINFO;
23377 }
23378 else
23379 return VAR_FLAVOUR_DEFAULT;
23380}
23381#endif
23382
23383#if defined(FEAT_VIMINFO) || defined(PROTO)
23384/*
23385 * Restore global vars that start with a capital from the viminfo file
23386 */
23387 int
23388read_viminfo_varlist(virp, writing)
23389 vir_T *virp;
23390 int writing;
23391{
23392 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023393 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023394 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395
23396 if (!writing && (find_viminfo_parameter('!') != NULL))
23397 {
23398 tab = vim_strchr(virp->vir_line + 1, '\t');
23399 if (tab != NULL)
23400 {
23401 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023402 switch (*tab)
23403 {
23404 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023405#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023406 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023407#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023408 case 'D': type = VAR_DICT; break;
23409 case 'L': type = VAR_LIST; break;
23410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411
23412 tab = vim_strchr(tab, '\t');
23413 if (tab != NULL)
23414 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023415 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023416 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023417 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023418 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023419#ifdef FEAT_FLOAT
23420 else if (type == VAR_FLOAT)
23421 (void)string2float(tab + 1, &tv.vval.v_float);
23422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023423 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023424 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023425 if (type == VAR_DICT || type == VAR_LIST)
23426 {
23427 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23428
23429 if (etv == NULL)
23430 /* Failed to parse back the dict or list, use it as a
23431 * string. */
23432 tv.v_type = VAR_STRING;
23433 else
23434 {
23435 vim_free(tv.vval.v_string);
23436 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023437 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023438 }
23439 }
23440
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023441 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023442
23443 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023444 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023445 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23446 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023447 }
23448 }
23449 }
23450
23451 return viminfo_readline(virp);
23452}
23453
23454/*
23455 * Write global vars that start with a capital to the viminfo file
23456 */
23457 void
23458write_viminfo_varlist(fp)
23459 FILE *fp;
23460{
Bram Moolenaar33570922005-01-25 22:26:29 +000023461 hashitem_T *hi;
23462 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023463 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023464 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023465 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023466 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023467 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023468
23469 if (find_viminfo_parameter('!') == NULL)
23470 return;
23471
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023472 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023473
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023474 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023475 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023477 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023479 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023480 this_var = HI2DI(hi);
23481 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023482 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023483 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023484 {
23485 case VAR_STRING: s = "STR"; break;
23486 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023487#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023488 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023489#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023490 case VAR_DICT: s = "DIC"; break;
23491 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023492 default: continue;
23493 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023494 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023495 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023496 if (p != NULL)
23497 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023498 vim_free(tofree);
23499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 }
23501 }
23502}
23503#endif
23504
23505#if defined(FEAT_SESSION) || defined(PROTO)
23506 int
23507store_session_globals(fd)
23508 FILE *fd;
23509{
Bram Moolenaar33570922005-01-25 22:26:29 +000023510 hashitem_T *hi;
23511 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023512 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023513 char_u *p, *t;
23514
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023515 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023516 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023517 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023518 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023519 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023520 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023521 this_var = HI2DI(hi);
23522 if ((this_var->di_tv.v_type == VAR_NUMBER
23523 || this_var->di_tv.v_type == VAR_STRING)
23524 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023525 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023526 /* Escape special characters with a backslash. Turn a LF and
23527 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023528 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023529 (char_u *)"\\\"\n\r");
23530 if (p == NULL) /* out of memory */
23531 break;
23532 for (t = p; *t != NUL; ++t)
23533 if (*t == '\n')
23534 *t = 'n';
23535 else if (*t == '\r')
23536 *t = 'r';
23537 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023538 this_var->di_key,
23539 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23540 : ' ',
23541 p,
23542 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23543 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023544 || put_eol(fd) == FAIL)
23545 {
23546 vim_free(p);
23547 return FAIL;
23548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023549 vim_free(p);
23550 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023551#ifdef FEAT_FLOAT
23552 else if (this_var->di_tv.v_type == VAR_FLOAT
23553 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23554 {
23555 float_T f = this_var->di_tv.vval.v_float;
23556 int sign = ' ';
23557
23558 if (f < 0)
23559 {
23560 f = -f;
23561 sign = '-';
23562 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023563 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023564 this_var->di_key, sign, f) < 0)
23565 || put_eol(fd) == FAIL)
23566 return FAIL;
23567 }
23568#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023569 }
23570 }
23571 return OK;
23572}
23573#endif
23574
Bram Moolenaar661b1822005-07-28 22:36:45 +000023575/*
23576 * Display script name where an item was last set.
23577 * Should only be invoked when 'verbose' is non-zero.
23578 */
23579 void
23580last_set_msg(scriptID)
23581 scid_T scriptID;
23582{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023583 char_u *p;
23584
Bram Moolenaar661b1822005-07-28 22:36:45 +000023585 if (scriptID != 0)
23586 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023587 p = home_replace_save(NULL, get_scriptname(scriptID));
23588 if (p != NULL)
23589 {
23590 verbose_enter();
23591 MSG_PUTS(_("\n\tLast set from "));
23592 MSG_PUTS(p);
23593 vim_free(p);
23594 verbose_leave();
23595 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023596 }
23597}
23598
Bram Moolenaard812df62008-11-09 12:46:09 +000023599/*
23600 * List v:oldfiles in a nice way.
23601 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023602 void
23603ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023604 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023605{
23606 list_T *l = vimvars[VV_OLDFILES].vv_list;
23607 listitem_T *li;
23608 int nr = 0;
23609
23610 if (l == NULL)
23611 msg((char_u *)_("No old files"));
23612 else
23613 {
23614 msg_start();
23615 msg_scroll = TRUE;
23616 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23617 {
23618 msg_outnum((long)++nr);
23619 MSG_PUTS(": ");
23620 msg_outtrans(get_tv_string(&li->li_tv));
23621 msg_putchar('\n');
23622 out_flush(); /* output one line at a time */
23623 ui_breakcheck();
23624 }
23625 /* Assume "got_int" was set to truncate the listing. */
23626 got_int = FALSE;
23627
23628#ifdef FEAT_BROWSE_CMD
23629 if (cmdmod.browse)
23630 {
23631 quit_more = FALSE;
23632 nr = prompt_for_number(FALSE);
23633 msg_starthere();
23634 if (nr > 0)
23635 {
23636 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23637 (long)nr);
23638
23639 if (p != NULL)
23640 {
23641 p = expand_env_save(p);
23642 eap->arg = p;
23643 eap->cmdidx = CMD_edit;
23644 cmdmod.browse = FALSE;
23645 do_exedit(eap, NULL);
23646 vim_free(p);
23647 }
23648 }
23649 }
23650#endif
23651 }
23652}
23653
Bram Moolenaar071d4272004-06-13 20:20:40 +000023654#endif /* FEAT_EVAL */
23655
Bram Moolenaar071d4272004-06-13 20:20:40 +000023656
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023657#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658
23659#ifdef WIN3264
23660/*
23661 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23662 */
23663static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23664static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23665static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23666
23667/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023668 * Get the short path (8.3) for the filename in "fnamep".
23669 * Only works for a valid file name.
23670 * When the path gets longer "fnamep" is changed and the allocated buffer
23671 * is put in "bufp".
23672 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23673 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023674 */
23675 static int
23676get_short_pathname(fnamep, bufp, fnamelen)
23677 char_u **fnamep;
23678 char_u **bufp;
23679 int *fnamelen;
23680{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023681 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023682 char_u *newbuf;
23683
23684 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023685 l = GetShortPathName(*fnamep, *fnamep, len);
23686 if (l > len - 1)
23687 {
23688 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023689 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023690 newbuf = vim_strnsave(*fnamep, l);
23691 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023692 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693
23694 vim_free(*bufp);
23695 *fnamep = *bufp = newbuf;
23696
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023697 /* Really should always succeed, as the buffer is big enough. */
23698 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023699 }
23700
23701 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023702 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023703}
23704
23705/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023706 * Get the short path (8.3) for the filename in "fname". The converted
23707 * path is returned in "bufp".
23708 *
23709 * Some of the directories specified in "fname" may not exist. This function
23710 * will shorten the existing directories at the beginning of the path and then
23711 * append the remaining non-existing path.
23712 *
23713 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023714 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023715 * bufp - Pointer to an allocated buffer for the filename.
23716 * fnamelen - Length of the filename pointed to by fname
23717 *
23718 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023719 */
23720 static int
23721shortpath_for_invalid_fname(fname, bufp, fnamelen)
23722 char_u **fname;
23723 char_u **bufp;
23724 int *fnamelen;
23725{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023726 char_u *short_fname, *save_fname, *pbuf_unused;
23727 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023729 int old_len, len;
23730 int new_len, sfx_len;
23731 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732
23733 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023734 old_len = *fnamelen;
23735 save_fname = vim_strnsave(*fname, old_len);
23736 pbuf_unused = NULL;
23737 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023738
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023739 endp = save_fname + old_len - 1; /* Find the end of the copy */
23740 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023741
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023742 /*
23743 * Try shortening the supplied path till it succeeds by removing one
23744 * directory at a time from the tail of the path.
23745 */
23746 len = 0;
23747 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023748 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023749 /* go back one path-separator */
23750 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23751 --endp;
23752 if (endp <= save_fname)
23753 break; /* processed the complete path */
23754
23755 /*
23756 * Replace the path separator with a NUL and try to shorten the
23757 * resulting path.
23758 */
23759 ch = *endp;
23760 *endp = 0;
23761 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023762 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023763 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23764 {
23765 retval = FAIL;
23766 goto theend;
23767 }
23768 *endp = ch; /* preserve the string */
23769
23770 if (len > 0)
23771 break; /* successfully shortened the path */
23772
23773 /* failed to shorten the path. Skip the path separator */
23774 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023775 }
23776
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023777 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023778 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023779 /*
23780 * Succeeded in shortening the path. Now concatenate the shortened
23781 * path with the remaining path at the tail.
23782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023783
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023784 /* Compute the length of the new path. */
23785 sfx_len = (int)(save_endp - endp) + 1;
23786 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023787
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023788 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023789 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023790 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023791 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023792 /* There is not enough space in the currently allocated string,
23793 * copy it to a buffer big enough. */
23794 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023796 {
23797 retval = FAIL;
23798 goto theend;
23799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023800 }
23801 else
23802 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023803 /* Transfer short_fname to the main buffer (it's big enough),
23804 * unless get_short_pathname() did its work in-place. */
23805 *fname = *bufp = save_fname;
23806 if (short_fname != save_fname)
23807 vim_strncpy(save_fname, short_fname, len);
23808 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023809 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023810
23811 /* concat the not-shortened part of the path */
23812 vim_strncpy(*fname + len, endp, sfx_len);
23813 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023814 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023815
23816theend:
23817 vim_free(pbuf_unused);
23818 vim_free(save_fname);
23819
23820 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023821}
23822
23823/*
23824 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023825 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023826 */
23827 static int
23828shortpath_for_partial(fnamep, bufp, fnamelen)
23829 char_u **fnamep;
23830 char_u **bufp;
23831 int *fnamelen;
23832{
23833 int sepcount, len, tflen;
23834 char_u *p;
23835 char_u *pbuf, *tfname;
23836 int hasTilde;
23837
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023838 /* Count up the path separators from the RHS.. so we know which part
23839 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023841 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 if (vim_ispathsep(*p))
23843 ++sepcount;
23844
23845 /* Need full path first (use expand_env() to remove a "~/") */
23846 hasTilde = (**fnamep == '~');
23847 if (hasTilde)
23848 pbuf = tfname = expand_env_save(*fnamep);
23849 else
23850 pbuf = tfname = FullName_save(*fnamep, FALSE);
23851
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023852 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023853
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023854 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23855 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023856
23857 if (len == 0)
23858 {
23859 /* Don't have a valid filename, so shorten the rest of the
23860 * path if we can. This CAN give us invalid 8.3 filenames, but
23861 * there's not a lot of point in guessing what it might be.
23862 */
23863 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023864 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23865 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023866 }
23867
23868 /* Count the paths backward to find the beginning of the desired string. */
23869 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023870 {
23871#ifdef FEAT_MBYTE
23872 if (has_mbyte)
23873 p -= mb_head_off(tfname, p);
23874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023875 if (vim_ispathsep(*p))
23876 {
23877 if (sepcount == 0 || (hasTilde && sepcount == 1))
23878 break;
23879 else
23880 sepcount --;
23881 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 if (hasTilde)
23884 {
23885 --p;
23886 if (p >= tfname)
23887 *p = '~';
23888 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023889 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890 }
23891 else
23892 ++p;
23893
23894 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23895 vim_free(*bufp);
23896 *fnamelen = (int)STRLEN(p);
23897 *bufp = pbuf;
23898 *fnamep = p;
23899
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023900 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023901}
23902#endif /* WIN3264 */
23903
23904/*
23905 * Adjust a filename, according to a string of modifiers.
23906 * *fnamep must be NUL terminated when called. When returning, the length is
23907 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023908 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023909 * When there is an error, *fnamep is set to NULL.
23910 */
23911 int
23912modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23913 char_u *src; /* string with modifiers */
23914 int *usedlen; /* characters after src that are used */
23915 char_u **fnamep; /* file name so far */
23916 char_u **bufp; /* buffer for allocated file name or NULL */
23917 int *fnamelen; /* length of fnamep */
23918{
23919 int valid = 0;
23920 char_u *tail;
23921 char_u *s, *p, *pbuf;
23922 char_u dirname[MAXPATHL];
23923 int c;
23924 int has_fullname = 0;
23925#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023926 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023927 int has_shortname = 0;
23928#endif
23929
23930repeat:
23931 /* ":p" - full path/file_name */
23932 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23933 {
23934 has_fullname = 1;
23935
23936 valid |= VALID_PATH;
23937 *usedlen += 2;
23938
23939 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23940 if ((*fnamep)[0] == '~'
23941#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23942 && ((*fnamep)[1] == '/'
23943# ifdef BACKSLASH_IN_FILENAME
23944 || (*fnamep)[1] == '\\'
23945# endif
23946 || (*fnamep)[1] == NUL)
23947
23948#endif
23949 )
23950 {
23951 *fnamep = expand_env_save(*fnamep);
23952 vim_free(*bufp); /* free any allocated file name */
23953 *bufp = *fnamep;
23954 if (*fnamep == NULL)
23955 return -1;
23956 }
23957
23958 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023959 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023960 {
23961 if (vim_ispathsep(*p)
23962 && p[1] == '.'
23963 && (p[2] == NUL
23964 || vim_ispathsep(p[2])
23965 || (p[2] == '.'
23966 && (p[3] == NUL || vim_ispathsep(p[3])))))
23967 break;
23968 }
23969
23970 /* FullName_save() is slow, don't use it when not needed. */
23971 if (*p != NUL || !vim_isAbsName(*fnamep))
23972 {
23973 *fnamep = FullName_save(*fnamep, *p != NUL);
23974 vim_free(*bufp); /* free any allocated file name */
23975 *bufp = *fnamep;
23976 if (*fnamep == NULL)
23977 return -1;
23978 }
23979
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023980#ifdef WIN3264
23981# if _WIN32_WINNT >= 0x0500
23982 if (vim_strchr(*fnamep, '~') != NULL)
23983 {
23984 /* Expand 8.3 filename to full path. Needed to make sure the same
23985 * file does not have two different names.
23986 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23987 p = alloc(_MAX_PATH + 1);
23988 if (p != NULL)
23989 {
23990 if (GetLongPathName(*fnamep, p, MAXPATHL))
23991 {
23992 vim_free(*bufp);
23993 *bufp = *fnamep = p;
23994 }
23995 else
23996 vim_free(p);
23997 }
23998 }
23999# endif
24000#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024001 /* Append a path separator to a directory. */
24002 if (mch_isdir(*fnamep))
24003 {
24004 /* Make room for one or two extra characters. */
24005 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24006 vim_free(*bufp); /* free any allocated file name */
24007 *bufp = *fnamep;
24008 if (*fnamep == NULL)
24009 return -1;
24010 add_pathsep(*fnamep);
24011 }
24012 }
24013
24014 /* ":." - path relative to the current directory */
24015 /* ":~" - path relative to the home directory */
24016 /* ":8" - shortname path - postponed till after */
24017 while (src[*usedlen] == ':'
24018 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24019 {
24020 *usedlen += 2;
24021 if (c == '8')
24022 {
24023#ifdef WIN3264
24024 has_shortname = 1; /* Postpone this. */
24025#endif
24026 continue;
24027 }
24028 pbuf = NULL;
24029 /* Need full path first (use expand_env() to remove a "~/") */
24030 if (!has_fullname)
24031 {
24032 if (c == '.' && **fnamep == '~')
24033 p = pbuf = expand_env_save(*fnamep);
24034 else
24035 p = pbuf = FullName_save(*fnamep, FALSE);
24036 }
24037 else
24038 p = *fnamep;
24039
24040 has_fullname = 0;
24041
24042 if (p != NULL)
24043 {
24044 if (c == '.')
24045 {
24046 mch_dirname(dirname, MAXPATHL);
24047 s = shorten_fname(p, dirname);
24048 if (s != NULL)
24049 {
24050 *fnamep = s;
24051 if (pbuf != NULL)
24052 {
24053 vim_free(*bufp); /* free any allocated file name */
24054 *bufp = pbuf;
24055 pbuf = NULL;
24056 }
24057 }
24058 }
24059 else
24060 {
24061 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24062 /* Only replace it when it starts with '~' */
24063 if (*dirname == '~')
24064 {
24065 s = vim_strsave(dirname);
24066 if (s != NULL)
24067 {
24068 *fnamep = s;
24069 vim_free(*bufp);
24070 *bufp = s;
24071 }
24072 }
24073 }
24074 vim_free(pbuf);
24075 }
24076 }
24077
24078 tail = gettail(*fnamep);
24079 *fnamelen = (int)STRLEN(*fnamep);
24080
24081 /* ":h" - head, remove "/file_name", can be repeated */
24082 /* Don't remove the first "/" or "c:\" */
24083 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24084 {
24085 valid |= VALID_HEAD;
24086 *usedlen += 2;
24087 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024088 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024089 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024090 *fnamelen = (int)(tail - *fnamep);
24091#ifdef VMS
24092 if (*fnamelen > 0)
24093 *fnamelen += 1; /* the path separator is part of the path */
24094#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024095 if (*fnamelen == 0)
24096 {
24097 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24098 p = vim_strsave((char_u *)".");
24099 if (p == NULL)
24100 return -1;
24101 vim_free(*bufp);
24102 *bufp = *fnamep = tail = p;
24103 *fnamelen = 1;
24104 }
24105 else
24106 {
24107 while (tail > s && !after_pathsep(s, tail))
24108 mb_ptr_back(*fnamep, tail);
24109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024110 }
24111
24112 /* ":8" - shortname */
24113 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24114 {
24115 *usedlen += 2;
24116#ifdef WIN3264
24117 has_shortname = 1;
24118#endif
24119 }
24120
24121#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024122 /*
24123 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024124 */
24125 if (has_shortname)
24126 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024127 /* Copy the string if it is shortened by :h and when it wasn't copied
24128 * yet, because we are going to change it in place. Avoids changing
24129 * the buffer name for "%:8". */
24130 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024131 {
24132 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024133 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024134 return -1;
24135 vim_free(*bufp);
24136 *bufp = *fnamep = p;
24137 }
24138
24139 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024140 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024141 if (!has_fullname && !vim_isAbsName(*fnamep))
24142 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024143 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024144 return -1;
24145 }
24146 else
24147 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024148 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149
Bram Moolenaardc935552011-08-17 15:23:23 +020024150 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024152 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153 return -1;
24154
24155 if (l == 0)
24156 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024157 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024158 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024159 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024160 return -1;
24161 }
24162 *fnamelen = l;
24163 }
24164 }
24165#endif /* WIN3264 */
24166
24167 /* ":t" - tail, just the basename */
24168 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24169 {
24170 *usedlen += 2;
24171 *fnamelen -= (int)(tail - *fnamep);
24172 *fnamep = tail;
24173 }
24174
24175 /* ":e" - extension, can be repeated */
24176 /* ":r" - root, without extension, can be repeated */
24177 while (src[*usedlen] == ':'
24178 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24179 {
24180 /* find a '.' in the tail:
24181 * - for second :e: before the current fname
24182 * - otherwise: The last '.'
24183 */
24184 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24185 s = *fnamep - 2;
24186 else
24187 s = *fnamep + *fnamelen - 1;
24188 for ( ; s > tail; --s)
24189 if (s[0] == '.')
24190 break;
24191 if (src[*usedlen + 1] == 'e') /* :e */
24192 {
24193 if (s > tail)
24194 {
24195 *fnamelen += (int)(*fnamep - (s + 1));
24196 *fnamep = s + 1;
24197#ifdef VMS
24198 /* cut version from the extension */
24199 s = *fnamep + *fnamelen - 1;
24200 for ( ; s > *fnamep; --s)
24201 if (s[0] == ';')
24202 break;
24203 if (s > *fnamep)
24204 *fnamelen = s - *fnamep;
24205#endif
24206 }
24207 else if (*fnamep <= tail)
24208 *fnamelen = 0;
24209 }
24210 else /* :r */
24211 {
24212 if (s > tail) /* remove one extension */
24213 *fnamelen = (int)(s - *fnamep);
24214 }
24215 *usedlen += 2;
24216 }
24217
24218 /* ":s?pat?foo?" - substitute */
24219 /* ":gs?pat?foo?" - global substitute */
24220 if (src[*usedlen] == ':'
24221 && (src[*usedlen + 1] == 's'
24222 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24223 {
24224 char_u *str;
24225 char_u *pat;
24226 char_u *sub;
24227 int sep;
24228 char_u *flags;
24229 int didit = FALSE;
24230
24231 flags = (char_u *)"";
24232 s = src + *usedlen + 2;
24233 if (src[*usedlen + 1] == 'g')
24234 {
24235 flags = (char_u *)"g";
24236 ++s;
24237 }
24238
24239 sep = *s++;
24240 if (sep)
24241 {
24242 /* find end of pattern */
24243 p = vim_strchr(s, sep);
24244 if (p != NULL)
24245 {
24246 pat = vim_strnsave(s, (int)(p - s));
24247 if (pat != NULL)
24248 {
24249 s = p + 1;
24250 /* find end of substitution */
24251 p = vim_strchr(s, sep);
24252 if (p != NULL)
24253 {
24254 sub = vim_strnsave(s, (int)(p - s));
24255 str = vim_strnsave(*fnamep, *fnamelen);
24256 if (sub != NULL && str != NULL)
24257 {
24258 *usedlen = (int)(p + 1 - src);
24259 s = do_string_sub(str, pat, sub, flags);
24260 if (s != NULL)
24261 {
24262 *fnamep = s;
24263 *fnamelen = (int)STRLEN(s);
24264 vim_free(*bufp);
24265 *bufp = s;
24266 didit = TRUE;
24267 }
24268 }
24269 vim_free(sub);
24270 vim_free(str);
24271 }
24272 vim_free(pat);
24273 }
24274 }
24275 /* after using ":s", repeat all the modifiers */
24276 if (didit)
24277 goto repeat;
24278 }
24279 }
24280
24281 return valid;
24282}
24283
24284/*
24285 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24286 * "flags" can be "g" to do a global substitute.
24287 * Returns an allocated string, NULL for error.
24288 */
24289 char_u *
24290do_string_sub(str, pat, sub, flags)
24291 char_u *str;
24292 char_u *pat;
24293 char_u *sub;
24294 char_u *flags;
24295{
24296 int sublen;
24297 regmatch_T regmatch;
24298 int i;
24299 int do_all;
24300 char_u *tail;
24301 garray_T ga;
24302 char_u *ret;
24303 char_u *save_cpo;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024304 int zero_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024305
24306 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24307 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024308 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024309
24310 ga_init2(&ga, 1, 200);
24311
24312 do_all = (flags[0] == 'g');
24313
24314 regmatch.rm_ic = p_ic;
24315 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24316 if (regmatch.regprog != NULL)
24317 {
24318 tail = str;
24319 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24320 {
24321 /*
24322 * Get some space for a temporary buffer to do the substitution
24323 * into. It will contain:
24324 * - The text up to where the match is.
24325 * - The substituted text.
24326 * - The text after the match.
24327 */
24328 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24329 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24330 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24331 {
24332 ga_clear(&ga);
24333 break;
24334 }
24335
24336 /* copy the text up to where the match is */
24337 i = (int)(regmatch.startp[0] - tail);
24338 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24339 /* add the substituted text */
24340 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24341 + ga.ga_len + i, TRUE, TRUE, FALSE);
24342 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024343 zero_width = (tail == regmatch.endp[0]
24344 || regmatch.startp[0] == regmatch.endp[0]);
24345 tail = regmatch.endp[0];
24346 if (*tail == NUL)
24347 break;
24348 if (zero_width)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024349 {
Bram Moolenaarceb84af2013-09-29 21:11:05 +020024350 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024351 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24352 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024354 if (!do_all)
24355 break;
24356 }
24357
24358 if (ga.ga_data != NULL)
24359 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24360
Bram Moolenaar473de612013-06-08 18:19:48 +020024361 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024362 }
24363
24364 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24365 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024366 if (p_cpo == empty_option)
24367 p_cpo = save_cpo;
24368 else
24369 /* Darn, evaluating {sub} expression changed the value. */
24370 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024371
24372 return ret;
24373}
24374
24375#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */