blob: 807efe25f3b7666aa5fec8aafe146f1844d31086 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar8c8de832008-06-24 22:58:06 +000030#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
31# include <math.h>
Bram Moolenaar071d4272004-06-13 20:20:40 +000032#endif
33
Bram Moolenaar33570922005-01-25 22:26:29 +000034#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000035
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000036#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
37 be freed. */
38
Bram Moolenaar071d4272004-06-13 20:20:40 +000039/*
Bram Moolenaar33570922005-01-25 22:26:29 +000040 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
41 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000042 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
43 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
44 * HI2DI() converts a hashitem pointer to a dictitem pointer.
45 */
Bram Moolenaar33570922005-01-25 22:26:29 +000046static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000047#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000048#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000049#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000050
51/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000052 * Structure returned by get_lval() and used by set_var_lval().
53 * For a plain name:
54 * "name" points to the variable name.
55 * "exp_name" is NULL.
56 * "tv" is NULL
57 * For a magic braces name:
58 * "name" points to the expanded variable name.
59 * "exp_name" is non-NULL, to be freed later.
60 * "tv" is NULL
61 * For an index in a list:
62 * "name" points to the (expanded) variable name.
63 * "exp_name" NULL or non-NULL, to be freed later.
64 * "tv" points to the (first) list item value
65 * "li" points to the (first) list item
66 * "range", "n1", "n2" and "empty2" indicate what items are used.
67 * For an existing Dict item:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the dict item value
71 * "newkey" is NULL
72 * For a non-existing Dict item:
73 * "name" points to the (expanded) variable name.
74 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000075 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000076 * "newkey" is the key for the new item.
77 */
78typedef struct lval_S
79{
80 char_u *ll_name; /* start of variable name (can be NULL) */
81 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000083 isn't NULL it's the Dict to which to add
84 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000085 listitem_T *ll_li; /* The list item or NULL. */
86 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000087 int ll_range; /* TRUE when a [i:j] range was used */
88 long ll_n1; /* First index for list */
89 long ll_n2; /* Second index for list range */
90 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 dict_T *ll_dict; /* The Dictionary or NULL */
92 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000093 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000094} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000095
Bram Moolenaar8c711452005-01-14 21:53:12 +000096
Bram Moolenaarc70646c2005-01-04 21:52:38 +000097static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000098static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000099static char *e_undefvar = N_("E121: Undefined variable: %s");
100static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000102static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaarce5e58e2005-01-19 22:24:34 +0000103static char *e_emptykey = N_("E713: Cannot use empty key for Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000104static char *e_listreq = N_("E714: List required");
105static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000107static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
108static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
109static char *e_funcdict = N_("E717: Dictionary entry already exists");
110static char *e_funcref = N_("E718: Funcref required");
111static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
112static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000113static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000114static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200115#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200116static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200117#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000118
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200119static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000120#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121
122/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000123 * Old Vim variables such as "v:version" are also available without the "v:".
124 * Also in functions. We need a special hashtable for them.
125 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000126static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000127
Bram Moolenaar2cefbed2010-07-11 23:12:29 +0200128/* When using exists() don't auto-load a script. */
129static int no_autoload = FALSE;
130
Bram Moolenaar532c7802005-01-27 14:44:31 +0000131/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000132 * When recursively copying lists and dicts we need to remember which ones we
133 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000134 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135 */
136static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000137#define COPYID_INC 2
138#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000139
140/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000141 * Array to hold the hashtab with variables local to each sourced script.
142 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000144typedef struct
145{
146 dictitem_T sv_var;
147 dict_T sv_dict;
148} scriptvar_T;
149
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200150static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
151#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
152#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153
154static int echo_attr = 0; /* attributes used for ":echo" */
155
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000156/* Values for trans_function_name() argument: */
157#define TFN_INT 1 /* internal function name OK */
158#define TFN_QUIET 2 /* no error messages */
159
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160/*
161 * Structure to hold info for a user function.
162 */
163typedef struct ufunc ufunc_T;
164
165struct ufunc
166{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000167 int uf_varargs; /* variable nr of arguments */
168 int uf_flags;
169 int uf_calls; /* nr of active calls */
170 garray_T uf_args; /* arguments */
171 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000172#ifdef FEAT_PROFILE
173 int uf_profiling; /* TRUE when func is being profiled */
174 /* profiling the function as a whole */
175 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000176 proftime_T uf_tm_total; /* time spent in function + children */
177 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000178 proftime_T uf_tm_children; /* time spent in children this call */
179 /* profiling the function per line */
180 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000181 proftime_T *uf_tml_total; /* time spent in a line + children */
182 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183 proftime_T uf_tml_start; /* start time for current line */
184 proftime_T uf_tml_children; /* time spent in children for this line */
185 proftime_T uf_tml_wait; /* start wait time for current line */
186 int uf_tml_idx; /* index of line being timed; -1 if none */
187 int uf_tml_execed; /* line being timed was executed */
188#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000189 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 int uf_refcount; /* for numbered function: reference count */
192 char_u uf_name[1]; /* name of function (actually longer); can
193 start with <SNR>123_ (<SNR> is K_SPECIAL
194 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195};
196
197/* function flags */
198#define FC_ABORT 1 /* abort function on error */
199#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000200#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201
202/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000203 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000205static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000207/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000208static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
209
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000210/* list heads for garbage collection */
211static dict_T *first_dict = NULL; /* list of all dicts */
212static list_T *first_list = NULL; /* list of all lists */
213
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000214/* From user function to hashitem and back. */
215static ufunc_T dumuf;
216#define UF2HIKEY(fp) ((fp)->uf_name)
217#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
218#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
219
220#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
221#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222
Bram Moolenaar33570922005-01-25 22:26:29 +0000223#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
224#define VAR_SHORT_LEN 20 /* short variable name length */
225#define FIXVAR_CNT 12 /* number of fixed variables */
226
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000228typedef struct funccall_S funccall_T;
229
230struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231{
232 ufunc_T *func; /* function being called */
233 int linenr; /* next line to be executed */
234 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000235 struct /* fixed variables for arguments */
236 {
237 dictitem_T var; /* variable (without room for name) */
238 char_u room[VAR_SHORT_LEN]; /* room for the name */
239 } fixvar[FIXVAR_CNT];
240 dict_T l_vars; /* l: local function variables */
241 dictitem_T l_vars_var; /* variable for l: scope */
242 dict_T l_avars; /* a: argument variables */
243 dictitem_T l_avars_var; /* variable for a: scope */
244 list_T l_varlist; /* list for a:000 */
245 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
246 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247 linenr_T breakpoint; /* next line with breakpoint or zero */
248 int dbg_tick; /* debug_tick when breakpoint was set */
249 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000250#ifdef FEAT_PROFILE
251 proftime_T prof_child; /* time spent in a child */
252#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000253 funccall_T *caller; /* calling function or NULL */
254};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255
256/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000257 * Info used by a ":for" loop.
258 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000259typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000260{
261 int fi_semicolon; /* TRUE if ending in '; var]' */
262 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263 listwatch_T fi_lw; /* keep an eye on the item used. */
264 list_T *fi_list; /* list being used */
265} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000266
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000267/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000268 * Struct used by trans_function_name()
269 */
270typedef struct
271{
Bram Moolenaar33570922005-01-25 22:26:29 +0000272 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000273 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dictitem_T *fd_di; /* Dictionary item used */
275} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000276
Bram Moolenaara7043832005-01-21 11:56:39 +0000277
278/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000279 * Array to hold the value of v: variables.
280 * The value is in a dictitem, so that it can also be used in the v: scope.
281 * The reason to use this table anyway is for very quick access to the
282 * variables with the VV_ defines.
283 */
284#include "version.h"
285
286/* values for vv_flags: */
287#define VV_COMPAT 1 /* compatible, also used without "v:" */
288#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000289#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000290
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000291#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
293static struct vimvar
294{
295 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000296 dictitem_T vv_di; /* value and name for key */
297 char vv_filler[16]; /* space for LONGEST name below!!! */
298 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
299} vimvars[VV_LEN] =
300{
301 /*
302 * The order here must match the VV_ defines in vim.h!
303 * Initializing a union does not work, leave tv.vval empty to get zero's.
304 */
305 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
306 {VV_NAME("count1", VAR_NUMBER), VV_RO},
307 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
308 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
309 {VV_NAME("warningmsg", VAR_STRING), 0},
310 {VV_NAME("statusmsg", VAR_STRING), 0},
311 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
312 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
313 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
315 {VV_NAME("termresponse", VAR_STRING), VV_RO},
316 {VV_NAME("fname", VAR_STRING), VV_RO},
317 {VV_NAME("lang", VAR_STRING), VV_RO},
318 {VV_NAME("lc_time", VAR_STRING), VV_RO},
319 {VV_NAME("ctype", VAR_STRING), VV_RO},
320 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
321 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
322 {VV_NAME("fname_in", VAR_STRING), VV_RO},
323 {VV_NAME("fname_out", VAR_STRING), VV_RO},
324 {VV_NAME("fname_new", VAR_STRING), VV_RO},
325 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
326 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
327 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
328 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
329 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
330 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("progname", VAR_STRING), VV_RO},
332 {VV_NAME("servername", VAR_STRING), VV_RO},
333 {VV_NAME("dying", VAR_NUMBER), VV_RO},
334 {VV_NAME("exception", VAR_STRING), VV_RO},
335 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
336 {VV_NAME("register", VAR_STRING), VV_RO},
337 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
338 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000339 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
340 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000341 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000342 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
343 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000344 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
345 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
346 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000349 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000350 {VV_NAME("swapname", VAR_STRING), VV_RO},
351 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000352 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200353 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000354 {VV_NAME("mouse_win", VAR_NUMBER), 0},
355 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
356 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000357 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000358 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000359 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200360 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000361};
362
363/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000364#define vv_type vv_di.di_tv.v_type
365#define vv_nr vv_di.di_tv.vval.v_number
366#define vv_float vv_di.di_tv.vval.v_float
367#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000368#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000369#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000370
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200371static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000372#define vimvarht vimvardict.dv_hashtab
373
Bram Moolenaara40058a2005-07-11 22:42:07 +0000374static void prepare_vimvar __ARGS((int idx, typval_T *save_tv));
375static void restore_vimvar __ARGS((int idx, typval_T *save_tv));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000376static int ex_let_vars __ARGS((char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars));
377static char_u *skip_var_list __ARGS((char_u *arg, int *var_count, int *semicolon));
378static char_u *skip_var_one __ARGS((char_u *arg));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000379static void list_hashtable_vars __ARGS((hashtab_T *ht, char_u *prefix, int empty, int *first));
380static void list_glob_vars __ARGS((int *first));
381static void list_buf_vars __ARGS((int *first));
382static void list_win_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000383#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000384static void list_tab_vars __ARGS((int *first));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000385#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000386static void list_vim_vars __ARGS((int *first));
387static void list_script_vars __ARGS((int *first));
388static void list_func_vars __ARGS((int *first));
389static char_u *list_arg_vars __ARGS((exarg_T *eap, char_u *arg, int *first));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000390static char_u *ex_let_one __ARGS((char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op));
391static int check_changedtick __ARGS((char_u *arg));
392static char_u *get_lval __ARGS((char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int quiet, int fne_flags));
393static void clear_lval __ARGS((lval_T *lp));
394static void set_var_lval __ARGS((lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op));
395static int tv_op __ARGS((typval_T *tv1, typval_T *tv2, char_u *op));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000396static void list_fix_watch __ARGS((list_T *l, listitem_T *item));
397static void ex_unletlock __ARGS((exarg_T *eap, char_u *argstart, int deep));
398static int do_unlet_var __ARGS((lval_T *lp, char_u *name_end, int forceit));
399static int do_lock_var __ARGS((lval_T *lp, char_u *name_end, int deep, int lock));
400static void item_lock __ARGS((typval_T *tv, int deep, int lock));
401static int tv_islocked __ARGS((typval_T *tv));
402
Bram Moolenaar33570922005-01-25 22:26:29 +0000403static int eval0 __ARGS((char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate));
404static int eval1 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
405static int eval2 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
406static int eval3 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
407static int eval4 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
408static int eval5 __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +0000409static int eval6 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
410static int eval7 __ARGS((char_u **arg, typval_T *rettv, int evaluate, int want_string));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000411
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000412static int eval_index __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000413static int get_option_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
414static int get_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
415static int get_lit_string_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
416static int get_list_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaareddf53b2006-02-27 00:11:10 +0000417static int rettv_list_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000418static long list_len __ARGS((list_T *l));
Bram Moolenaar67b3f992010-11-10 20:41:57 +0100419static int list_equal __ARGS((list_T *l1, list_T *l2, int ic, int recursive));
420static int dict_equal __ARGS((dict_T *d1, dict_T *d2, int ic, int recursive));
421static int tv_equal __ARGS((typval_T *tv1, typval_T *tv2, int ic, int recursive));
Bram Moolenaara5525202006-03-02 22:52:09 +0000422static long list_find_nr __ARGS((list_T *l, long idx, int *errorp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000423static long list_idx_of_item __ARGS((list_T *l, listitem_T *item));
Bram Moolenaar4463f292005-09-25 22:20:24 +0000424static int list_append_number __ARGS((list_T *l, varnumber_T n));
Bram Moolenaar33570922005-01-25 22:26:29 +0000425static int list_extend __ARGS((list_T *l1, list_T *l2, listitem_T *bef));
426static int list_concat __ARGS((list_T *l1, list_T *l2, typval_T *tv));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000427static list_T *list_copy __ARGS((list_T *orig, int deep, int copyID));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000428static char_u *list2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar3fe37d62012-02-06 00:13:22 +0100429static int list_join_inner __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000430static int list_join __ARGS((garray_T *gap, list_T *l, char_u *sep, int echo, int copyID));
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000431static int free_unref_items __ARGS((int copyID));
Bram Moolenaara800b422010-06-27 01:15:55 +0200432static int rettv_dict_alloc __ARGS((typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000433static dictitem_T *dictitem_copy __ARGS((dictitem_T *org));
434static void dictitem_remove __ARGS((dict_T *dict, dictitem_T *item));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000435static dict_T *dict_copy __ARGS((dict_T *orig, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000436static long dict_len __ARGS((dict_T *d));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000437static char_u *dict2string __ARGS((typval_T *tv, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000438static int get_dict_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000439static char_u *echo_string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
440static char_u *tv2string __ARGS((typval_T *tv, char_u **tofree, char_u *numbuf, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000441static char_u *string_quote __ARGS((char_u *str, int function));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000442#ifdef FEAT_FLOAT
443static int string2float __ARGS((char_u *text, float_T *value));
444#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000445static int get_env_tv __ARGS((char_u **arg, typval_T *rettv, int evaluate));
446static int find_internal_func __ARGS((char_u *name));
447static char_u *deref_func_name __ARGS((char_u *name, int *lenp));
448static int get_func_tv __ARGS((char_u *name, int len, typval_T *rettv, char_u **arg, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaarf506c5b2010-06-22 06:28:58 +0200449static int call_func __ARGS((char_u *funcname, int len, typval_T *rettv, int argcount, typval_T *argvars, linenr_T firstline, linenr_T lastline, int *doesrange, int evaluate, dict_T *selfdict));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000450static void emsg_funcname __ARGS((char *ermsg, char_u *name));
Bram Moolenaar05bb9532008-07-04 09:44:11 +0000451static int non_zero_arg __ARGS((typval_T *argvars));
Bram Moolenaar33570922005-01-25 22:26:29 +0000452
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000453#ifdef FEAT_FLOAT
454static void f_abs __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200455static void f_acos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000456#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000457static void f_add __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100458static void f_and __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000459static void f_append __ARGS((typval_T *argvars, typval_T *rettv));
460static void f_argc __ARGS((typval_T *argvars, typval_T *rettv));
461static void f_argidx __ARGS((typval_T *argvars, typval_T *rettv));
462static void f_argv __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200464static void f_asin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465static void f_atan __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200466static void f_atan2 __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000467#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000468static void f_browse __ARGS((typval_T *argvars, typval_T *rettv));
469static void f_browsedir __ARGS((typval_T *argvars, typval_T *rettv));
470static void f_bufexists __ARGS((typval_T *argvars, typval_T *rettv));
471static void f_buflisted __ARGS((typval_T *argvars, typval_T *rettv));
472static void f_bufloaded __ARGS((typval_T *argvars, typval_T *rettv));
473static void f_bufname __ARGS((typval_T *argvars, typval_T *rettv));
474static void f_bufnr __ARGS((typval_T *argvars, typval_T *rettv));
475static void f_bufwinnr __ARGS((typval_T *argvars, typval_T *rettv));
476static void f_byte2line __ARGS((typval_T *argvars, typval_T *rettv));
477static void f_byteidx __ARGS((typval_T *argvars, typval_T *rettv));
478static void f_call __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
480static void f_ceil __ARGS((typval_T *argvars, typval_T *rettv));
481#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +0000482static void f_changenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000483static void f_char2nr __ARGS((typval_T *argvars, typval_T *rettv));
484static void f_cindent __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000485static void f_clearmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000486static void f_col __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000487#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +0000488static void f_complete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar572cb562005-08-05 21:35:02 +0000489static void f_complete_add __ARGS((typval_T *argvars, typval_T *rettv));
490static void f_complete_check __ARGS((typval_T *argvars, typval_T *rettv));
491#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000492static void f_confirm __ARGS((typval_T *argvars, typval_T *rettv));
493static void f_copy __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000494#ifdef FEAT_FLOAT
495static void f_cos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200496static void f_cosh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000498static void f_count __ARGS((typval_T *argvars, typval_T *rettv));
499static void f_cscope_connection __ARGS((typval_T *argvars, typval_T *rettv));
500static void f_cursor __ARGS((typval_T *argsvars, typval_T *rettv));
501static void f_deepcopy __ARGS((typval_T *argvars, typval_T *rettv));
502static void f_delete __ARGS((typval_T *argvars, typval_T *rettv));
503static void f_did_filetype __ARGS((typval_T *argvars, typval_T *rettv));
504static void f_diff_filler __ARGS((typval_T *argvars, typval_T *rettv));
505static void f_diff_hlID __ARGS((typval_T *argvars, typval_T *rettv));
506static void f_empty __ARGS((typval_T *argvars, typval_T *rettv));
507static void f_escape __ARGS((typval_T *argvars, typval_T *rettv));
508static void f_eval __ARGS((typval_T *argvars, typval_T *rettv));
509static void f_eventhandler __ARGS((typval_T *argvars, typval_T *rettv));
510static void f_executable __ARGS((typval_T *argvars, typval_T *rettv));
511static void f_exists __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200512#ifdef FEAT_FLOAT
513static void f_exp __ARGS((typval_T *argvars, typval_T *rettv));
514#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000515static void f_expand __ARGS((typval_T *argvars, typval_T *rettv));
516static void f_extend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarf9393ef2006-04-24 19:47:27 +0000517static void f_feedkeys __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000518static void f_filereadable __ARGS((typval_T *argvars, typval_T *rettv));
519static void f_filewritable __ARGS((typval_T *argvars, typval_T *rettv));
520static void f_filter __ARGS((typval_T *argvars, typval_T *rettv));
521static void f_finddir __ARGS((typval_T *argvars, typval_T *rettv));
522static void f_findfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000523#ifdef FEAT_FLOAT
524static void f_float2nr __ARGS((typval_T *argvars, typval_T *rettv));
525static void f_floor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200526static void f_fmod __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000527#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +0000528static void f_fnameescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000529static void f_fnamemodify __ARGS((typval_T *argvars, typval_T *rettv));
530static void f_foldclosed __ARGS((typval_T *argvars, typval_T *rettv));
531static void f_foldclosedend __ARGS((typval_T *argvars, typval_T *rettv));
532static void f_foldlevel __ARGS((typval_T *argvars, typval_T *rettv));
533static void f_foldtext __ARGS((typval_T *argvars, typval_T *rettv));
534static void f_foldtextresult __ARGS((typval_T *argvars, typval_T *rettv));
535static void f_foreground __ARGS((typval_T *argvars, typval_T *rettv));
536static void f_function __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000537static void f_garbagecollect __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000538static void f_get __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +0000539static void f_getbufline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000540static void f_getbufvar __ARGS((typval_T *argvars, typval_T *rettv));
541static void f_getchar __ARGS((typval_T *argvars, typval_T *rettv));
542static void f_getcharmod __ARGS((typval_T *argvars, typval_T *rettv));
543static void f_getcmdline __ARGS((typval_T *argvars, typval_T *rettv));
544static void f_getcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000545static void f_getcmdtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000546static void f_getcwd __ARGS((typval_T *argvars, typval_T *rettv));
547static void f_getfontname __ARGS((typval_T *argvars, typval_T *rettv));
548static void f_getfperm __ARGS((typval_T *argvars, typval_T *rettv));
549static void f_getfsize __ARGS((typval_T *argvars, typval_T *rettv));
550static void f_getftime __ARGS((typval_T *argvars, typval_T *rettv));
551static void f_getftype __ARGS((typval_T *argvars, typval_T *rettv));
552static void f_getline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000553static void f_getmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar18081e32008-02-20 19:11:07 +0000554static void f_getpid __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara5525202006-03-02 22:52:09 +0000555static void f_getpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000556static void f_getqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000557static void f_getreg __ARGS((typval_T *argvars, typval_T *rettv));
558static void f_getregtype __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200559static void f_gettabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000560static void f_gettabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000561static void f_getwinposx __ARGS((typval_T *argvars, typval_T *rettv));
562static void f_getwinposy __ARGS((typval_T *argvars, typval_T *rettv));
563static void f_getwinvar __ARGS((typval_T *argvars, typval_T *rettv));
564static void f_glob __ARGS((typval_T *argvars, typval_T *rettv));
565static void f_globpath __ARGS((typval_T *argvars, typval_T *rettv));
566static void f_has __ARGS((typval_T *argvars, typval_T *rettv));
567static void f_has_key __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard267b9c2007-04-26 15:06:45 +0000568static void f_haslocaldir __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000569static void f_hasmapto __ARGS((typval_T *argvars, typval_T *rettv));
570static void f_histadd __ARGS((typval_T *argvars, typval_T *rettv));
571static void f_histdel __ARGS((typval_T *argvars, typval_T *rettv));
572static void f_histget __ARGS((typval_T *argvars, typval_T *rettv));
573static void f_histnr __ARGS((typval_T *argvars, typval_T *rettv));
574static void f_hlID __ARGS((typval_T *argvars, typval_T *rettv));
575static void f_hlexists __ARGS((typval_T *argvars, typval_T *rettv));
576static void f_hostname __ARGS((typval_T *argvars, typval_T *rettv));
577static void f_iconv __ARGS((typval_T *argvars, typval_T *rettv));
578static void f_indent __ARGS((typval_T *argvars, typval_T *rettv));
579static void f_index __ARGS((typval_T *argvars, typval_T *rettv));
580static void f_input __ARGS((typval_T *argvars, typval_T *rettv));
581static void f_inputdialog __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6efa2b32005-09-10 19:26:26 +0000582static void f_inputlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000583static void f_inputrestore __ARGS((typval_T *argvars, typval_T *rettv));
584static void f_inputsave __ARGS((typval_T *argvars, typval_T *rettv));
585static void f_inputsecret __ARGS((typval_T *argvars, typval_T *rettv));
586static void f_insert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100587static void f_invert __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000588static void f_isdirectory __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000589static void f_islocked __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000590static void f_items __ARGS((typval_T *argvars, typval_T *rettv));
591static void f_join __ARGS((typval_T *argvars, typval_T *rettv));
592static void f_keys __ARGS((typval_T *argvars, typval_T *rettv));
593static void f_last_buffer_nr __ARGS((typval_T *argvars, typval_T *rettv));
594static void f_len __ARGS((typval_T *argvars, typval_T *rettv));
595static void f_libcall __ARGS((typval_T *argvars, typval_T *rettv));
596static void f_libcallnr __ARGS((typval_T *argvars, typval_T *rettv));
597static void f_line __ARGS((typval_T *argvars, typval_T *rettv));
598static void f_line2byte __ARGS((typval_T *argvars, typval_T *rettv));
599static void f_lispindent __ARGS((typval_T *argvars, typval_T *rettv));
600static void f_localtime __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000601#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200602static void f_log __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000603static void f_log10 __ARGS((typval_T *argvars, typval_T *rettv));
604#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200605#ifdef FEAT_LUA
606static void f_luaeval __ARGS((typval_T *argvars, typval_T *rettv));
607#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000608static void f_map __ARGS((typval_T *argvars, typval_T *rettv));
609static void f_maparg __ARGS((typval_T *argvars, typval_T *rettv));
610static void f_mapcheck __ARGS((typval_T *argvars, typval_T *rettv));
611static void f_match __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000612static void f_matchadd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000613static void f_matcharg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000614static void f_matchdelete __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000615static void f_matchend __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000616static void f_matchlist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000617static void f_matchstr __ARGS((typval_T *argvars, typval_T *rettv));
618static void f_max __ARGS((typval_T *argvars, typval_T *rettv));
619static void f_min __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000620#ifdef vim_mkdir
621static void f_mkdir __ARGS((typval_T *argvars, typval_T *rettv));
622#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000623static void f_mode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100624#ifdef FEAT_MZSCHEME
625static void f_mzeval __ARGS((typval_T *argvars, typval_T *rettv));
626#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000627static void f_nextnonblank __ARGS((typval_T *argvars, typval_T *rettv));
628static void f_nr2char __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100629static void f_or __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000630static void f_pathshorten __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000631#ifdef FEAT_FLOAT
632static void f_pow __ARGS((typval_T *argvars, typval_T *rettv));
633#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000634static void f_prevnonblank __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000635static void f_printf __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000636static void f_pumvisible __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb913952012-06-29 12:54:53 +0200637#ifdef FEAT_PYTHON3
638static void f_py3eval __ARGS((typval_T *argvars, typval_T *rettv));
639#endif
640#ifdef FEAT_PYTHON
641static void f_pyeval __ARGS((typval_T *argvars, typval_T *rettv));
642#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000643static void f_range __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000644static void f_readfile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaare580b0c2006-03-21 21:33:03 +0000645static void f_reltime __ARGS((typval_T *argvars, typval_T *rettv));
646static void f_reltimestr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000647static void f_remote_expr __ARGS((typval_T *argvars, typval_T *rettv));
648static void f_remote_foreground __ARGS((typval_T *argvars, typval_T *rettv));
649static void f_remote_peek __ARGS((typval_T *argvars, typval_T *rettv));
650static void f_remote_read __ARGS((typval_T *argvars, typval_T *rettv));
651static void f_remote_send __ARGS((typval_T *argvars, typval_T *rettv));
652static void f_remove __ARGS((typval_T *argvars, typval_T *rettv));
653static void f_rename __ARGS((typval_T *argvars, typval_T *rettv));
654static void f_repeat __ARGS((typval_T *argvars, typval_T *rettv));
655static void f_resolve __ARGS((typval_T *argvars, typval_T *rettv));
656static void f_reverse __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#ifdef FEAT_FLOAT
658static void f_round __ARGS((typval_T *argvars, typval_T *rettv));
659#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +0200660static void f_screenattr __ARGS((typval_T *argvars, typval_T *rettv));
661static void f_screenchar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9750bb12012-12-05 16:10:42 +0100662static void f_screencol __ARGS((typval_T *argvars, typval_T *rettv));
663static void f_screenrow __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000664static void f_search __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardd2436f2005-09-05 22:14:46 +0000665static void f_searchdecl __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000666static void f_searchpair __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000667static void f_searchpairpos __ARGS((typval_T *argvars, typval_T *rettv));
668static void f_searchpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000669static void f_server2client __ARGS((typval_T *argvars, typval_T *rettv));
670static void f_serverlist __ARGS((typval_T *argvars, typval_T *rettv));
671static void f_setbufvar __ARGS((typval_T *argvars, typval_T *rettv));
672static void f_setcmdpos __ARGS((typval_T *argvars, typval_T *rettv));
673static void f_setline __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar17c7c012006-01-26 22:25:15 +0000674static void f_setloclist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar6ee10162007-07-26 20:58:42 +0000675static void f_setmatches __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000676static void f_setpos __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2641f772005-03-25 21:58:17 +0000677static void f_setqflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000678static void f_setreg __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar06b5d512010-05-22 15:37:44 +0200679static void f_settabvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000680static void f_settabwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000681static void f_setwinvar __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100682#ifdef FEAT_CRYPT
683static void f_sha256 __ARGS((typval_T *argvars, typval_T *rettv));
684#endif /* FEAT_CRYPT */
Bram Moolenaar60a495f2006-10-03 12:44:42 +0000685static void f_shellescape __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2d17fa32012-10-21 00:45:18 +0200686static void f_shiftwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000687static void f_simplify __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000688#ifdef FEAT_FLOAT
689static void f_sin __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200690static void f_sinh __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000692static void f_sort __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +0000693static void f_soundfold __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard857f0e2005-06-21 22:37:39 +0000694static void f_spellbadword __ARGS((typval_T *argvars, typval_T *rettv));
695static void f_spellsuggest __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000696static void f_split __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000697#ifdef FEAT_FLOAT
698static void f_sqrt __ARGS((typval_T *argvars, typval_T *rettv));
699static void f_str2float __ARGS((typval_T *argvars, typval_T *rettv));
700#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +0000701static void f_str2nr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200702static void f_strchars __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000703#ifdef HAVE_STRFTIME
704static void f_strftime __ARGS((typval_T *argvars, typval_T *rettv));
705#endif
706static void f_stridx __ARGS((typval_T *argvars, typval_T *rettv));
707static void f_string __ARGS((typval_T *argvars, typval_T *rettv));
708static void f_strlen __ARGS((typval_T *argvars, typval_T *rettv));
709static void f_strpart __ARGS((typval_T *argvars, typval_T *rettv));
710static void f_strridx __ARGS((typval_T *argvars, typval_T *rettv));
711static void f_strtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardc536092010-07-18 15:45:49 +0200712static void f_strdisplaywidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar72597a52010-07-18 15:31:08 +0200713static void f_strwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000714static void f_submatch __ARGS((typval_T *argvars, typval_T *rettv));
715static void f_substitute __ARGS((typval_T *argvars, typval_T *rettv));
716static void f_synID __ARGS((typval_T *argvars, typval_T *rettv));
717static void f_synIDattr __ARGS((typval_T *argvars, typval_T *rettv));
718static void f_synIDtrans __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar9d188ab2008-01-10 21:24:39 +0000719static void f_synstack __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7510fe72010-07-25 12:46:44 +0200720static void f_synconcealed __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000721static void f_system __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000722static void f_tabpagebuflist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar7e8fd632006-02-18 22:14:51 +0000723static void f_tabpagenr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaarfaa959a2006-02-20 21:37:40 +0000724static void f_tabpagewinnr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000725static void f_taglist __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard43b6cf2005-09-09 19:53:42 +0000726static void f_tagfiles __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000727static void f_tempname __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard52d9742005-08-21 22:20:28 +0000728static void f_test __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200729#ifdef FEAT_FLOAT
730static void f_tan __ARGS((typval_T *argvars, typval_T *rettv));
731static void f_tanh __ARGS((typval_T *argvars, typval_T *rettv));
732#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000733static void f_tolower __ARGS((typval_T *argvars, typval_T *rettv));
734static void f_toupper __ARGS((typval_T *argvars, typval_T *rettv));
735static void f_tr __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000736#ifdef FEAT_FLOAT
737static void f_trunc __ARGS((typval_T *argvars, typval_T *rettv));
738#endif
Bram Moolenaar33570922005-01-25 22:26:29 +0000739static void f_type __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara17d4c12010-05-30 18:30:36 +0200740static void f_undofile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaara800b422010-06-27 01:15:55 +0200741static void f_undotree __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000742static void f_values __ARGS((typval_T *argvars, typval_T *rettv));
743static void f_virtcol __ARGS((typval_T *argvars, typval_T *rettv));
744static void f_visualmode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar8738fc12013-02-20 17:59:11 +0100745static void f_wildmenumode __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000746static void f_winbufnr __ARGS((typval_T *argvars, typval_T *rettv));
747static void f_wincol __ARGS((typval_T *argvars, typval_T *rettv));
748static void f_winheight __ARGS((typval_T *argvars, typval_T *rettv));
749static void f_winline __ARGS((typval_T *argvars, typval_T *rettv));
750static void f_winnr __ARGS((typval_T *argvars, typval_T *rettv));
751static void f_winrestcmd __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar768b8c42006-03-04 21:58:33 +0000752static void f_winrestview __ARGS((typval_T *argvars, typval_T *rettv));
753static void f_winsaveview __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000754static void f_winwidth __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000755static void f_writefile __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaard6e256c2011-12-14 15:32:50 +0100756static void f_xor __ARGS((typval_T *argvars, typval_T *rettv));
Bram Moolenaar33570922005-01-25 22:26:29 +0000757
Bram Moolenaar0e34f622006-03-03 23:00:03 +0000758static int list2fpos __ARGS((typval_T *arg, pos_T *posp, int *fnump));
Bram Moolenaar477933c2007-07-17 14:32:23 +0000759static pos_T *var2fpos __ARGS((typval_T *varp, int dollar_lnum, int *fnum));
Bram Moolenaar33570922005-01-25 22:26:29 +0000760static int get_env_len __ARGS((char_u **arg));
761static int get_id_len __ARGS((char_u **arg));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000762static int get_name_len __ARGS((char_u **arg, char_u **alias, int evaluate, int verbose));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000763static char_u *find_name_end __ARGS((char_u *arg, char_u **expr_start, char_u **expr_end, int flags));
764#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
765#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
766 valid character */
Bram Moolenaara40058a2005-07-11 22:42:07 +0000767static char_u * make_expanded_name __ARGS((char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end));
Bram Moolenaar33570922005-01-25 22:26:29 +0000768static int eval_isnamec __ARGS((int c));
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000769static int eval_isnamec1 __ARGS((int c));
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000770static int get_var_tv __ARGS((char_u *name, int len, typval_T *rettv, int verbose));
771static int handle_subscript __ARGS((char_u **arg, typval_T *rettv, int evaluate, int verbose));
Bram Moolenaar33570922005-01-25 22:26:29 +0000772static typval_T *alloc_tv __ARGS((void));
773static typval_T *alloc_string_tv __ARGS((char_u *string));
Bram Moolenaar33570922005-01-25 22:26:29 +0000774static void init_tv __ARGS((typval_T *varp));
775static long get_tv_number __ARGS((typval_T *varp));
776static linenr_T get_tv_lnum __ARGS((typval_T *argvars));
Bram Moolenaar661b1822005-07-28 22:36:45 +0000777static linenr_T get_tv_lnum_buf __ARGS((typval_T *argvars, buf_T *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000778static char_u *get_tv_string __ARGS((typval_T *varp));
779static char_u *get_tv_string_buf __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +0000780static char_u *get_tv_string_buf_chk __ARGS((typval_T *varp, char_u *buf));
Bram Moolenaar33570922005-01-25 22:26:29 +0000781static dictitem_T *find_var __ARGS((char_u *name, hashtab_T **htp));
Bram Moolenaar332ac062013-04-15 13:06:21 +0200782static dictitem_T *find_var_in_ht __ARGS((hashtab_T *ht, int htname, char_u *varname, int writing));
Bram Moolenaar33570922005-01-25 22:26:29 +0000783static hashtab_T *find_var_ht __ARGS((char_u *name, char_u **varname));
784static void vars_clear_ext __ARGS((hashtab_T *ht, int free_val));
785static void delete_var __ARGS((hashtab_T *ht, hashitem_T *hi));
Bram Moolenaar7d61a922007-08-30 09:12:23 +0000786static void list_one_var __ARGS((dictitem_T *v, char_u *prefix, int *first));
787static void list_one_var_a __ARGS((char_u *prefix, char_u *name, int type, char_u *string, int *first));
Bram Moolenaar33570922005-01-25 22:26:29 +0000788static void set_var __ARGS((char_u *name, typval_T *varp, int copy));
789static int var_check_ro __ARGS((int flags, char_u *name));
Bram Moolenaar4e957af2006-09-02 11:41:07 +0000790static int var_check_fixed __ARGS((int flags, char_u *name));
Bram Moolenaar4228bec2011-03-27 16:03:15 +0200791static int var_check_func_name __ARGS((char_u *name, int new_var));
792static int valid_varname __ARGS((char_u *varname));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +0000793static int tv_check_lock __ARGS((int lock, char_u *name));
Bram Moolenaar81bf7082005-02-12 14:31:42 +0000794static int item_copy __ARGS((typval_T *from, typval_T *to, int deep, int copyID));
Bram Moolenaar33570922005-01-25 22:26:29 +0000795static char_u *find_option_end __ARGS((char_u **arg, int *opt_flags));
796static char_u *trans_function_name __ARGS((char_u **pp, int skip, int flags, funcdict_T *fd));
797static int eval_fname_script __ARGS((char_u *p));
798static int eval_fname_sid __ARGS((char_u *p));
799static void list_func_head __ARGS((ufunc_T *fp, int indent));
Bram Moolenaar33570922005-01-25 22:26:29 +0000800static ufunc_T *find_func __ARGS((char_u *name));
801static int function_exists __ARGS((char_u *name));
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +0000802static int builtin_function __ARGS((char_u *name));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000803#ifdef FEAT_PROFILE
804static void func_do_profile __ARGS((ufunc_T *fp));
Bram Moolenaar73830342005-02-28 22:48:19 +0000805static void prof_sort_list __ARGS((FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self));
806static void prof_func_line __ARGS((FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self));
807static int
808# ifdef __BORLANDC__
809 _RTLENTRYF
810# endif
811 prof_total_cmp __ARGS((const void *s1, const void *s2));
812static int
813# ifdef __BORLANDC__
814 _RTLENTRYF
815# endif
816 prof_self_cmp __ARGS((const void *s1, const void *s2));
Bram Moolenaar05159a02005-02-26 23:04:13 +0000817#endif
Bram Moolenaar018acca2013-05-30 13:37:28 +0200818static int script_autoload __ARGS((char_u *name, int reload));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000819static char_u *autoload_name __ARGS((char_u *name));
Bram Moolenaara40058a2005-07-11 22:42:07 +0000820static void cat_func_name __ARGS((char_u *buf, ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000821static void func_free __ARGS((ufunc_T *fp));
Bram Moolenaar33570922005-01-25 22:26:29 +0000822static void call_user_func __ARGS((ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict));
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000823static int can_free_funccal __ARGS((funccall_T *fc, int copyID)) ;
824static void free_funccal __ARGS((funccall_T *fc, int free_val));
Bram Moolenaar33570922005-01-25 22:26:29 +0000825static void add_nr_var __ARGS((dict_T *dp, dictitem_T *v, char *name, varnumber_T nr));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000826static win_T *find_win_by_nr __ARGS((typval_T *vp, tabpage_T *tp));
827static void getwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000828static int searchpair_cmn __ARGS((typval_T *argvars, pos_T *match_pos));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000829static int search_cmn __ARGS((typval_T *argvars, pos_T *match_pos, int *flagsp));
Bram Moolenaar99ebf042006-04-15 20:28:54 +0000830static void setwinvar __ARGS((typval_T *argvars, typval_T *rettv, int off));
Bram Moolenaar33570922005-01-25 22:26:29 +0000831
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200832
833#ifdef EBCDIC
834static int compare_func_name __ARGS((const void *s1, const void *s2));
835static void sortFunctions __ARGS(());
836#endif
837
Bram Moolenaar33570922005-01-25 22:26:29 +0000838/*
839 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000840 */
841 void
842eval_init()
843{
Bram Moolenaar33570922005-01-25 22:26:29 +0000844 int i;
845 struct vimvar *p;
846
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200847 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
848 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200849 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000850 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000851 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000852
853 for (i = 0; i < VV_LEN; ++i)
854 {
855 p = &vimvars[i];
856 STRCPY(p->vv_di.di_key, p->vv_name);
857 if (p->vv_flags & VV_RO)
858 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
859 else if (p->vv_flags & VV_RO_SBX)
860 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
861 else
862 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000863
864 /* add to v: scope dict, unless the value is not always available */
865 if (p->vv_type != VAR_UNKNOWN)
866 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000867 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000868 /* add to compat scope dict */
869 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000870 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000871 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200872 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200873
874#ifdef EBCDIC
875 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100876 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200877 */
878 sortFunctions();
879#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000880}
881
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000882#if defined(EXITFREE) || defined(PROTO)
883 void
884eval_clear()
885{
886 int i;
887 struct vimvar *p;
888
889 for (i = 0; i < VV_LEN; ++i)
890 {
891 p = &vimvars[i];
892 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000893 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000894 vim_free(p->vv_str);
895 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000896 }
897 else if (p->vv_di.di_tv.v_type == VAR_LIST)
898 {
899 list_unref(p->vv_list);
900 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000901 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000902 }
903 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000904 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000905 hash_clear(&compat_hashtab);
906
Bram Moolenaard9fba312005-06-26 22:34:35 +0000907 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100908# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200909 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100910# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000911
912 /* global variables */
913 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000914
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000915 /* autoloaded script names */
916 ga_clear_strings(&ga_loaded);
917
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200918 /* script-local variables */
919 for (i = 1; i <= ga_scripts.ga_len; ++i)
920 {
921 vars_clear(&SCRIPT_VARS(i));
922 vim_free(SCRIPT_SV(i));
923 }
924 ga_clear(&ga_scripts);
925
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000926 /* unreferenced lists and dicts */
927 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000928
929 /* functions */
930 free_all_functions();
931 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000932}
933#endif
934
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 * Return the name of the executed function.
937 */
938 char_u *
939func_name(cookie)
940 void *cookie;
941{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000942 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943}
944
945/*
946 * Return the address holding the next breakpoint line for a funccall cookie.
947 */
948 linenr_T *
949func_breakpoint(cookie)
950 void *cookie;
951{
Bram Moolenaar33570922005-01-25 22:26:29 +0000952 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953}
954
955/*
956 * Return the address holding the debug tick for a funccall cookie.
957 */
958 int *
959func_dbg_tick(cookie)
960 void *cookie;
961{
Bram Moolenaar33570922005-01-25 22:26:29 +0000962 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963}
964
965/*
966 * Return the nesting level for a funccall cookie.
967 */
968 int
969func_level(cookie)
970 void *cookie;
971{
Bram Moolenaar33570922005-01-25 22:26:29 +0000972 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973}
974
975/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +0000976funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +0000978/* pointer to list of previously used funccal, still around because some
979 * item in it is still being used. */
980funccall_T *previous_funccal = NULL;
981
Bram Moolenaar071d4272004-06-13 20:20:40 +0000982/*
983 * Return TRUE when a function was ended by a ":return" command.
984 */
985 int
986current_func_returned()
987{
988 return current_funccal->returned;
989}
990
991
992/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 * Set an internal variable to a string value. Creates the variable if it does
994 * not already exist.
995 */
996 void
997set_internal_string_var(name, value)
998 char_u *name;
999 char_u *value;
1000{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001001 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001002 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001003
1004 val = vim_strsave(value);
1005 if (val != NULL)
1006 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001007 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001008 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001010 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001011 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 }
1013 }
1014}
1015
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001016static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001017static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001018static char_u *redir_endp = NULL;
1019static char_u *redir_varname = NULL;
1020
1021/*
1022 * Start recording command output to a variable
1023 * Returns OK if successfully completed the setup. FAIL otherwise.
1024 */
1025 int
1026var_redir_start(name, append)
1027 char_u *name;
1028 int append; /* append to an existing variable */
1029{
1030 int save_emsg;
1031 int err;
1032 typval_T tv;
1033
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001034 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001035 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001036 {
1037 EMSG(_(e_invarg));
1038 return FAIL;
1039 }
1040
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001041 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001042 redir_varname = vim_strsave(name);
1043 if (redir_varname == NULL)
1044 return FAIL;
1045
1046 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1047 if (redir_lval == NULL)
1048 {
1049 var_redir_stop();
1050 return FAIL;
1051 }
1052
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001053 /* The output is stored in growarray "redir_ga" until redirection ends. */
1054 ga_init2(&redir_ga, (int)sizeof(char), 500);
1055
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001056 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001057 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, FALSE,
1058 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001059 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1060 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001061 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001062 if (redir_endp != NULL && *redir_endp != NUL)
1063 /* Trailing characters are present after the variable name */
1064 EMSG(_(e_trailing));
1065 else
1066 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001067 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001068 var_redir_stop();
1069 return FAIL;
1070 }
1071
1072 /* check if we can write to the variable: set it to or append an empty
1073 * string */
1074 save_emsg = did_emsg;
1075 did_emsg = FALSE;
1076 tv.v_type = VAR_STRING;
1077 tv.vval.v_string = (char_u *)"";
1078 if (append)
1079 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1080 else
1081 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001082 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001083 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001084 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001085 if (err)
1086 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001087 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001088 var_redir_stop();
1089 return FAIL;
1090 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001091
1092 return OK;
1093}
1094
1095/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096 * Append "value[value_len]" to the variable set by var_redir_start().
1097 * The actual appending is postponed until redirection ends, because the value
1098 * appended may in fact be the string we write to, changing it may cause freed
1099 * memory to be used:
1100 * :redir => foo
1101 * :let foo
1102 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 */
1104 void
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001105var_redir_str(value, value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 char_u *value;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001107 int value_len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001109 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110
1111 if (redir_lval == NULL)
1112 return;
1113
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001114 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001115 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001117 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001118
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001119 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001120 {
1121 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001122 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001123 }
1124 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001125 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126}
1127
1128/*
1129 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001130 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001131 */
1132 void
1133var_redir_stop()
1134{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001135 typval_T tv;
1136
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_lval != NULL)
1138 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001139 /* If there was no error: assign the text to the variable. */
1140 if (redir_endp != NULL)
1141 {
1142 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1143 tv.v_type = VAR_STRING;
1144 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001145 /* Call get_lval() again, if it's inside a Dict or List it may
1146 * have changed. */
1147 redir_endp = get_lval(redir_varname, NULL, redir_lval,
1148 FALSE, FALSE, FALSE, FNE_CHECK_START);
1149 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1150 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1151 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001153
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001154 /* free the collected output */
1155 vim_free(redir_ga.ga_data);
1156 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001157
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001158 vim_free(redir_lval);
1159 redir_lval = NULL;
1160 }
1161 vim_free(redir_varname);
1162 redir_varname = NULL;
1163}
1164
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165# if defined(FEAT_MBYTE) || defined(PROTO)
1166 int
1167eval_charconvert(enc_from, enc_to, fname_from, fname_to)
1168 char_u *enc_from;
1169 char_u *enc_to;
1170 char_u *fname_from;
1171 char_u *fname_to;
1172{
1173 int err = FALSE;
1174
1175 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1176 set_vim_var_string(VV_CC_TO, enc_to, -1);
1177 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1178 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1179 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1180 err = TRUE;
1181 set_vim_var_string(VV_CC_FROM, NULL, -1);
1182 set_vim_var_string(VV_CC_TO, NULL, -1);
1183 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1184 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1185
1186 if (err)
1187 return FAIL;
1188 return OK;
1189}
1190# endif
1191
1192# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1193 int
1194eval_printexpr(fname, args)
1195 char_u *fname;
1196 char_u *args;
1197{
1198 int err = FALSE;
1199
1200 set_vim_var_string(VV_FNAME_IN, fname, -1);
1201 set_vim_var_string(VV_CMDARG, args, -1);
1202 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1203 err = TRUE;
1204 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1205 set_vim_var_string(VV_CMDARG, NULL, -1);
1206
1207 if (err)
1208 {
1209 mch_remove(fname);
1210 return FAIL;
1211 }
1212 return OK;
1213}
1214# endif
1215
1216# if defined(FEAT_DIFF) || defined(PROTO)
1217 void
1218eval_diff(origfile, newfile, outfile)
1219 char_u *origfile;
1220 char_u *newfile;
1221 char_u *outfile;
1222{
1223 int err = FALSE;
1224
1225 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1226 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1227 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1228 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1229 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1230 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1231 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1232}
1233
1234 void
1235eval_patch(origfile, difffile, outfile)
1236 char_u *origfile;
1237 char_u *difffile;
1238 char_u *outfile;
1239{
1240 int err;
1241
1242 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1243 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1244 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1245 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1246 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1247 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1248 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1249}
1250# endif
1251
1252/*
1253 * Top level evaluation function, returning a boolean.
1254 * Sets "error" to TRUE if there was an error.
1255 * Return TRUE or FALSE.
1256 */
1257 int
1258eval_to_bool(arg, error, nextcmd, skip)
1259 char_u *arg;
1260 int *error;
1261 char_u **nextcmd;
1262 int skip; /* only parse, don't execute */
1263{
Bram Moolenaar33570922005-01-25 22:26:29 +00001264 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 int retval = FALSE;
1266
1267 if (skip)
1268 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001269 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271 else
1272 {
1273 *error = FALSE;
1274 if (!skip)
1275 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001276 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001277 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 }
1279 }
1280 if (skip)
1281 --emsg_skip;
1282
1283 return retval;
1284}
1285
1286/*
1287 * Top level evaluation function, returning a string. If "skip" is TRUE,
1288 * only parsing to "nextcmd" is done, without reporting errors. Return
1289 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1290 */
1291 char_u *
1292eval_to_string_skip(arg, nextcmd, skip)
1293 char_u *arg;
1294 char_u **nextcmd;
1295 int skip; /* only parse, don't execute */
1296{
Bram Moolenaar33570922005-01-25 22:26:29 +00001297 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 char_u *retval;
1299
1300 if (skip)
1301 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001302 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 retval = NULL;
1304 else
1305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001306 retval = vim_strsave(get_tv_string(&tv));
1307 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 }
1309 if (skip)
1310 --emsg_skip;
1311
1312 return retval;
1313}
1314
1315/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001316 * Skip over an expression at "*pp".
1317 * Return FAIL for an error, OK otherwise.
1318 */
1319 int
1320skip_expr(pp)
1321 char_u **pp;
1322{
Bram Moolenaar33570922005-01-25 22:26:29 +00001323 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001324
1325 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001326 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001327}
1328
1329/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001331 * When "convert" is TRUE convert a List into a sequence of lines and convert
1332 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001333 * Return pointer to allocated memory, or NULL for failure.
1334 */
1335 char_u *
Bram Moolenaara85fb752008-09-07 11:55:43 +00001336eval_to_string(arg, nextcmd, convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 char_u *arg;
1338 char_u **nextcmd;
Bram Moolenaara85fb752008-09-07 11:55:43 +00001339 int convert;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001343 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001344#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001345 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001348 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 retval = NULL;
1350 else
1351 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001352 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001353 {
1354 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001355 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001356 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001357 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001358 if (tv.vval.v_list->lv_len > 0)
1359 ga_append(&ga, NL);
1360 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001361 ga_append(&ga, NUL);
1362 retval = (char_u *)ga.ga_data;
1363 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001364#ifdef FEAT_FLOAT
1365 else if (convert && tv.v_type == VAR_FLOAT)
1366 {
1367 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1368 retval = vim_strsave(numbuf);
1369 }
1370#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001371 else
1372 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001373 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 }
1375
1376 return retval;
1377}
1378
1379/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001380 * Call eval_to_string() without using current local variables and using
1381 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 */
1383 char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001384eval_to_string_safe(arg, nextcmd, use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 char_u *arg;
1386 char_u **nextcmd;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001387 int use_sandbox;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388{
1389 char_u *retval;
1390 void *save_funccalp;
1391
1392 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001393 if (use_sandbox)
1394 ++sandbox;
1395 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001396 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001397 if (use_sandbox)
1398 --sandbox;
1399 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 restore_funccal(save_funccalp);
1401 return retval;
1402}
1403
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404/*
1405 * Top level evaluation function, returning a number.
1406 * Evaluates "expr" silently.
1407 * Returns -1 for an error.
1408 */
1409 int
1410eval_to_number(expr)
1411 char_u *expr;
1412{
Bram Moolenaar33570922005-01-25 22:26:29 +00001413 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001415 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416
1417 ++emsg_off;
1418
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001419 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 retval = -1;
1421 else
1422 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001423 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 }
1426 --emsg_off;
1427
1428 return retval;
1429}
1430
Bram Moolenaara40058a2005-07-11 22:42:07 +00001431/*
1432 * Prepare v: variable "idx" to be used.
1433 * Save the current typeval in "save_tv".
1434 * When not used yet add the variable to the v: hashtable.
1435 */
1436 static void
1437prepare_vimvar(idx, save_tv)
1438 int idx;
1439 typval_T *save_tv;
1440{
1441 *save_tv = vimvars[idx].vv_tv;
1442 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1443 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1444}
1445
1446/*
1447 * Restore v: variable "idx" to typeval "save_tv".
1448 * When no longer defined, remove the variable from the v: hashtable.
1449 */
1450 static void
1451restore_vimvar(idx, save_tv)
1452 int idx;
1453 typval_T *save_tv;
1454{
1455 hashitem_T *hi;
1456
Bram Moolenaara40058a2005-07-11 22:42:07 +00001457 vimvars[idx].vv_tv = *save_tv;
1458 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1459 {
1460 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1461 if (HASHITEM_EMPTY(hi))
1462 EMSG2(_(e_intern2), "restore_vimvar()");
1463 else
1464 hash_remove(&vimvarht, hi);
1465 }
1466}
1467
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001468#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001469/*
1470 * Evaluate an expression to a list with suggestions.
1471 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001472 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001473 */
1474 list_T *
1475eval_spell_expr(badword, expr)
1476 char_u *badword;
1477 char_u *expr;
1478{
1479 typval_T save_val;
1480 typval_T rettv;
1481 list_T *list = NULL;
1482 char_u *p = skipwhite(expr);
1483
1484 /* Set "v:val" to the bad word. */
1485 prepare_vimvar(VV_VAL, &save_val);
1486 vimvars[VV_VAL].vv_type = VAR_STRING;
1487 vimvars[VV_VAL].vv_str = badword;
1488 if (p_verbose == 0)
1489 ++emsg_off;
1490
1491 if (eval1(&p, &rettv, TRUE) == OK)
1492 {
1493 if (rettv.v_type != VAR_LIST)
1494 clear_tv(&rettv);
1495 else
1496 list = rettv.vval.v_list;
1497 }
1498
1499 if (p_verbose == 0)
1500 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001501 restore_vimvar(VV_VAL, &save_val);
1502
1503 return list;
1504}
1505
1506/*
1507 * "list" is supposed to contain two items: a word and a number. Return the
1508 * word in "pp" and the number as the return value.
1509 * Return -1 if anything isn't right.
1510 * Used to get the good word and score from the eval_spell_expr() result.
1511 */
1512 int
1513get_spellword(list, pp)
1514 list_T *list;
1515 char_u **pp;
1516{
1517 listitem_T *li;
1518
1519 li = list->lv_first;
1520 if (li == NULL)
1521 return -1;
1522 *pp = get_tv_string(&li->li_tv);
1523
1524 li = li->li_next;
1525 if (li == NULL)
1526 return -1;
1527 return get_tv_number(&li->li_tv);
1528}
1529#endif
1530
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001531/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001532 * Top level evaluation function.
1533 * Returns an allocated typval_T with the result.
1534 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001535 */
1536 typval_T *
1537eval_expr(arg, nextcmd)
1538 char_u *arg;
1539 char_u **nextcmd;
1540{
1541 typval_T *tv;
1542
1543 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001544 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001545 {
1546 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001547 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001548 }
1549
1550 return tv;
1551}
1552
1553
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001555 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001556 * Uses argv[argc] for the function arguments. Only Number and String
1557 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001558 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001560 int
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001561call_vim_function(func, argc, argv, safe, str_arg_only, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001562 char_u *func;
1563 int argc;
1564 char_u **argv;
1565 int safe; /* use the sandbox */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001566 int str_arg_only; /* all arguments are strings */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001567 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568{
Bram Moolenaar33570922005-01-25 22:26:29 +00001569 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 long n;
1571 int len;
1572 int i;
1573 int doesrange;
1574 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001575 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001577 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001579 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001580
1581 for (i = 0; i < argc; i++)
1582 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001583 /* Pass a NULL or empty argument as an empty string */
1584 if (argv[i] == NULL || *argv[i] == NUL)
1585 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001586 argvars[i].v_type = VAR_STRING;
1587 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001588 continue;
1589 }
1590
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001591 if (str_arg_only)
1592 len = 0;
1593 else
1594 /* Recognize a number argument, the others must be strings. */
1595 vim_str2nr(argv[i], NULL, &len, TRUE, TRUE, &n, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (len != 0 && len == (int)STRLEN(argv[i]))
1597 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001598 argvars[i].v_type = VAR_NUMBER;
1599 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 else
1602 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001603 argvars[i].v_type = VAR_STRING;
1604 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606 }
1607
1608 if (safe)
1609 {
1610 save_funccalp = save_funccal();
1611 ++sandbox;
1612 }
1613
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001614 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1615 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 if (safe)
1619 {
1620 --sandbox;
1621 restore_funccal(save_funccalp);
1622 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 vim_free(argvars);
1624
1625 if (ret == FAIL)
1626 clear_tv(rettv);
1627
1628 return ret;
1629}
1630
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001631/*
1632 * Call vimL function "func" and return the result as a number.
1633 * Returns -1 when calling the function fails.
1634 * Uses argv[argc] for the function arguments.
1635 */
1636 long
1637call_func_retnr(func, argc, argv, safe)
1638 char_u *func;
1639 int argc;
1640 char_u **argv;
1641 int safe; /* use the sandbox */
1642{
1643 typval_T rettv;
1644 long retval;
1645
1646 /* All arguments are passed as strings, no conversion to number. */
1647 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1648 return -1;
1649
1650 retval = get_tv_number_chk(&rettv, NULL);
1651 clear_tv(&rettv);
1652 return retval;
1653}
1654
1655#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1656 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1657
Bram Moolenaar4f688582007-07-24 12:34:30 +00001658# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001659/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001660 * Call vimL function "func" and return the result as a string.
1661 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001662 * Uses argv[argc] for the function arguments.
1663 */
1664 void *
1665call_func_retstr(func, argc, argv, safe)
1666 char_u *func;
1667 int argc;
1668 char_u **argv;
1669 int safe; /* use the sandbox */
1670{
1671 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001672 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001673
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001674 /* All arguments are passed as strings, no conversion to number. */
1675 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 return NULL;
1677
1678 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 return retval;
1681}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001682# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001684/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001685 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001686 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001687 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688 */
1689 void *
1690call_func_retlist(func, argc, argv, safe)
1691 char_u *func;
1692 int argc;
1693 char_u **argv;
1694 int safe; /* use the sandbox */
1695{
1696 typval_T rettv;
1697
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001698 /* All arguments are passed as strings, no conversion to number. */
1699 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001700 return NULL;
1701
1702 if (rettv.v_type != VAR_LIST)
1703 {
1704 clear_tv(&rettv);
1705 return NULL;
1706 }
1707
1708 return rettv.vval.v_list;
1709}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710#endif
1711
1712/*
1713 * Save the current function call pointer, and set it to NULL.
1714 * Used when executing autocommands and for ":source".
1715 */
1716 void *
1717save_funccal()
1718{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001719 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 current_funccal = NULL;
1722 return (void *)fc;
1723}
1724
1725 void
Bram Moolenaar05159a02005-02-26 23:04:13 +00001726restore_funccal(vfc)
1727 void *vfc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001729 funccall_T *fc = (funccall_T *)vfc;
1730
1731 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732}
1733
Bram Moolenaar05159a02005-02-26 23:04:13 +00001734#if defined(FEAT_PROFILE) || defined(PROTO)
1735/*
1736 * Prepare profiling for entering a child or something else that is not
1737 * counted for the script/function itself.
1738 * Should always be called in pair with prof_child_exit().
1739 */
1740 void
1741prof_child_enter(tm)
1742 proftime_T *tm; /* place to store waittime */
1743{
1744 funccall_T *fc = current_funccal;
1745
1746 if (fc != NULL && fc->func->uf_profiling)
1747 profile_start(&fc->prof_child);
1748 script_prof_save(tm);
1749}
1750
1751/*
1752 * Take care of time spent in a child.
1753 * Should always be called after prof_child_enter().
1754 */
1755 void
1756prof_child_exit(tm)
1757 proftime_T *tm; /* where waittime was stored */
1758{
1759 funccall_T *fc = current_funccal;
1760
1761 if (fc != NULL && fc->func->uf_profiling)
1762 {
1763 profile_end(&fc->prof_child);
1764 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1765 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1766 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1767 }
1768 script_prof_restore(tm);
1769}
1770#endif
1771
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773#ifdef FEAT_FOLDING
1774/*
1775 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1776 * it in "*cp". Doesn't give error messages.
1777 */
1778 int
1779eval_foldexpr(arg, cp)
1780 char_u *arg;
1781 int *cp;
1782{
Bram Moolenaar33570922005-01-25 22:26:29 +00001783 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 int retval;
1785 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001786 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1787 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
1789 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001790 if (use_sandbox)
1791 ++sandbox;
1792 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001794 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 retval = 0;
1796 else
1797 {
1798 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001799 if (tv.v_type == VAR_NUMBER)
1800 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001801 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 retval = 0;
1803 else
1804 {
1805 /* If the result is a string, check if there is a non-digit before
1806 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001807 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 if (!VIM_ISDIGIT(*s) && *s != '-')
1809 *cp = *s++;
1810 retval = atol((char *)s);
1811 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001812 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 }
1814 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001815 if (use_sandbox)
1816 --sandbox;
1817 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818
1819 return retval;
1820}
1821#endif
1822
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001824 * ":let" list all variable values
1825 * ":let var1 var2" list variable values
1826 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001827 * ":let var += expr" assignment command.
1828 * ":let var -= expr" assignment command.
1829 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001830 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 */
1832 void
1833ex_let(eap)
1834 exarg_T *eap;
1835{
1836 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001837 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001838 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001840 int var_count = 0;
1841 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001842 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001843 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001844 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845
Bram Moolenaardb552d602006-03-23 22:59:57 +00001846 argend = skip_var_list(arg, &var_count, &semicolon);
1847 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001848 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001849 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1850 --argend;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001851 expr = vim_strchr(argend, '=');
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001852 if (expr == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001854 /*
1855 * ":let" without "=": list variables
1856 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001857 if (*arg == '[')
1858 EMSG(_(e_invarg));
1859 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001861 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001862 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001863 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001865 list_glob_vars(&first);
1866 list_buf_vars(&first);
1867 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001868#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001869 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001870#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001871 list_script_vars(&first);
1872 list_func_vars(&first);
1873 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 eap->nextcmd = check_nextcmd(arg);
1876 }
1877 else
1878 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001879 op[0] = '=';
1880 op[1] = NUL;
Bram Moolenaardb552d602006-03-23 22:59:57 +00001881 if (expr > argend)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001882 {
1883 if (vim_strchr((char_u *)"+-.", expr[-1]) != NULL)
1884 op[0] = expr[-1]; /* +=, -= or .= */
1885 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001886 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 if (eap->skip)
1889 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001890 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001891 if (eap->skip)
1892 {
1893 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001894 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 --emsg_skip;
1896 }
1897 else if (i != FAIL)
1898 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001899 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001902 }
1903 }
1904}
1905
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906/*
1907 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1908 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001909 * When "nextchars" is not NULL it points to a string with characters that
1910 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1911 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 * Returns OK or FAIL;
1913 */
1914 static int
1915ex_let_vars(arg_start, tv, copy, semicolon, var_count, nextchars)
1916 char_u *arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001917 typval_T *tv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001918 int copy; /* copy values from "tv", don't move */
1919 int semicolon; /* from skip_var_list() */
1920 int var_count; /* from skip_var_list() */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001921 char_u *nextchars;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922{
1923 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001924 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001925 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001926 listitem_T *item;
1927 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001928
1929 if (*arg != '[')
1930 {
1931 /*
1932 * ":let var = expr" or ":for var in list"
1933 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001934 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001935 return FAIL;
1936 return OK;
1937 }
1938
1939 /*
1940 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
1941 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00001942 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001943 {
1944 EMSG(_(e_listreq));
1945 return FAIL;
1946 }
1947
1948 i = list_len(l);
1949 if (semicolon == 0 && var_count < i)
1950 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001951 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001952 return FAIL;
1953 }
1954 if (var_count - semicolon > i)
1955 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00001956 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 return FAIL;
1958 }
1959
1960 item = l->lv_first;
1961 while (*arg != ']')
1962 {
1963 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001965 item = item->li_next;
1966 if (arg == NULL)
1967 return FAIL;
1968
1969 arg = skipwhite(arg);
1970 if (*arg == ';')
1971 {
1972 /* Put the rest of the list (may be empty) in the var after ';'.
1973 * Create a new list for this. */
1974 l = list_alloc();
1975 if (l == NULL)
1976 return FAIL;
1977 while (item != NULL)
1978 {
1979 list_append_tv(l, &item->li_tv);
1980 item = item->li_next;
1981 }
1982
1983 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001984 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001985 ltv.vval.v_list = l;
1986 l->lv_refcount = 1;
1987
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001988 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
1989 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990 clear_tv(&ltv);
1991 if (arg == NULL)
1992 return FAIL;
1993 break;
1994 }
1995 else if (*arg != ',' && *arg != ']')
1996 {
1997 EMSG2(_(e_intern2), "ex_let_vars()");
1998 return FAIL;
1999 }
2000 }
2001
2002 return OK;
2003}
2004
2005/*
2006 * Skip over assignable variable "var" or list of variables "[var, var]".
2007 * Used for ":let varvar = expr" and ":for varvar in expr".
2008 * For "[var, var]" increment "*var_count" for each variable.
2009 * for "[var, var; var]" set "semicolon".
2010 * Return NULL for an error.
2011 */
2012 static char_u *
2013skip_var_list(arg, var_count, semicolon)
2014 char_u *arg;
2015 int *var_count;
2016 int *semicolon;
2017{
2018 char_u *p, *s;
2019
2020 if (*arg == '[')
2021 {
2022 /* "[var, var]": find the matching ']'. */
2023 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002024 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 {
2026 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2027 s = skip_var_one(p);
2028 if (s == p)
2029 {
2030 EMSG2(_(e_invarg2), p);
2031 return NULL;
2032 }
2033 ++*var_count;
2034
2035 p = skipwhite(s);
2036 if (*p == ']')
2037 break;
2038 else if (*p == ';')
2039 {
2040 if (*semicolon == 1)
2041 {
2042 EMSG(_("Double ; in list of variables"));
2043 return NULL;
2044 }
2045 *semicolon = 1;
2046 }
2047 else if (*p != ',')
2048 {
2049 EMSG2(_(e_invarg2), p);
2050 return NULL;
2051 }
2052 }
2053 return p + 1;
2054 }
2055 else
2056 return skip_var_one(arg);
2057}
2058
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002059/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002060 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002061 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002062 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002063 static char_u *
2064skip_var_one(arg)
2065 char_u *arg;
2066{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002067 if (*arg == '@' && arg[1] != NUL)
2068 return arg + 2;
2069 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2070 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002071}
2072
Bram Moolenaara7043832005-01-21 11:56:39 +00002073/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002074 * List variables for hashtab "ht" with prefix "prefix".
2075 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002077 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002078list_hashtable_vars(ht, prefix, empty, first)
Bram Moolenaar33570922005-01-25 22:26:29 +00002079 hashtab_T *ht;
Bram Moolenaara7043832005-01-21 11:56:39 +00002080 char_u *prefix;
Bram Moolenaar33570922005-01-25 22:26:29 +00002081 int empty;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002082 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002083{
Bram Moolenaar33570922005-01-25 22:26:29 +00002084 hashitem_T *hi;
2085 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002086 int todo;
2087
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002088 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002089 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2090 {
2091 if (!HASHITEM_EMPTY(hi))
2092 {
2093 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002094 di = HI2DI(hi);
2095 if (empty || di->di_tv.v_type != VAR_STRING
2096 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002097 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002098 }
2099 }
2100}
2101
2102/*
2103 * List global variables.
2104 */
2105 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002106list_glob_vars(first)
2107 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002108{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002109 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002110}
2111
2112/*
2113 * List buffer variables.
2114 */
2115 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002116list_buf_vars(first)
2117 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002118{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002119 char_u numbuf[NUMBUFLEN];
2120
Bram Moolenaar429fa852013-04-15 12:27:36 +02002121 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002122 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123
2124 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002125 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2126 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002127}
2128
2129/*
2130 * List window variables.
2131 */
2132 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002133list_win_vars(first)
2134 int *first;
Bram Moolenaara7043832005-01-21 11:56:39 +00002135{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002136 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002137 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002138}
2139
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002140#ifdef FEAT_WINDOWS
2141/*
2142 * List tab page variables.
2143 */
2144 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002145list_tab_vars(first)
2146 int *first;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002147{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002148 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002149 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002150}
2151#endif
2152
Bram Moolenaara7043832005-01-21 11:56:39 +00002153/*
2154 * List Vim variables.
2155 */
2156 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157list_vim_vars(first)
2158 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002159{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002161}
2162
2163/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002164 * List script-local variables, if there is a script.
2165 */
2166 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002167list_script_vars(first)
2168 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002169{
2170 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2172 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002173}
2174
2175/*
2176 * List function variables, if there is a function.
2177 */
2178 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002179list_func_vars(first)
2180 int *first;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002181{
2182 if (current_funccal != NULL)
2183 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002184 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002185}
2186
2187/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002188 * List variables in "arg".
2189 */
2190 static char_u *
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191list_arg_vars(eap, arg, first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002192 exarg_T *eap;
2193 char_u *arg;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 int *first;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002195{
2196 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002197 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002198 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002199 char_u *name_start;
2200 char_u *arg_subsc;
2201 char_u *tofree;
2202 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002203
2204 while (!ends_excmd(*arg) && !got_int)
2205 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002206 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002207 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002208 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002209 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2210 {
2211 emsg_severe = TRUE;
2212 EMSG(_(e_trailing));
2213 break;
2214 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002215 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002216 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002218 /* get_name_len() takes care of expanding curly braces */
2219 name_start = name = arg;
2220 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2221 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002223 /* This is mainly to keep test 49 working: when expanding
2224 * curly braces fails overrule the exception error message. */
2225 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002226 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002227 emsg_severe = TRUE;
2228 EMSG2(_(e_invarg2), arg);
2229 break;
2230 }
2231 error = TRUE;
2232 }
2233 else
2234 {
2235 if (tofree != NULL)
2236 name = tofree;
2237 if (get_var_tv(name, len, &tv, TRUE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002238 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002239 else
2240 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002241 /* handle d.key, l[idx], f(expr) */
2242 arg_subsc = arg;
2243 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002244 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002246 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002248 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002250 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002251 case 'g': list_glob_vars(first); break;
2252 case 'b': list_buf_vars(first); break;
2253 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002254#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002255 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002256#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002257 case 'v': list_vim_vars(first); break;
2258 case 's': list_script_vars(first); break;
2259 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 default:
2261 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002262 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002263 }
2264 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 {
2266 char_u numbuf[NUMBUFLEN];
2267 char_u *tf;
2268 int c;
2269 char_u *s;
2270
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002271 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 c = *arg;
2273 *arg = NUL;
2274 list_one_var_a((char_u *)"",
2275 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002276 tv.v_type,
2277 s == NULL ? (char_u *)"" : s,
2278 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 *arg = c;
2280 vim_free(tf);
2281 }
2282 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002283 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 }
2285 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286
2287 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002289
2290 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 }
2292
2293 return arg;
2294}
2295
2296/*
2297 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2298 * Returns a pointer to the char just after the var name.
2299 * Returns NULL if there is an error.
2300 */
2301 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002302ex_let_one(arg, tv, copy, endchars, op)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 char_u *arg; /* points to variable name */
Bram Moolenaar33570922005-01-25 22:26:29 +00002304 typval_T *tv; /* value to assign to variable */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002305 int copy; /* copy value from "tv" */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002306 char_u *endchars; /* valid chars after variable name or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002307 char_u *op; /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308{
2309 int c1;
2310 char_u *name;
2311 char_u *p;
2312 char_u *arg_end = NULL;
2313 int len;
2314 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002315 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002316
2317 /*
2318 * ":let $VAR = expr": Set environment variable.
2319 */
2320 if (*arg == '$')
2321 {
2322 /* Find the end of the name. */
2323 ++arg;
2324 name = arg;
2325 len = get_env_len(&arg);
2326 if (len == 0)
2327 EMSG2(_(e_invarg2), name - 1);
2328 else
2329 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002330 if (op != NULL && (*op == '+' || *op == '-'))
2331 EMSG2(_(e_letwrong), op);
2332 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002333 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002335 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002336 {
2337 c1 = name[len];
2338 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002339 p = get_tv_string_chk(tv);
2340 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002341 {
2342 int mustfree = FALSE;
2343 char_u *s = vim_getenv(name, &mustfree);
2344
2345 if (s != NULL)
2346 {
2347 p = tofree = concat_str(s, p);
2348 if (mustfree)
2349 vim_free(s);
2350 }
2351 }
2352 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002353 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002354 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002355 if (STRICMP(name, "HOME") == 0)
2356 init_homedir();
2357 else if (didset_vim && STRICMP(name, "VIM") == 0)
2358 didset_vim = FALSE;
2359 else if (didset_vimruntime
2360 && STRICMP(name, "VIMRUNTIME") == 0)
2361 didset_vimruntime = FALSE;
2362 arg_end = arg;
2363 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366 }
2367 }
2368 }
2369
2370 /*
2371 * ":let &option = expr": Set option value.
2372 * ":let &l:option = expr": Set local option value.
2373 * ":let &g:option = expr": Set global option value.
2374 */
2375 else if (*arg == '&')
2376 {
2377 /* Find the end of the name. */
2378 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002379 if (p == NULL || (endchars != NULL
2380 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002381 EMSG(_(e_letunexp));
2382 else
2383 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002384 long n;
2385 int opt_type;
2386 long numval;
2387 char_u *stringval = NULL;
2388 char_u *s;
2389
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 c1 = *p;
2391 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002392
2393 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002394 s = get_tv_string_chk(tv); /* != NULL if number or string */
2395 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002396 {
2397 opt_type = get_option_value(arg, &numval,
2398 &stringval, opt_flags);
2399 if ((opt_type == 1 && *op == '.')
2400 || (opt_type == 0 && *op != '.'))
2401 EMSG2(_(e_letwrong), op);
2402 else
2403 {
2404 if (opt_type == 1) /* number */
2405 {
2406 if (*op == '+')
2407 n = numval + n;
2408 else
2409 n = numval - n;
2410 }
2411 else if (opt_type == 0 && stringval != NULL) /* string */
2412 {
2413 s = concat_str(stringval, s);
2414 vim_free(stringval);
2415 stringval = s;
2416 }
2417 }
2418 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002419 if (s != NULL)
2420 {
2421 set_option_value(arg, n, s, opt_flags);
2422 arg_end = p;
2423 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002425 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002426 }
2427 }
2428
2429 /*
2430 * ":let @r = expr": Set register contents.
2431 */
2432 else if (*arg == '@')
2433 {
2434 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002435 if (op != NULL && (*op == '+' || *op == '-'))
2436 EMSG2(_(e_letwrong), op);
2437 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002438 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 EMSG(_(e_letunexp));
2440 else
2441 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002442 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002443 char_u *s;
2444
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002445 p = get_tv_string_chk(tv);
2446 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 {
Bram Moolenaar92124a32005-06-17 22:03:40 +00002448 s = get_reg_contents(*arg == '@' ? '"' : *arg, TRUE, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 if (s != NULL)
2450 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002451 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 vim_free(s);
2453 }
2454 }
2455 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002456 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002457 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002458 arg_end = arg + 1;
2459 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002460 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002461 }
2462 }
2463
2464 /*
2465 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002466 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002467 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002468 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002469 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002470 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002471
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002472 p = get_lval(arg, tv, &lv, FALSE, FALSE, FALSE, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002473 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002475 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2476 EMSG(_(e_letunexp));
2477 else
2478 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002479 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002480 arg_end = p;
2481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002483 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485
2486 else
2487 EMSG2(_(e_invarg2), arg);
2488
2489 return arg_end;
2490}
2491
2492/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002493 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2494 */
2495 static int
2496check_changedtick(arg)
2497 char_u *arg;
2498{
2499 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2500 {
2501 EMSG2(_(e_readonlyvar), arg);
2502 return TRUE;
2503 }
2504 return FALSE;
2505}
2506
2507/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002508 * Get an lval: variable, Dict item or List item that can be assigned a value
2509 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2510 * "name.key", "name.key[expr]" etc.
2511 * Indexing only works if "name" is an existing List or Dictionary.
2512 * "name" points to the start of the name.
2513 * If "rettv" is not NULL it points to the value to be assigned.
2514 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2515 * wrong; must end in space or cmd separator.
2516 *
2517 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002518 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 */
2521 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002522get_lval(name, rettv, lp, unlet, skip, quiet, fne_flags)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +00002524 typval_T *rettv;
2525 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 int unlet;
2527 int skip;
2528 int quiet; /* don't give error messages */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002529 int fne_flags; /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 char_u *expr_start, *expr_end;
2533 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002534 dictitem_T *v;
2535 typval_T var1;
2536 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002538 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002539 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002540 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002541 hashtab_T *ht;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002544 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002545
2546 if (skip)
2547 {
2548 /* When skipping just find the end of the name. */
2549 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002550 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002551 }
2552
2553 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002554 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002555 if (expr_start != NULL)
2556 {
2557 /* Don't expand the name when we already know there is an error. */
2558 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2559 && *p != '[' && *p != '.')
2560 {
2561 EMSG(_(e_trailing));
2562 return NULL;
2563 }
2564
2565 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2566 if (lp->ll_exp_name == NULL)
2567 {
2568 /* Report an invalid expression in braces, unless the
2569 * expression evaluation has been cancelled due to an
2570 * aborting error, an interrupt, or an exception. */
2571 if (!aborting() && !quiet)
2572 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002573 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002574 EMSG2(_(e_invarg2), name);
2575 return NULL;
2576 }
2577 }
2578 lp->ll_name = lp->ll_exp_name;
2579 }
2580 else
2581 lp->ll_name = name;
2582
2583 /* Without [idx] or .key we are done. */
2584 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2585 return p;
2586
2587 cc = *p;
2588 *p = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00002589 v = find_var(lp->ll_name, &ht);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 if (v == NULL && !quiet)
2591 EMSG2(_(e_undefvar), lp->ll_name);
2592 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002593 if (v == NULL)
2594 return NULL;
2595
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 /*
2597 * Loop until no more [idx] or .key is following.
2598 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002599 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002601 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2603 && !(lp->ll_tv->v_type == VAR_DICT
2604 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606 if (!quiet)
2607 EMSG(_("E689: Can only index a List or Dictionary"));
2608 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002611 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (!quiet)
2613 EMSG(_("E708: [:] must come last"));
2614 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002615 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002616
Bram Moolenaar8c711452005-01-14 21:53:12 +00002617 len = -1;
2618 if (*p == '.')
2619 {
2620 key = p + 1;
2621 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2622 ;
2623 if (len == 0)
2624 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002625 if (!quiet)
2626 EMSG(_(e_emptykey));
2627 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002628 }
2629 p = key + len;
2630 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002631 else
2632 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002633 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002634 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002635 if (*p == ':')
2636 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002637 else
2638 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002639 empty1 = FALSE;
2640 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002642 if (get_tv_string_chk(&var1) == NULL)
2643 {
2644 /* not a number or string */
2645 clear_tv(&var1);
2646 return NULL;
2647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002648 }
2649
2650 /* Optionally get the second index [ :expr]. */
2651 if (*p == ':')
2652 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002654 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002655 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002656 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002657 if (!empty1)
2658 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002660 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 if (rettv != NULL && (rettv->v_type != VAR_LIST
2662 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!quiet)
2665 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002666 if (!empty1)
2667 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002669 }
2670 p = skipwhite(p + 1);
2671 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002673 else
2674 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002675 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002676 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2677 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 if (!empty1)
2679 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002680 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002681 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002682 if (get_tv_string_chk(&var2) == NULL)
2683 {
2684 /* not a number or string */
2685 if (!empty1)
2686 clear_tv(&var1);
2687 clear_tv(&var2);
2688 return NULL;
2689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002691 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002694 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 if (!quiet)
2699 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 if (!empty1)
2701 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002703 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002704 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
2706
2707 /* Skip to past ']'. */
2708 ++p;
2709 }
2710
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002711 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002712 {
2713 if (len == -1)
2714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002716 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 if (*key == NUL)
2718 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 if (!quiet)
2720 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
2724 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002725 lp->ll_list = NULL;
2726 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002727 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002728
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002729 /* When assigning to a scope dictionary check that a function and
2730 * variable name is valid (only variable name unless it is l: or
2731 * g: dictionary). Disallow overwriting a builtin function. */
2732 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002733 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002734 int prevval;
2735 int wrong;
2736
2737 if (len != -1)
2738 {
2739 prevval = key[len];
2740 key[len] = NUL;
2741 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002742 else
2743 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002744 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2745 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002746 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002747 || !valid_varname(key);
2748 if (len != -1)
2749 key[len] = prevval;
2750 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002751 return NULL;
2752 }
2753
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002756 /* Can't add "v:" variable. */
2757 if (lp->ll_dict == &vimvardict)
2758 {
2759 EMSG2(_(e_illvar), name);
2760 return NULL;
2761 }
2762
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002763 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002767 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002768 if (len == -1)
2769 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002770 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 }
2772 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002776 if (len == -1)
2777 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 p = NULL;
2780 break;
2781 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782 /* existing variable, need to check if it can be changed */
2783 else if (var_check_ro(lp->ll_di->di_flags, name))
2784 return NULL;
2785
Bram Moolenaar8c711452005-01-14 21:53:12 +00002786 if (len == -1)
2787 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002788 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002789 }
2790 else
2791 {
2792 /*
2793 * Get the number and item for the only or first index of the List.
2794 */
2795 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002796 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002797 else
2798 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002799 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002800 clear_tv(&var1);
2801 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002802 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002803 lp->ll_list = lp->ll_tv->vval.v_list;
2804 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2805 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002806 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002807 if (lp->ll_n1 < 0)
2808 {
2809 lp->ll_n1 = 0;
2810 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2811 }
2812 }
2813 if (lp->ll_li == NULL)
2814 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002817 if (!quiet)
2818 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002819 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002820 }
2821
2822 /*
2823 * May need to find the item or absolute index for the second
2824 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 * When no index given: "lp->ll_empty2" is TRUE.
2826 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002830 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002836 {
2837 if (!quiet)
2838 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002840 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002841 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002842 }
2843
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002844 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2845 if (lp->ll_n1 < 0)
2846 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2847 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002848 {
2849 if (!quiet)
2850 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002851 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002852 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002853 }
2854
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002856 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002857 }
2858
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002859 return p;
2860}
2861
2862/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002863 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 */
2865 static void
2866clear_lval(lp)
Bram Moolenaar33570922005-01-25 22:26:29 +00002867 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002868{
2869 vim_free(lp->ll_exp_name);
2870 vim_free(lp->ll_newkey);
2871}
2872
2873/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002874 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002876 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 */
2878 static void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002879set_var_lval(lp, endp, rettv, copy, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00002880 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 char_u *endp;
Bram Moolenaar33570922005-01-25 22:26:29 +00002882 typval_T *rettv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002883 int copy;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002884 char_u *op;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885{
2886 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002887 listitem_T *ri;
2888 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889
2890 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002891 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002893 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 cc = *endp;
2895 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002896 if (op != NULL && *op != '=')
2897 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002898 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002899
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002900 /* handle +=, -= and .= */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002901 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002902 &tv, TRUE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002903 {
2904 if (tv_op(&tv, rettv, op) == OK)
2905 set_var(lp->ll_name, &tv, FALSE);
2906 clear_tv(&tv);
2907 }
2908 }
2909 else
2910 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002912 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002914 else if (tv_check_lock(lp->ll_newkey == NULL
2915 ? lp->ll_tv->v_lock
2916 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name))
2917 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 else if (lp->ll_range)
2919 {
2920 /*
2921 * Assign the List values to the list items.
2922 */
2923 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002924 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002925 if (op != NULL && *op != '=')
2926 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
2927 else
2928 {
2929 clear_tv(&lp->ll_li->li_tv);
2930 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
2931 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002932 ri = ri->li_next;
2933 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
2934 break;
2935 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002936 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00002938 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002939 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002940 ri = NULL;
2941 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002942 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002943 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944 lp->ll_li = lp->ll_li->li_next;
2945 ++lp->ll_n1;
2946 }
2947 if (ri != NULL)
2948 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 else if (lp->ll_empty2
2950 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 : lp->ll_n1 != lp->ll_n2)
2952 EMSG(_("E711: List value has not enough items"));
2953 }
2954 else
2955 {
2956 /*
2957 * Assign to a List or Dictionary item.
2958 */
2959 if (lp->ll_newkey != NULL)
2960 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002961 if (op != NULL && *op != '=')
2962 {
2963 EMSG2(_(e_letwrong), op);
2964 return;
2965 }
2966
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002967 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002968 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 if (di == NULL)
2970 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002971 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
2972 {
2973 vim_free(di);
2974 return;
2975 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002977 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002978 else if (op != NULL && *op != '=')
2979 {
2980 tv_op(lp->ll_tv, rettv, op);
2981 return;
2982 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002985
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002986 /*
2987 * Assign the value to the variable or list item.
2988 */
2989 if (copy)
2990 copy_tv(rettv, lp->ll_tv);
2991 else
2992 {
2993 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00002994 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002996 }
2997 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002998}
2999
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003000/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003001 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3002 * Returns OK or FAIL.
3003 */
3004 static int
3005tv_op(tv1, tv2, op)
Bram Moolenaar33570922005-01-25 22:26:29 +00003006 typval_T *tv1;
3007 typval_T *tv2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003008 char_u *op;
3009{
3010 long n;
3011 char_u numbuf[NUMBUFLEN];
3012 char_u *s;
3013
3014 /* Can't do anything with a Funcref or a Dict on the right. */
3015 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT)
3016 {
3017 switch (tv1->v_type)
3018 {
3019 case VAR_DICT:
3020 case VAR_FUNC:
3021 break;
3022
3023 case VAR_LIST:
3024 if (*op != '+' || tv2->v_type != VAR_LIST)
3025 break;
3026 /* List += List */
3027 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3028 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3029 return OK;
3030
3031 case VAR_NUMBER:
3032 case VAR_STRING:
3033 if (tv2->v_type == VAR_LIST)
3034 break;
3035 if (*op == '+' || *op == '-')
3036 {
3037 /* nr += nr or nr -= nr*/
3038 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003039#ifdef FEAT_FLOAT
3040 if (tv2->v_type == VAR_FLOAT)
3041 {
3042 float_T f = n;
3043
3044 if (*op == '+')
3045 f += tv2->vval.v_float;
3046 else
3047 f -= tv2->vval.v_float;
3048 clear_tv(tv1);
3049 tv1->v_type = VAR_FLOAT;
3050 tv1->vval.v_float = f;
3051 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003052 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003053#endif
3054 {
3055 if (*op == '+')
3056 n += get_tv_number(tv2);
3057 else
3058 n -= get_tv_number(tv2);
3059 clear_tv(tv1);
3060 tv1->v_type = VAR_NUMBER;
3061 tv1->vval.v_number = n;
3062 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003063 }
3064 else
3065 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003066 if (tv2->v_type == VAR_FLOAT)
3067 break;
3068
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003069 /* str .= str */
3070 s = get_tv_string(tv1);
3071 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3072 clear_tv(tv1);
3073 tv1->v_type = VAR_STRING;
3074 tv1->vval.v_string = s;
3075 }
3076 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003077
3078#ifdef FEAT_FLOAT
3079 case VAR_FLOAT:
3080 {
3081 float_T f;
3082
3083 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3084 && tv2->v_type != VAR_NUMBER
3085 && tv2->v_type != VAR_STRING))
3086 break;
3087 if (tv2->v_type == VAR_FLOAT)
3088 f = tv2->vval.v_float;
3089 else
3090 f = get_tv_number(tv2);
3091 if (*op == '+')
3092 tv1->vval.v_float += f;
3093 else
3094 tv1->vval.v_float -= f;
3095 }
3096 return OK;
3097#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003098 }
3099 }
3100
3101 EMSG2(_(e_letwrong), op);
3102 return FAIL;
3103}
3104
3105/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003106 * Add a watcher to a list.
3107 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003108 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003109list_add_watch(l, lw)
Bram Moolenaar33570922005-01-25 22:26:29 +00003110 list_T *l;
3111 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003112{
3113 lw->lw_next = l->lv_watch;
3114 l->lv_watch = lw;
3115}
3116
3117/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003118 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003119 * No warning when it isn't found...
3120 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003121 void
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003122list_rem_watch(l, lwrem)
Bram Moolenaar33570922005-01-25 22:26:29 +00003123 list_T *l;
3124 listwatch_T *lwrem;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003125{
Bram Moolenaar33570922005-01-25 22:26:29 +00003126 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003127
3128 lwp = &l->lv_watch;
3129 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3130 {
3131 if (lw == lwrem)
3132 {
3133 *lwp = lw->lw_next;
3134 break;
3135 }
3136 lwp = &lw->lw_next;
3137 }
3138}
3139
3140/*
3141 * Just before removing an item from a list: advance watchers to the next
3142 * item.
3143 */
3144 static void
3145list_fix_watch(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00003146 list_T *l;
3147 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003148{
Bram Moolenaar33570922005-01-25 22:26:29 +00003149 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003150
3151 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3152 if (lw->lw_item == item)
3153 lw->lw_item = item->li_next;
3154}
3155
3156/*
3157 * Evaluate the expression used in a ":for var in expr" command.
3158 * "arg" points to "var".
3159 * Set "*errp" to TRUE for an error, FALSE otherwise;
3160 * Return a pointer that holds the info. Null when there is an error.
3161 */
3162 void *
3163eval_for_line(arg, errp, nextcmdp, skip)
3164 char_u *arg;
3165 int *errp;
3166 char_u **nextcmdp;
3167 int skip;
3168{
Bram Moolenaar33570922005-01-25 22:26:29 +00003169 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003170 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003171 typval_T tv;
3172 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003173
3174 *errp = TRUE; /* default: there is an error */
3175
Bram Moolenaar33570922005-01-25 22:26:29 +00003176 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003177 if (fi == NULL)
3178 return NULL;
3179
3180 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3181 if (expr == NULL)
3182 return fi;
3183
3184 expr = skipwhite(expr);
3185 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3186 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003187 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003188 return fi;
3189 }
3190
3191 if (skip)
3192 ++emsg_skip;
3193 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3194 {
3195 *errp = FALSE;
3196 if (!skip)
3197 {
3198 l = tv.vval.v_list;
3199 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003200 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003202 clear_tv(&tv);
3203 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204 else
3205 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003206 /* No need to increment the refcount, it's already set for the
3207 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 fi->fi_list = l;
3209 list_add_watch(l, &fi->fi_lw);
3210 fi->fi_lw.lw_item = l->lv_first;
3211 }
3212 }
3213 }
3214 if (skip)
3215 --emsg_skip;
3216
3217 return fi;
3218}
3219
3220/*
3221 * Use the first item in a ":for" list. Advance to the next.
3222 * Assign the values to the variable (list). "arg" points to the first one.
3223 * Return TRUE when a valid item was found, FALSE when at end of list or
3224 * something wrong.
3225 */
3226 int
3227next_for_item(fi_void, arg)
3228 void *fi_void;
3229 char_u *arg;
3230{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003231 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003232 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003233 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003234
3235 item = fi->fi_lw.lw_item;
3236 if (item == NULL)
3237 result = FALSE;
3238 else
3239 {
3240 fi->fi_lw.lw_item = item->li_next;
3241 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3242 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3243 }
3244 return result;
3245}
3246
3247/*
3248 * Free the structure used to store info used by ":for".
3249 */
3250 void
3251free_for_info(fi_void)
3252 void *fi_void;
3253{
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003256 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003257 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003259 list_unref(fi->fi_list);
3260 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003261 vim_free(fi);
3262}
3263
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3265
3266 void
3267set_context_for_expression(xp, arg, cmdidx)
3268 expand_T *xp;
3269 char_u *arg;
3270 cmdidx_T cmdidx;
3271{
3272 int got_eq = FALSE;
3273 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003274 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 if (cmdidx == CMD_let)
3277 {
3278 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003279 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 {
3281 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003282 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 {
3284 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003285 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 if (vim_iswhite(*p))
3287 break;
3288 }
3289 return;
3290 }
3291 }
3292 else
3293 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3294 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295 while ((xp->xp_pattern = vim_strpbrk(arg,
3296 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3297 {
3298 c = *xp->xp_pattern;
3299 if (c == '&')
3300 {
3301 c = xp->xp_pattern[1];
3302 if (c == '&')
3303 {
3304 ++xp->xp_pattern;
3305 xp->xp_context = cmdidx != CMD_let || got_eq
3306 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3307 }
3308 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003311 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3312 xp->xp_pattern += 2;
3313
3314 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315 }
3316 else if (c == '$')
3317 {
3318 /* environment variable */
3319 xp->xp_context = EXPAND_ENV_VARS;
3320 }
3321 else if (c == '=')
3322 {
3323 got_eq = TRUE;
3324 xp->xp_context = EXPAND_EXPRESSION;
3325 }
3326 else if (c == '<'
3327 && xp->xp_context == EXPAND_FUNCTIONS
3328 && vim_strchr(xp->xp_pattern, '(') == NULL)
3329 {
3330 /* Function name can start with "<SNR>" */
3331 break;
3332 }
3333 else if (cmdidx != CMD_let || got_eq)
3334 {
3335 if (c == '"') /* string */
3336 {
3337 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3338 if (c == '\\' && xp->xp_pattern[1] != NUL)
3339 ++xp->xp_pattern;
3340 xp->xp_context = EXPAND_NOTHING;
3341 }
3342 else if (c == '\'') /* literal string */
3343 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003344 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3346 /* skip */ ;
3347 xp->xp_context = EXPAND_NOTHING;
3348 }
3349 else if (c == '|')
3350 {
3351 if (xp->xp_pattern[1] == '|')
3352 {
3353 ++xp->xp_pattern;
3354 xp->xp_context = EXPAND_EXPRESSION;
3355 }
3356 else
3357 xp->xp_context = EXPAND_COMMANDS;
3358 }
3359 else
3360 xp->xp_context = EXPAND_EXPRESSION;
3361 }
3362 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003363 /* Doesn't look like something valid, expand as an expression
3364 * anyway. */
3365 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 arg = xp->xp_pattern;
3367 if (*arg != NUL)
3368 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3369 /* skip */ ;
3370 }
3371 xp->xp_pattern = arg;
3372}
3373
3374#endif /* FEAT_CMDL_COMPL */
3375
3376/*
3377 * ":1,25call func(arg1, arg2)" function call.
3378 */
3379 void
3380ex_call(eap)
3381 exarg_T *eap;
3382{
3383 char_u *arg = eap->arg;
3384 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003386 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003388 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 linenr_T lnum;
3390 int doesrange;
3391 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003392 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003394 if (eap->skip)
3395 {
3396 /* trans_function_name() doesn't work well when skipping, use eval0()
3397 * instead to skip to any following command, e.g. for:
3398 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003399 ++emsg_skip;
3400 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3401 clear_tv(&rettv);
3402 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003403 return;
3404 }
3405
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003406 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003407 if (fudi.fd_newkey != NULL)
3408 {
3409 /* Still need to give an error message for missing key. */
3410 EMSG2(_(e_dictkey), fudi.fd_newkey);
3411 vim_free(fudi.fd_newkey);
3412 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003413 if (tofree == NULL)
3414 return;
3415
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003416 /* Increase refcount on dictionary, it could get deleted when evaluating
3417 * the arguments. */
3418 if (fudi.fd_dict != NULL)
3419 ++fudi.fd_dict->dv_refcount;
3420
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003421 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003422 len = (int)STRLEN(tofree);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003423 name = deref_func_name(tofree, &len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424
Bram Moolenaar532c7802005-01-27 14:44:31 +00003425 /* Skip white space to allow ":call func ()". Not good, but required for
3426 * backward compatibility. */
3427 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003428 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003429
3430 if (*startarg != '(')
3431 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003432 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 goto end;
3434 }
3435
3436 /*
3437 * When skipping, evaluate the function once, to find the end of the
3438 * arguments.
3439 * When the function takes a range, this is discovered after the first
3440 * call, and the loop is broken.
3441 */
3442 if (eap->skip)
3443 {
3444 ++emsg_skip;
3445 lnum = eap->line2; /* do it once, also with an invalid range */
3446 }
3447 else
3448 lnum = eap->line1;
3449 for ( ; lnum <= eap->line2; ++lnum)
3450 {
3451 if (!eap->skip && eap->addr_count > 0)
3452 {
3453 curwin->w_cursor.lnum = lnum;
3454 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003455#ifdef FEAT_VIRTUALEDIT
3456 curwin->w_cursor.coladd = 0;
3457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 }
3459 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003460 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003461 eap->line1, eap->line2, &doesrange,
3462 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 {
3464 failed = TRUE;
3465 break;
3466 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003467
3468 /* Handle a function returning a Funcref, Dictionary or List. */
3469 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3470 {
3471 failed = TRUE;
3472 break;
3473 }
3474
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003475 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 if (doesrange || eap->skip)
3477 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003478
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003480 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003481 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003482 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003483 if (aborting())
3484 break;
3485 }
3486 if (eap->skip)
3487 --emsg_skip;
3488
3489 if (!failed)
3490 {
3491 /* Check for trailing illegal characters and a following command. */
3492 if (!ends_excmd(*arg))
3493 {
3494 emsg_severe = TRUE;
3495 EMSG(_(e_trailing));
3496 }
3497 else
3498 eap->nextcmd = check_nextcmd(arg);
3499 }
3500
3501end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003502 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003503 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504}
3505
3506/*
3507 * ":unlet[!] var1 ... " command.
3508 */
3509 void
3510ex_unlet(eap)
3511 exarg_T *eap;
3512{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003513 ex_unletlock(eap, eap->arg, 0);
3514}
3515
3516/*
3517 * ":lockvar" and ":unlockvar" commands
3518 */
3519 void
3520ex_lockvar(eap)
3521 exarg_T *eap;
3522{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003524 int deep = 2;
3525
3526 if (eap->forceit)
3527 deep = -1;
3528 else if (vim_isdigit(*arg))
3529 {
3530 deep = getdigits(&arg);
3531 arg = skipwhite(arg);
3532 }
3533
3534 ex_unletlock(eap, arg, deep);
3535}
3536
3537/*
3538 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3539 */
3540 static void
3541ex_unletlock(eap, argstart, deep)
3542 exarg_T *eap;
3543 char_u *argstart;
3544 int deep;
3545{
3546 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003547 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003549 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550
3551 do
3552 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003553 /* Parse the name and find the end. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003554 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, FALSE,
3555 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003556 if (lv.ll_name == NULL)
3557 error = TRUE; /* error but continue parsing */
3558 if (name_end == NULL || (!vim_iswhite(*name_end)
3559 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003561 if (name_end != NULL)
3562 {
3563 emsg_severe = TRUE;
3564 EMSG(_(e_trailing));
3565 }
3566 if (!(eap->skip || error))
3567 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 break;
3569 }
3570
3571 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003572 {
3573 if (eap->cmdidx == CMD_unlet)
3574 {
3575 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3576 error = TRUE;
3577 }
3578 else
3579 {
3580 if (do_lock_var(&lv, name_end, deep,
3581 eap->cmdidx == CMD_lockvar) == FAIL)
3582 error = TRUE;
3583 }
3584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003586 if (!eap->skip)
3587 clear_lval(&lv);
3588
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 arg = skipwhite(name_end);
3590 } while (!ends_excmd(*arg));
3591
3592 eap->nextcmd = check_nextcmd(arg);
3593}
3594
Bram Moolenaar8c711452005-01-14 21:53:12 +00003595 static int
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003596do_unlet_var(lp, name_end, forceit)
Bram Moolenaar33570922005-01-25 22:26:29 +00003597 lval_T *lp;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003598 char_u *name_end;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003599 int forceit;
3600{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003601 int ret = OK;
3602 int cc;
3603
3604 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003605 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003606 cc = *name_end;
3607 *name_end = NUL;
3608
3609 /* Normal name or expanded name. */
3610 if (check_changedtick(lp->ll_name))
3611 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003612 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003613 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003614 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003615 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616 else if (tv_check_lock(lp->ll_tv->v_lock, lp->ll_name))
3617 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 else if (lp->ll_range)
3619 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003620 listitem_T *li;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621
3622 /* Delete a range of List items. */
3623 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3624 {
3625 li = lp->ll_li->li_next;
3626 listitem_remove(lp->ll_list, lp->ll_li);
3627 lp->ll_li = li;
3628 ++lp->ll_n1;
3629 }
3630 }
3631 else
3632 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003633 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 /* unlet a List item. */
3635 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003638 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 }
3640
3641 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003642}
3643
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644/*
3645 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003646 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 */
3648 int
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003649do_unlet(name, forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 char_u *name;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003651 int forceit;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652{
Bram Moolenaar33570922005-01-25 22:26:29 +00003653 hashtab_T *ht;
3654 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003655 char_u *varname;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003656 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
Bram Moolenaar33570922005-01-25 22:26:29 +00003658 ht = find_var_ht(name, &varname);
3659 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003661 hi = hash_find(ht, varname);
3662 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003663 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003664 di = HI2DI(hi);
3665 if (var_check_fixed(di->di_flags, name)
3666 || var_check_ro(di->di_flags, name))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003667 return FAIL;
3668 delete_var(ht, hi);
3669 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003670 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003672 if (forceit)
3673 return OK;
3674 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 return FAIL;
3676}
3677
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003678/*
3679 * Lock or unlock variable indicated by "lp".
3680 * "deep" is the levels to go (-1 for unlimited);
3681 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3682 */
3683 static int
3684do_lock_var(lp, name_end, deep, lock)
3685 lval_T *lp;
3686 char_u *name_end;
3687 int deep;
3688 int lock;
3689{
3690 int ret = OK;
3691 int cc;
3692 dictitem_T *di;
3693
3694 if (deep == 0) /* nothing to do */
3695 return OK;
3696
3697 if (lp->ll_tv == NULL)
3698 {
3699 cc = *name_end;
3700 *name_end = NUL;
3701
3702 /* Normal name or expanded name. */
3703 if (check_changedtick(lp->ll_name))
3704 ret = FAIL;
3705 else
3706 {
3707 di = find_var(lp->ll_name, NULL);
3708 if (di == NULL)
3709 ret = FAIL;
3710 else
3711 {
3712 if (lock)
3713 di->di_flags |= DI_FLAGS_LOCK;
3714 else
3715 di->di_flags &= ~DI_FLAGS_LOCK;
3716 item_lock(&di->di_tv, deep, lock);
3717 }
3718 }
3719 *name_end = cc;
3720 }
3721 else if (lp->ll_range)
3722 {
3723 listitem_T *li = lp->ll_li;
3724
3725 /* (un)lock a range of List items. */
3726 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3727 {
3728 item_lock(&li->li_tv, deep, lock);
3729 li = li->li_next;
3730 ++lp->ll_n1;
3731 }
3732 }
3733 else if (lp->ll_list != NULL)
3734 /* (un)lock a List item. */
3735 item_lock(&lp->ll_li->li_tv, deep, lock);
3736 else
3737 /* un(lock) a Dictionary item. */
3738 item_lock(&lp->ll_di->di_tv, deep, lock);
3739
3740 return ret;
3741}
3742
3743/*
3744 * Lock or unlock an item. "deep" is nr of levels to go.
3745 */
3746 static void
3747item_lock(tv, deep, lock)
3748 typval_T *tv;
3749 int deep;
3750 int lock;
3751{
3752 static int recurse = 0;
3753 list_T *l;
3754 listitem_T *li;
3755 dict_T *d;
3756 hashitem_T *hi;
3757 int todo;
3758
3759 if (recurse >= DICT_MAXNEST)
3760 {
3761 EMSG(_("E743: variable nested too deep for (un)lock"));
3762 return;
3763 }
3764 if (deep == 0)
3765 return;
3766 ++recurse;
3767
3768 /* lock/unlock the item itself */
3769 if (lock)
3770 tv->v_lock |= VAR_LOCKED;
3771 else
3772 tv->v_lock &= ~VAR_LOCKED;
3773
3774 switch (tv->v_type)
3775 {
3776 case VAR_LIST:
3777 if ((l = tv->vval.v_list) != NULL)
3778 {
3779 if (lock)
3780 l->lv_lock |= VAR_LOCKED;
3781 else
3782 l->lv_lock &= ~VAR_LOCKED;
3783 if (deep < 0 || deep > 1)
3784 /* recursive: lock/unlock the items the List contains */
3785 for (li = l->lv_first; li != NULL; li = li->li_next)
3786 item_lock(&li->li_tv, deep - 1, lock);
3787 }
3788 break;
3789 case VAR_DICT:
3790 if ((d = tv->vval.v_dict) != NULL)
3791 {
3792 if (lock)
3793 d->dv_lock |= VAR_LOCKED;
3794 else
3795 d->dv_lock &= ~VAR_LOCKED;
3796 if (deep < 0 || deep > 1)
3797 {
3798 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003799 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003800 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3801 {
3802 if (!HASHITEM_EMPTY(hi))
3803 {
3804 --todo;
3805 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3806 }
3807 }
3808 }
3809 }
3810 }
3811 --recurse;
3812}
3813
Bram Moolenaara40058a2005-07-11 22:42:07 +00003814/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003815 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3816 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003817 */
3818 static int
3819tv_islocked(tv)
3820 typval_T *tv;
3821{
3822 return (tv->v_lock & VAR_LOCKED)
3823 || (tv->v_type == VAR_LIST
3824 && tv->vval.v_list != NULL
3825 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3826 || (tv->v_type == VAR_DICT
3827 && tv->vval.v_dict != NULL
3828 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3829}
3830
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3832/*
3833 * Delete all "menutrans_" variables.
3834 */
3835 void
3836del_menutrans_vars()
3837{
Bram Moolenaar33570922005-01-25 22:26:29 +00003838 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003839 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003840
Bram Moolenaar33570922005-01-25 22:26:29 +00003841 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003842 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003843 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003844 {
3845 if (!HASHITEM_EMPTY(hi))
3846 {
3847 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003848 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3849 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003850 }
3851 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003852 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853}
3854#endif
3855
3856#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3857
3858/*
3859 * Local string buffer for the next two functions to store a variable name
3860 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3861 * get_user_var_name().
3862 */
3863
3864static char_u *cat_prefix_varname __ARGS((int prefix, char_u *name));
3865
3866static char_u *varnamebuf = NULL;
3867static int varnamebuflen = 0;
3868
3869/*
3870 * Function to concatenate a prefix and a variable name.
3871 */
3872 static char_u *
3873cat_prefix_varname(prefix, name)
3874 int prefix;
3875 char_u *name;
3876{
3877 int len;
3878
3879 len = (int)STRLEN(name) + 3;
3880 if (len > varnamebuflen)
3881 {
3882 vim_free(varnamebuf);
3883 len += 10; /* some additional space */
3884 varnamebuf = alloc(len);
3885 if (varnamebuf == NULL)
3886 {
3887 varnamebuflen = 0;
3888 return NULL;
3889 }
3890 varnamebuflen = len;
3891 }
3892 *varnamebuf = prefix;
3893 varnamebuf[1] = ':';
3894 STRCPY(varnamebuf + 2, name);
3895 return varnamebuf;
3896}
3897
3898/*
3899 * Function given to ExpandGeneric() to obtain the list of user defined
3900 * (global/buffer/window/built-in) variable names.
3901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 char_u *
3903get_user_var_name(xp, idx)
3904 expand_T *xp;
3905 int idx;
3906{
Bram Moolenaar532c7802005-01-27 14:44:31 +00003907 static long_u gdone;
3908 static long_u bdone;
3909 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003910#ifdef FEAT_WINDOWS
3911 static long_u tdone;
3912#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00003913 static int vidx;
3914 static hashitem_T *hi;
3915 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
3917 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003918 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003919 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003920#ifdef FEAT_WINDOWS
3921 tdone = 0;
3922#endif
3923 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003924
3925 /* Global variables */
3926 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003928 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003929 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003930 else
3931 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003932 while (HASHITEM_EMPTY(hi))
3933 ++hi;
3934 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
3935 return cat_prefix_varname('g', hi->hi_key);
3936 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003938
3939 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003940 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003943 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003945 else
3946 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 while (HASHITEM_EMPTY(hi))
3948 ++hi;
3949 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 return (char_u *)"b:changedtick";
3955 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003956
3957 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003958 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00003961 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00003962 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00003963 else
3964 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 while (HASHITEM_EMPTY(hi))
3966 ++hi;
3967 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003969
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003970#ifdef FEAT_WINDOWS
3971 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02003972 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003973 if (tdone < ht->ht_used)
3974 {
3975 if (tdone++ == 0)
3976 hi = ht->ht_array;
3977 else
3978 ++hi;
3979 while (HASHITEM_EMPTY(hi))
3980 ++hi;
3981 return cat_prefix_varname('t', hi->hi_key);
3982 }
3983#endif
3984
Bram Moolenaar33570922005-01-25 22:26:29 +00003985 /* v: variables */
3986 if (vidx < VV_LEN)
3987 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988
3989 vim_free(varnamebuf);
3990 varnamebuf = NULL;
3991 varnamebuflen = 0;
3992 return NULL;
3993}
3994
3995#endif /* FEAT_CMDL_COMPL */
3996
3997/*
3998 * types for expressions.
3999 */
4000typedef enum
4001{
4002 TYPE_UNKNOWN = 0
4003 , TYPE_EQUAL /* == */
4004 , TYPE_NEQUAL /* != */
4005 , TYPE_GREATER /* > */
4006 , TYPE_GEQUAL /* >= */
4007 , TYPE_SMALLER /* < */
4008 , TYPE_SEQUAL /* <= */
4009 , TYPE_MATCH /* =~ */
4010 , TYPE_NOMATCH /* !~ */
4011} exptype_T;
4012
4013/*
4014 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004015 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4017 */
4018
4019/*
4020 * Handle zero level expression.
4021 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004022 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004023 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 * Return OK or FAIL.
4025 */
4026 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004027eval0(arg, rettv, nextcmd, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 char_u *arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004029 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 char_u **nextcmd;
4031 int evaluate;
4032{
4033 int ret;
4034 char_u *p;
4035
4036 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004037 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 if (ret == FAIL || !ends_excmd(*p))
4039 {
4040 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004041 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 /*
4043 * Report the invalid expression unless the expression evaluation has
4044 * been cancelled due to an aborting error, an interrupt, or an
4045 * exception.
4046 */
4047 if (!aborting())
4048 EMSG2(_(e_invexpr2), arg);
4049 ret = FAIL;
4050 }
4051 if (nextcmd != NULL)
4052 *nextcmd = check_nextcmd(p);
4053
4054 return ret;
4055}
4056
4057/*
4058 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004059 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 *
4061 * "arg" must point to the first non-white of the expression.
4062 * "arg" is advanced to the next non-white after the recognized expression.
4063 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004064 * Note: "rettv.v_lock" is not set.
4065 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 * Return OK or FAIL.
4067 */
4068 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004069eval1(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004071 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 int evaluate;
4073{
4074 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004075 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076
4077 /*
4078 * Get the first variable.
4079 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004080 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 return FAIL;
4082
4083 if ((*arg)[0] == '?')
4084 {
4085 result = FALSE;
4086 if (evaluate)
4087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004088 int error = FALSE;
4089
4090 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004092 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004093 if (error)
4094 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 }
4096
4097 /*
4098 * Get the second variable.
4099 */
4100 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004101 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 return FAIL;
4103
4104 /*
4105 * Check for the ":".
4106 */
4107 if ((*arg)[0] != ':')
4108 {
4109 EMSG(_("E109: Missing ':' after '?'"));
4110 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004111 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112 return FAIL;
4113 }
4114
4115 /*
4116 * Get the third variable.
4117 */
4118 *arg = skipwhite(*arg + 1);
4119 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4120 {
4121 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 return FAIL;
4124 }
4125 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004126 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 }
4128
4129 return OK;
4130}
4131
4132/*
4133 * Handle first level expression:
4134 * expr2 || expr2 || expr2 logical OR
4135 *
4136 * "arg" must point to the first non-white of the expression.
4137 * "arg" is advanced to the next non-white after the recognized expression.
4138 *
4139 * Return OK or FAIL.
4140 */
4141 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142eval2(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004144 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 int evaluate;
4146{
Bram Moolenaar33570922005-01-25 22:26:29 +00004147 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 long result;
4149 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004150 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151
4152 /*
4153 * Get the first variable.
4154 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 return FAIL;
4157
4158 /*
4159 * Repeat until there is no following "||".
4160 */
4161 first = TRUE;
4162 result = FALSE;
4163 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4164 {
4165 if (evaluate && first)
4166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004167 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004169 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004170 if (error)
4171 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 first = FALSE;
4173 }
4174
4175 /*
4176 * Get the second variable.
4177 */
4178 *arg = skipwhite(*arg + 2);
4179 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4180 return FAIL;
4181
4182 /*
4183 * Compute the result.
4184 */
4185 if (evaluate && !result)
4186 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004187 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004189 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 if (error)
4191 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 }
4193 if (evaluate)
4194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004195 rettv->v_type = VAR_NUMBER;
4196 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198 }
4199
4200 return OK;
4201}
4202
4203/*
4204 * Handle second level expression:
4205 * expr3 && expr3 && expr3 logical AND
4206 *
4207 * "arg" must point to the first non-white of the expression.
4208 * "arg" is advanced to the next non-white after the recognized expression.
4209 *
4210 * Return OK or FAIL.
4211 */
4212 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213eval3(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004215 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 int evaluate;
4217{
Bram Moolenaar33570922005-01-25 22:26:29 +00004218 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 long result;
4220 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004221 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223 /*
4224 * Get the first variable.
4225 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 return FAIL;
4228
4229 /*
4230 * Repeat until there is no following "&&".
4231 */
4232 first = TRUE;
4233 result = TRUE;
4234 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4235 {
4236 if (evaluate && first)
4237 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004238 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004240 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004241 if (error)
4242 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 first = FALSE;
4244 }
4245
4246 /*
4247 * Get the second variable.
4248 */
4249 *arg = skipwhite(*arg + 2);
4250 if (eval4(arg, &var2, evaluate && result) == FAIL)
4251 return FAIL;
4252
4253 /*
4254 * Compute the result.
4255 */
4256 if (evaluate && result)
4257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004261 if (error)
4262 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 }
4264 if (evaluate)
4265 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 rettv->v_type = VAR_NUMBER;
4267 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 }
4270
4271 return OK;
4272}
4273
4274/*
4275 * Handle third level expression:
4276 * var1 == var2
4277 * var1 =~ var2
4278 * var1 != var2
4279 * var1 !~ var2
4280 * var1 > var2
4281 * var1 >= var2
4282 * var1 < var2
4283 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004284 * var1 is var2
4285 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004286 *
4287 * "arg" must point to the first non-white of the expression.
4288 * "arg" is advanced to the next non-white after the recognized expression.
4289 *
4290 * Return OK or FAIL.
4291 */
4292 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004293eval4(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004295 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 int evaluate;
4297{
Bram Moolenaar33570922005-01-25 22:26:29 +00004298 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 char_u *p;
4300 int i;
4301 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004302 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 int len = 2;
4304 long n1, n2;
4305 char_u *s1, *s2;
4306 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4307 regmatch_T regmatch;
4308 int ic;
4309 char_u *save_cpo;
4310
4311 /*
4312 * Get the first variable.
4313 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004314 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 return FAIL;
4316
4317 p = *arg;
4318 switch (p[0])
4319 {
4320 case '=': if (p[1] == '=')
4321 type = TYPE_EQUAL;
4322 else if (p[1] == '~')
4323 type = TYPE_MATCH;
4324 break;
4325 case '!': if (p[1] == '=')
4326 type = TYPE_NEQUAL;
4327 else if (p[1] == '~')
4328 type = TYPE_NOMATCH;
4329 break;
4330 case '>': if (p[1] != '=')
4331 {
4332 type = TYPE_GREATER;
4333 len = 1;
4334 }
4335 else
4336 type = TYPE_GEQUAL;
4337 break;
4338 case '<': if (p[1] != '=')
4339 {
4340 type = TYPE_SMALLER;
4341 len = 1;
4342 }
4343 else
4344 type = TYPE_SEQUAL;
4345 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004346 case 'i': if (p[1] == 's')
4347 {
4348 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4349 len = 5;
4350 if (!vim_isIDc(p[len]))
4351 {
4352 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4353 type_is = TRUE;
4354 }
4355 }
4356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 }
4358
4359 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004360 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 */
4362 if (type != TYPE_UNKNOWN)
4363 {
4364 /* extra question mark appended: ignore case */
4365 if (p[len] == '?')
4366 {
4367 ic = TRUE;
4368 ++len;
4369 }
4370 /* extra '#' appended: match case */
4371 else if (p[len] == '#')
4372 {
4373 ic = FALSE;
4374 ++len;
4375 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004376 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 else
4378 ic = p_ic;
4379
4380 /*
4381 * Get the second variable.
4382 */
4383 *arg = skipwhite(p + len);
4384 if (eval5(arg, &var2, evaluate) == FAIL)
4385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004386 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 return FAIL;
4388 }
4389
4390 if (evaluate)
4391 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004392 if (type_is && rettv->v_type != var2.v_type)
4393 {
4394 /* For "is" a different type always means FALSE, for "notis"
4395 * it means TRUE. */
4396 n1 = (type == TYPE_NEQUAL);
4397 }
4398 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4399 {
4400 if (type_is)
4401 {
4402 n1 = (rettv->v_type == var2.v_type
4403 && rettv->vval.v_list == var2.vval.v_list);
4404 if (type == TYPE_NEQUAL)
4405 n1 = !n1;
4406 }
4407 else if (rettv->v_type != var2.v_type
4408 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4409 {
4410 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004411 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004412 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004413 EMSG(_("E692: Invalid operation for Lists"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004414 clear_tv(rettv);
4415 clear_tv(&var2);
4416 return FAIL;
4417 }
4418 else
4419 {
4420 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004421 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4422 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004423 if (type == TYPE_NEQUAL)
4424 n1 = !n1;
4425 }
4426 }
4427
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004428 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4429 {
4430 if (type_is)
4431 {
4432 n1 = (rettv->v_type == var2.v_type
4433 && rettv->vval.v_dict == var2.vval.v_dict);
4434 if (type == TYPE_NEQUAL)
4435 n1 = !n1;
4436 }
4437 else if (rettv->v_type != var2.v_type
4438 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4439 {
4440 if (rettv->v_type != var2.v_type)
4441 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4442 else
4443 EMSG(_("E736: Invalid operation for Dictionary"));
4444 clear_tv(rettv);
4445 clear_tv(&var2);
4446 return FAIL;
4447 }
4448 else
4449 {
4450 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004451 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4452 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004453 if (type == TYPE_NEQUAL)
4454 n1 = !n1;
4455 }
4456 }
4457
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004458 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4459 {
4460 if (rettv->v_type != var2.v_type
4461 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4462 {
4463 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004464 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004465 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004466 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004467 clear_tv(rettv);
4468 clear_tv(&var2);
4469 return FAIL;
4470 }
4471 else
4472 {
4473 /* Compare two Funcrefs for being equal or unequal. */
4474 if (rettv->vval.v_string == NULL
4475 || var2.vval.v_string == NULL)
4476 n1 = FALSE;
4477 else
4478 n1 = STRCMP(rettv->vval.v_string,
4479 var2.vval.v_string) == 0;
4480 if (type == TYPE_NEQUAL)
4481 n1 = !n1;
4482 }
4483 }
4484
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004485#ifdef FEAT_FLOAT
4486 /*
4487 * If one of the two variables is a float, compare as a float.
4488 * When using "=~" or "!~", always compare as string.
4489 */
4490 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4491 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4492 {
4493 float_T f1, f2;
4494
4495 if (rettv->v_type == VAR_FLOAT)
4496 f1 = rettv->vval.v_float;
4497 else
4498 f1 = get_tv_number(rettv);
4499 if (var2.v_type == VAR_FLOAT)
4500 f2 = var2.vval.v_float;
4501 else
4502 f2 = get_tv_number(&var2);
4503 n1 = FALSE;
4504 switch (type)
4505 {
4506 case TYPE_EQUAL: n1 = (f1 == f2); break;
4507 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4508 case TYPE_GREATER: n1 = (f1 > f2); break;
4509 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4510 case TYPE_SMALLER: n1 = (f1 < f2); break;
4511 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4512 case TYPE_UNKNOWN:
4513 case TYPE_MATCH:
4514 case TYPE_NOMATCH: break; /* avoid gcc warning */
4515 }
4516 }
4517#endif
4518
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 /*
4520 * If one of the two variables is a number, compare as a number.
4521 * When using "=~" or "!~", always compare as string.
4522 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004523 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4525 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004526 n1 = get_tv_number(rettv);
4527 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 switch (type)
4529 {
4530 case TYPE_EQUAL: n1 = (n1 == n2); break;
4531 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4532 case TYPE_GREATER: n1 = (n1 > n2); break;
4533 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4534 case TYPE_SMALLER: n1 = (n1 < n2); break;
4535 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4536 case TYPE_UNKNOWN:
4537 case TYPE_MATCH:
4538 case TYPE_NOMATCH: break; /* avoid gcc warning */
4539 }
4540 }
4541 else
4542 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004543 s1 = get_tv_string_buf(rettv, buf1);
4544 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4546 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4547 else
4548 i = 0;
4549 n1 = FALSE;
4550 switch (type)
4551 {
4552 case TYPE_EQUAL: n1 = (i == 0); break;
4553 case TYPE_NEQUAL: n1 = (i != 0); break;
4554 case TYPE_GREATER: n1 = (i > 0); break;
4555 case TYPE_GEQUAL: n1 = (i >= 0); break;
4556 case TYPE_SMALLER: n1 = (i < 0); break;
4557 case TYPE_SEQUAL: n1 = (i <= 0); break;
4558
4559 case TYPE_MATCH:
4560 case TYPE_NOMATCH:
4561 /* avoid 'l' flag in 'cpoptions' */
4562 save_cpo = p_cpo;
4563 p_cpo = (char_u *)"";
4564 regmatch.regprog = vim_regcomp(s2,
4565 RE_MAGIC + RE_STRING);
4566 regmatch.rm_ic = ic;
4567 if (regmatch.regprog != NULL)
4568 {
4569 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004570 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 if (type == TYPE_NOMATCH)
4572 n1 = !n1;
4573 }
4574 p_cpo = save_cpo;
4575 break;
4576
4577 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4578 }
4579 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004580 clear_tv(rettv);
4581 clear_tv(&var2);
4582 rettv->v_type = VAR_NUMBER;
4583 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 }
4585 }
4586
4587 return OK;
4588}
4589
4590/*
4591 * Handle fourth level expression:
4592 * + number addition
4593 * - number subtraction
4594 * . string concatenation
4595 *
4596 * "arg" must point to the first non-white of the expression.
4597 * "arg" is advanced to the next non-white after the recognized expression.
4598 *
4599 * Return OK or FAIL.
4600 */
4601 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004602eval5(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004604 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 int evaluate;
4606{
Bram Moolenaar33570922005-01-25 22:26:29 +00004607 typval_T var2;
4608 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 int op;
4610 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004611#ifdef FEAT_FLOAT
4612 float_T f1 = 0, f2 = 0;
4613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 char_u *s1, *s2;
4615 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4616 char_u *p;
4617
4618 /*
4619 * Get the first variable.
4620 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004621 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 return FAIL;
4623
4624 /*
4625 * Repeat computing, until no '+', '-' or '.' is following.
4626 */
4627 for (;;)
4628 {
4629 op = **arg;
4630 if (op != '+' && op != '-' && op != '.')
4631 break;
4632
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004633 if ((op != '+' || rettv->v_type != VAR_LIST)
4634#ifdef FEAT_FLOAT
4635 && (op == '.' || rettv->v_type != VAR_FLOAT)
4636#endif
4637 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004638 {
4639 /* For "list + ...", an illegal use of the first operand as
4640 * a number cannot be determined before evaluating the 2nd
4641 * operand: if this is also a list, all is ok.
4642 * For "something . ...", "something - ..." or "non-list + ...",
4643 * we know that the first operand needs to be a string or number
4644 * without evaluating the 2nd operand. So check before to avoid
4645 * side effects after an error. */
4646 if (evaluate && get_tv_string_chk(rettv) == NULL)
4647 {
4648 clear_tv(rettv);
4649 return FAIL;
4650 }
4651 }
4652
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 /*
4654 * Get the second variable.
4655 */
4656 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004657 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004659 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 return FAIL;
4661 }
4662
4663 if (evaluate)
4664 {
4665 /*
4666 * Compute the result.
4667 */
4668 if (op == '.')
4669 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004670 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4671 s2 = get_tv_string_buf_chk(&var2, buf2);
4672 if (s2 == NULL) /* type error ? */
4673 {
4674 clear_tv(rettv);
4675 clear_tv(&var2);
4676 return FAIL;
4677 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004678 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004679 clear_tv(rettv);
4680 rettv->v_type = VAR_STRING;
4681 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004683 else if (op == '+' && rettv->v_type == VAR_LIST
4684 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004685 {
4686 /* concatenate Lists */
4687 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4688 &var3) == FAIL)
4689 {
4690 clear_tv(rettv);
4691 clear_tv(&var2);
4692 return FAIL;
4693 }
4694 clear_tv(rettv);
4695 *rettv = var3;
4696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 else
4698 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004699 int error = FALSE;
4700
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004701#ifdef FEAT_FLOAT
4702 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004703 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004704 f1 = rettv->vval.v_float;
4705 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004706 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004707 else
4708#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004709 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004710 n1 = get_tv_number_chk(rettv, &error);
4711 if (error)
4712 {
4713 /* This can only happen for "list + non-list". For
4714 * "non-list + ..." or "something - ...", we returned
4715 * before evaluating the 2nd operand. */
4716 clear_tv(rettv);
4717 return FAIL;
4718 }
4719#ifdef FEAT_FLOAT
4720 if (var2.v_type == VAR_FLOAT)
4721 f1 = n1;
4722#endif
4723 }
4724#ifdef FEAT_FLOAT
4725 if (var2.v_type == VAR_FLOAT)
4726 {
4727 f2 = var2.vval.v_float;
4728 n2 = 0;
4729 }
4730 else
4731#endif
4732 {
4733 n2 = get_tv_number_chk(&var2, &error);
4734 if (error)
4735 {
4736 clear_tv(rettv);
4737 clear_tv(&var2);
4738 return FAIL;
4739 }
4740#ifdef FEAT_FLOAT
4741 if (rettv->v_type == VAR_FLOAT)
4742 f2 = n2;
4743#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004744 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004745 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004746
4747#ifdef FEAT_FLOAT
4748 /* If there is a float on either side the result is a float. */
4749 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4750 {
4751 if (op == '+')
4752 f1 = f1 + f2;
4753 else
4754 f1 = f1 - f2;
4755 rettv->v_type = VAR_FLOAT;
4756 rettv->vval.v_float = f1;
4757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004759#endif
4760 {
4761 if (op == '+')
4762 n1 = n1 + n2;
4763 else
4764 n1 = n1 - n2;
4765 rettv->v_type = VAR_NUMBER;
4766 rettv->vval.v_number = n1;
4767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771 }
4772 return OK;
4773}
4774
4775/*
4776 * Handle fifth level expression:
4777 * * number multiplication
4778 * / number division
4779 * % number modulo
4780 *
4781 * "arg" must point to the first non-white of the expression.
4782 * "arg" is advanced to the next non-white after the recognized expression.
4783 *
4784 * Return OK or FAIL.
4785 */
4786 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004787eval6(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004789 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 int evaluate;
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004791 int want_string; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792{
Bram Moolenaar33570922005-01-25 22:26:29 +00004793 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 int op;
4795 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004796#ifdef FEAT_FLOAT
4797 int use_float = FALSE;
4798 float_T f1 = 0, f2;
4799#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801
4802 /*
4803 * Get the first variable.
4804 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004805 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return FAIL;
4807
4808 /*
4809 * Repeat computing, until no '*', '/' or '%' is following.
4810 */
4811 for (;;)
4812 {
4813 op = **arg;
4814 if (op != '*' && op != '/' && op != '%')
4815 break;
4816
4817 if (evaluate)
4818 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004819#ifdef FEAT_FLOAT
4820 if (rettv->v_type == VAR_FLOAT)
4821 {
4822 f1 = rettv->vval.v_float;
4823 use_float = TRUE;
4824 n1 = 0;
4825 }
4826 else
4827#endif
4828 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004829 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004830 if (error)
4831 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 }
4833 else
4834 n1 = 0;
4835
4836 /*
4837 * Get the second variable.
4838 */
4839 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004840 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 return FAIL;
4842
4843 if (evaluate)
4844 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004845#ifdef FEAT_FLOAT
4846 if (var2.v_type == VAR_FLOAT)
4847 {
4848 if (!use_float)
4849 {
4850 f1 = n1;
4851 use_float = TRUE;
4852 }
4853 f2 = var2.vval.v_float;
4854 n2 = 0;
4855 }
4856 else
4857#endif
4858 {
4859 n2 = get_tv_number_chk(&var2, &error);
4860 clear_tv(&var2);
4861 if (error)
4862 return FAIL;
4863#ifdef FEAT_FLOAT
4864 if (use_float)
4865 f2 = n2;
4866#endif
4867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868
4869 /*
4870 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004871 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004873#ifdef FEAT_FLOAT
4874 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004876 if (op == '*')
4877 f1 = f1 * f2;
4878 else if (op == '/')
4879 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004880# ifdef VMS
4881 /* VMS crashes on divide by zero, work around it */
4882 if (f2 == 0.0)
4883 {
4884 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004885 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004886 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004887 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004888 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004889 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004890 }
4891 else
4892 f1 = f1 / f2;
4893# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004894 /* We rely on the floating point library to handle divide
4895 * by zero to result in "inf" and not a crash. */
4896 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004897# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004900 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004901 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004902 return FAIL;
4903 }
4904 rettv->v_type = VAR_FLOAT;
4905 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
4907 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004908#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910 if (op == '*')
4911 n1 = n1 * n2;
4912 else if (op == '/')
4913 {
4914 if (n2 == 0) /* give an error message? */
4915 {
4916 if (n1 == 0)
4917 n1 = -0x7fffffffL - 1L; /* similar to NaN */
4918 else if (n1 < 0)
4919 n1 = -0x7fffffffL;
4920 else
4921 n1 = 0x7fffffffL;
4922 }
4923 else
4924 n1 = n1 / n2;
4925 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004927 {
4928 if (n2 == 0) /* give an error message? */
4929 n1 = 0;
4930 else
4931 n1 = n1 % n2;
4932 }
4933 rettv->v_type = VAR_NUMBER;
4934 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937 }
4938
4939 return OK;
4940}
4941
4942/*
4943 * Handle sixth level expression:
4944 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00004945 * "string" string constant
4946 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 * &option-name option value
4948 * @r register contents
4949 * identifier variable value
4950 * function() function call
4951 * $VAR environment variable
4952 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00004953 * [expr, expr] List
4954 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 *
4956 * Also handle:
4957 * ! in front logical NOT
4958 * - in front unary minus
4959 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00004960 * trailing [] subscript in String or List
4961 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 *
4963 * "arg" must point to the first non-white of the expression.
4964 * "arg" is advanced to the next non-white after the recognized expression.
4965 *
4966 * Return OK or FAIL.
4967 */
4968 static int
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004969eval7(arg, rettv, evaluate, want_string)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00004971 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 int evaluate;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02004973 int want_string UNUSED; /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 long n;
4976 int len;
4977 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 char_u *start_leader, *end_leader;
4979 int ret = OK;
4980 char_u *alias;
4981
4982 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004983 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00004984 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004986 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987
4988 /*
4989 * Skip '!' and '-' characters. They are handled later.
4990 */
4991 start_leader = *arg;
4992 while (**arg == '!' || **arg == '-' || **arg == '+')
4993 *arg = skipwhite(*arg + 1);
4994 end_leader = *arg;
4995
4996 switch (**arg)
4997 {
4998 /*
4999 * Number constant.
5000 */
5001 case '0':
5002 case '1':
5003 case '2':
5004 case '3':
5005 case '4':
5006 case '5':
5007 case '6':
5008 case '7':
5009 case '8':
5010 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005011 {
5012#ifdef FEAT_FLOAT
5013 char_u *p = skipdigits(*arg + 1);
5014 int get_float = FALSE;
5015
5016 /* We accept a float when the format matches
5017 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005018 * strict to avoid backwards compatibility problems.
5019 * Don't look for a float after the "." operator, so that
5020 * ":let vers = 1.2.3" doesn't fail. */
5021 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005023 get_float = TRUE;
5024 p = skipdigits(p + 2);
5025 if (*p == 'e' || *p == 'E')
5026 {
5027 ++p;
5028 if (*p == '-' || *p == '+')
5029 ++p;
5030 if (!vim_isdigit(*p))
5031 get_float = FALSE;
5032 else
5033 p = skipdigits(p + 1);
5034 }
5035 if (ASCII_ISALPHA(*p) || *p == '.')
5036 get_float = FALSE;
5037 }
5038 if (get_float)
5039 {
5040 float_T f;
5041
5042 *arg += string2float(*arg, &f);
5043 if (evaluate)
5044 {
5045 rettv->v_type = VAR_FLOAT;
5046 rettv->vval.v_float = f;
5047 }
5048 }
5049 else
5050#endif
5051 {
5052 vim_str2nr(*arg, NULL, &len, TRUE, TRUE, &n, NULL);
5053 *arg += len;
5054 if (evaluate)
5055 {
5056 rettv->v_type = VAR_NUMBER;
5057 rettv->vval.v_number = n;
5058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
5063 /*
5064 * String constant: "string".
5065 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005066 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 break;
5068
5069 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005072 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005073 break;
5074
5075 /*
5076 * List: [expr, expr]
5077 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005078 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 break;
5080
5081 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005082 * Dictionary: {key: val, key: val}
5083 */
5084 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5085 break;
5086
5087 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005088 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005090 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 break;
5092
5093 /*
5094 * Environment variable: $VAR.
5095 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005096 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 break;
5098
5099 /*
5100 * Register contents: @r.
5101 */
5102 case '@': ++*arg;
5103 if (evaluate)
5104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005105 rettv->v_type = VAR_STRING;
Bram Moolenaar92124a32005-06-17 22:03:40 +00005106 rettv->vval.v_string = get_reg_contents(**arg, TRUE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 if (**arg != NUL)
5109 ++*arg;
5110 break;
5111
5112 /*
5113 * nested expression: (expression).
5114 */
5115 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005116 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 if (**arg == ')')
5118 ++*arg;
5119 else if (ret == OK)
5120 {
5121 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005122 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 ret = FAIL;
5124 }
5125 break;
5126
Bram Moolenaar8c711452005-01-14 21:53:12 +00005127 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 break;
5129 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005130
5131 if (ret == NOTDONE)
5132 {
5133 /*
5134 * Must be a variable or function name.
5135 * Can also be a curly-braces kind of name: {expr}.
5136 */
5137 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005138 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005139 if (alias != NULL)
5140 s = alias;
5141
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005142 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005143 ret = FAIL;
5144 else
5145 {
5146 if (**arg == '(') /* recursive! */
5147 {
5148 /* If "s" is the name of a variable of type VAR_FUNC
5149 * use its contents. */
5150 s = deref_func_name(s, &len);
5151
5152 /* Invoke the function. */
5153 ret = get_func_tv(s, len, rettv, arg,
5154 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005155 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005156
5157 /* If evaluate is FALSE rettv->v_type was not set in
5158 * get_func_tv, but it's needed in handle_subscript() to parse
5159 * what follows. So set it here. */
5160 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5161 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005162 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005163 rettv->v_type = VAR_FUNC;
5164 }
5165
Bram Moolenaar8c711452005-01-14 21:53:12 +00005166 /* Stop the expression evaluation when immediately
5167 * aborting on error, or when an interrupt occurred or
5168 * an exception was thrown but not caught. */
5169 if (aborting())
5170 {
5171 if (ret == OK)
5172 clear_tv(rettv);
5173 ret = FAIL;
5174 }
5175 }
5176 else if (evaluate)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005177 ret = get_var_tv(s, len, rettv, TRUE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005178 else
5179 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005181 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005182 }
5183
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 *arg = skipwhite(*arg);
5185
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005186 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5187 * expr(expr). */
5188 if (ret == OK)
5189 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190
5191 /*
5192 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5193 */
5194 if (ret == OK && evaluate && end_leader > start_leader)
5195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005196 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005197 int val = 0;
5198#ifdef FEAT_FLOAT
5199 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005200
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005201 if (rettv->v_type == VAR_FLOAT)
5202 f = rettv->vval.v_float;
5203 else
5204#endif
5205 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005206 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005208 clear_tv(rettv);
5209 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005210 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005211 else
5212 {
5213 while (end_leader > start_leader)
5214 {
5215 --end_leader;
5216 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005217 {
5218#ifdef FEAT_FLOAT
5219 if (rettv->v_type == VAR_FLOAT)
5220 f = !f;
5221 else
5222#endif
5223 val = !val;
5224 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005225 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005226 {
5227#ifdef FEAT_FLOAT
5228 if (rettv->v_type == VAR_FLOAT)
5229 f = -f;
5230 else
5231#endif
5232 val = -val;
5233 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005234 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005235#ifdef FEAT_FLOAT
5236 if (rettv->v_type == VAR_FLOAT)
5237 {
5238 clear_tv(rettv);
5239 rettv->vval.v_float = f;
5240 }
5241 else
5242#endif
5243 {
5244 clear_tv(rettv);
5245 rettv->v_type = VAR_NUMBER;
5246 rettv->vval.v_number = val;
5247 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250
5251 return ret;
5252}
5253
5254/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005255 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5256 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005257 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5258 */
5259 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005260eval_index(arg, rettv, evaluate, verbose)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005261 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005262 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005263 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005264 int verbose; /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005265{
5266 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005267 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005268 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005269 long len = -1;
5270 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005271 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005273
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005274 if (rettv->v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005276 if (verbose)
5277 EMSG(_("E695: Cannot index a Funcref"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005278 return FAIL;
5279 }
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005280#ifdef FEAT_FLOAT
5281 else if (rettv->v_type == VAR_FLOAT)
5282 {
5283 if (verbose)
5284 EMSG(_(e_float_as_string));
5285 return FAIL;
5286 }
5287#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005288
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005290 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005291 /*
5292 * dict.name
5293 */
5294 key = *arg + 1;
5295 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5296 ;
5297 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005298 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005299 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005300 }
5301 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005302 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005303 /*
5304 * something[idx]
5305 *
5306 * Get the (first) variable from inside the [].
5307 */
5308 *arg = skipwhite(*arg + 1);
5309 if (**arg == ':')
5310 empty1 = TRUE;
5311 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5312 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5314 {
5315 /* not a number or string */
5316 clear_tv(&var1);
5317 return FAIL;
5318 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005319
5320 /*
5321 * Get the second variable from inside the [:].
5322 */
5323 if (**arg == ':')
5324 {
5325 range = TRUE;
5326 *arg = skipwhite(*arg + 1);
5327 if (**arg == ']')
5328 empty2 = TRUE;
5329 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5330 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005331 if (!empty1)
5332 clear_tv(&var1);
5333 return FAIL;
5334 }
5335 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5336 {
5337 /* not a number or string */
5338 if (!empty1)
5339 clear_tv(&var1);
5340 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005341 return FAIL;
5342 }
5343 }
5344
5345 /* Check for the ']'. */
5346 if (**arg != ']')
5347 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005348 if (verbose)
5349 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005350 clear_tv(&var1);
5351 if (range)
5352 clear_tv(&var2);
5353 return FAIL;
5354 }
5355 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 }
5357
5358 if (evaluate)
5359 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005360 n1 = 0;
5361 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005363 n1 = get_tv_number(&var1);
5364 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 }
5366 if (range)
5367 {
5368 if (empty2)
5369 n2 = -1;
5370 else
5371 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005372 n2 = get_tv_number(&var2);
5373 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005374 }
5375 }
5376
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005377 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 {
5379 case VAR_NUMBER:
5380 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005381 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 len = (long)STRLEN(s);
5383 if (range)
5384 {
5385 /* The resulting variable is a substring. If the indexes
5386 * are out of range the result is empty. */
5387 if (n1 < 0)
5388 {
5389 n1 = len + n1;
5390 if (n1 < 0)
5391 n1 = 0;
5392 }
5393 if (n2 < 0)
5394 n2 = len + n2;
5395 else if (n2 >= len)
5396 n2 = len;
5397 if (n1 >= len || n2 < 0 || n1 > n2)
5398 s = NULL;
5399 else
5400 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5401 }
5402 else
5403 {
5404 /* The resulting variable is a string of a single
5405 * character. If the index is too big or negative the
5406 * result is empty. */
5407 if (n1 >= len || n1 < 0)
5408 s = NULL;
5409 else
5410 s = vim_strnsave(s + n1, 1);
5411 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005412 clear_tv(rettv);
5413 rettv->v_type = VAR_STRING;
5414 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 break;
5416
5417 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005418 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005419 if (n1 < 0)
5420 n1 = len + n1;
5421 if (!empty1 && (n1 < 0 || n1 >= len))
5422 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005423 /* For a range we allow invalid values and return an empty
5424 * list. A list index out of range is an error. */
5425 if (!range)
5426 {
5427 if (verbose)
5428 EMSGN(_(e_listidx), n1);
5429 return FAIL;
5430 }
5431 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005432 }
5433 if (range)
5434 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005435 list_T *l;
5436 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005437
5438 if (n2 < 0)
5439 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005440 else if (n2 >= len)
5441 n2 = len - 1;
5442 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005443 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005444 l = list_alloc();
5445 if (l == NULL)
5446 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005447 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005448 n1 <= n2; ++n1)
5449 {
5450 if (list_append_tv(l, &item->li_tv) == FAIL)
5451 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005452 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005453 return FAIL;
5454 }
5455 item = item->li_next;
5456 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005457 clear_tv(rettv);
5458 rettv->v_type = VAR_LIST;
5459 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005460 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005461 }
5462 else
5463 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005464 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005465 clear_tv(rettv);
5466 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 }
5468 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469
5470 case VAR_DICT:
5471 if (range)
5472 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005473 if (verbose)
5474 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475 if (len == -1)
5476 clear_tv(&var1);
5477 return FAIL;
5478 }
5479 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005480 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481
5482 if (len == -1)
5483 {
5484 key = get_tv_string(&var1);
5485 if (*key == NUL)
5486 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005487 if (verbose)
5488 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005489 clear_tv(&var1);
5490 return FAIL;
5491 }
5492 }
5493
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005494 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005495
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005496 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005497 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005498 if (len == -1)
5499 clear_tv(&var1);
5500 if (item == NULL)
5501 return FAIL;
5502
5503 copy_tv(&item->di_tv, &var1);
5504 clear_tv(rettv);
5505 *rettv = var1;
5506 }
5507 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005508 }
5509 }
5510
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005511 return OK;
5512}
5513
5514/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 * Get an option value.
5516 * "arg" points to the '&' or '+' before the option name.
5517 * "arg" is advanced to character after the option name.
5518 * Return OK or FAIL.
5519 */
5520 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521get_option_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005523 typval_T *rettv; /* when NULL, only check if option exists */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 int evaluate;
5525{
5526 char_u *option_end;
5527 long numval;
5528 char_u *stringval;
5529 int opt_type;
5530 int c;
5531 int working = (**arg == '+'); /* has("+option") */
5532 int ret = OK;
5533 int opt_flags;
5534
5535 /*
5536 * Isolate the option name and find its value.
5537 */
5538 option_end = find_option_end(arg, &opt_flags);
5539 if (option_end == NULL)
5540 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005541 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 EMSG2(_("E112: Option name missing: %s"), *arg);
5543 return FAIL;
5544 }
5545
5546 if (!evaluate)
5547 {
5548 *arg = option_end;
5549 return OK;
5550 }
5551
5552 c = *option_end;
5553 *option_end = NUL;
5554 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556
5557 if (opt_type == -3) /* invalid name */
5558 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005559 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 EMSG2(_("E113: Unknown option: %s"), *arg);
5561 ret = FAIL;
5562 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005563 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 {
5565 if (opt_type == -2) /* hidden string option */
5566 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005567 rettv->v_type = VAR_STRING;
5568 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569 }
5570 else if (opt_type == -1) /* hidden number option */
5571 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 rettv->v_type = VAR_NUMBER;
5573 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574 }
5575 else if (opt_type == 1) /* number option */
5576 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005577 rettv->v_type = VAR_NUMBER;
5578 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
5580 else /* string option */
5581 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005582 rettv->v_type = VAR_STRING;
5583 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
5585 }
5586 else if (working && (opt_type == -2 || opt_type == -1))
5587 ret = FAIL;
5588
5589 *option_end = c; /* put back for error messages */
5590 *arg = option_end;
5591
5592 return ret;
5593}
5594
5595/*
5596 * Allocate a variable for a string constant.
5597 * Return OK or FAIL.
5598 */
5599 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005600get_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005602 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 int evaluate;
5604{
5605 char_u *p;
5606 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005607 int extra = 0;
5608
5609 /*
5610 * Find the end of the string, skipping backslashed characters.
5611 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005612 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 {
5614 if (*p == '\\' && p[1] != NUL)
5615 {
5616 ++p;
5617 /* A "\<x>" form occupies at least 4 characters, and produces up
5618 * to 6 characters: reserve space for 2 extra */
5619 if (*p == '<')
5620 extra += 2;
5621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 }
5623
5624 if (*p != '"')
5625 {
5626 EMSG2(_("E114: Missing quote: %s"), *arg);
5627 return FAIL;
5628 }
5629
5630 /* If only parsing, set *arg and return here */
5631 if (!evaluate)
5632 {
5633 *arg = p + 1;
5634 return OK;
5635 }
5636
5637 /*
5638 * Copy the string into allocated memory, handling backslashed
5639 * characters.
5640 */
5641 name = alloc((unsigned)(p - *arg + extra));
5642 if (name == NULL)
5643 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005644 rettv->v_type = VAR_STRING;
5645 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005646
Bram Moolenaar8c711452005-01-14 21:53:12 +00005647 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 {
5649 if (*p == '\\')
5650 {
5651 switch (*++p)
5652 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005653 case 'b': *name++ = BS; ++p; break;
5654 case 'e': *name++ = ESC; ++p; break;
5655 case 'f': *name++ = FF; ++p; break;
5656 case 'n': *name++ = NL; ++p; break;
5657 case 'r': *name++ = CAR; ++p; break;
5658 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659
5660 case 'X': /* hex: "\x1", "\x12" */
5661 case 'x':
5662 case 'u': /* Unicode: "\u0023" */
5663 case 'U':
5664 if (vim_isxdigit(p[1]))
5665 {
5666 int n, nr;
5667 int c = toupper(*p);
5668
5669 if (c == 'X')
5670 n = 2;
5671 else
5672 n = 4;
5673 nr = 0;
5674 while (--n >= 0 && vim_isxdigit(p[1]))
5675 {
5676 ++p;
5677 nr = (nr << 4) + hex2nr(*p);
5678 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005679 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680#ifdef FEAT_MBYTE
5681 /* For "\u" store the number according to
5682 * 'encoding'. */
5683 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005684 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 else
5686#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005687 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 break;
5690
5691 /* octal: "\1", "\12", "\123" */
5692 case '0':
5693 case '1':
5694 case '2':
5695 case '3':
5696 case '4':
5697 case '5':
5698 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005699 case '7': *name = *p++ - '0';
5700 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005702 *name = (*name << 3) + *p++ - '0';
5703 if (*p >= '0' && *p <= '7')
5704 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005706 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005707 break;
5708
5709 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005710 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 if (extra != 0)
5712 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005713 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 break;
5715 }
5716 /* FALLTHROUGH */
5717
Bram Moolenaar8c711452005-01-14 21:53:12 +00005718 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 break;
5720 }
5721 }
5722 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005723 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005726 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 *arg = p + 1;
5728
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 return OK;
5730}
5731
5732/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005733 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 * Return OK or FAIL.
5735 */
5736 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005737get_lit_string_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005739 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 int evaluate;
5741{
5742 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005744 int reduce = 0;
5745
5746 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005747 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005748 */
5749 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5750 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005751 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005752 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005753 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005754 break;
5755 ++reduce;
5756 ++p;
5757 }
5758 }
5759
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005761 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005762 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005763 return FAIL;
5764 }
5765
Bram Moolenaar8c711452005-01-14 21:53:12 +00005766 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005767 if (!evaluate)
5768 {
5769 *arg = p + 1;
5770 return OK;
5771 }
5772
5773 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005774 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005775 */
5776 str = alloc((unsigned)((p - *arg) - reduce));
5777 if (str == NULL)
5778 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005779 rettv->v_type = VAR_STRING;
5780 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005781
Bram Moolenaar8c711452005-01-14 21:53:12 +00005782 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005785 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005786 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005787 break;
5788 ++p;
5789 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005791 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005792 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005793 *arg = p + 1;
5794
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005795 return OK;
5796}
5797
5798/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005799 * Allocate a variable for a List and fill it from "*arg".
5800 * Return OK or FAIL.
5801 */
5802 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005803get_list_tv(arg, rettv, evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005804 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00005805 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005806 int evaluate;
5807{
Bram Moolenaar33570922005-01-25 22:26:29 +00005808 list_T *l = NULL;
5809 typval_T tv;
5810 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005811
5812 if (evaluate)
5813 {
5814 l = list_alloc();
5815 if (l == NULL)
5816 return FAIL;
5817 }
5818
5819 *arg = skipwhite(*arg + 1);
5820 while (**arg != ']' && **arg != NUL)
5821 {
5822 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5823 goto failret;
5824 if (evaluate)
5825 {
5826 item = listitem_alloc();
5827 if (item != NULL)
5828 {
5829 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005830 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005831 list_append(l, item);
5832 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005833 else
5834 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005835 }
5836
5837 if (**arg == ']')
5838 break;
5839 if (**arg != ',')
5840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005842 goto failret;
5843 }
5844 *arg = skipwhite(*arg + 1);
5845 }
5846
5847 if (**arg != ']')
5848 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005850failret:
5851 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005852 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005853 return FAIL;
5854 }
5855
5856 *arg = skipwhite(*arg + 1);
5857 if (evaluate)
5858 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005859 rettv->v_type = VAR_LIST;
5860 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005861 ++l->lv_refcount;
5862 }
5863
5864 return OK;
5865}
5866
5867/*
5868 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005869 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005870 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005871 list_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005872list_alloc()
5873{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005874 list_T *l;
5875
5876 l = (list_T *)alloc_clear(sizeof(list_T));
5877 if (l != NULL)
5878 {
5879 /* Prepend the list to the list of lists for garbage collection. */
5880 if (first_list != NULL)
5881 first_list->lv_used_prev = l;
5882 l->lv_used_prev = NULL;
5883 l->lv_used_next = first_list;
5884 first_list = l;
5885 }
5886 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005887}
5888
5889/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005890 * Allocate an empty list for a return value.
5891 * Returns OK or FAIL.
5892 */
5893 static int
5894rettv_list_alloc(rettv)
5895 typval_T *rettv;
5896{
5897 list_T *l = list_alloc();
5898
5899 if (l == NULL)
5900 return FAIL;
5901
5902 rettv->vval.v_list = l;
5903 rettv->v_type = VAR_LIST;
5904 ++l->lv_refcount;
5905 return OK;
5906}
5907
5908/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005909 * Unreference a list: decrement the reference count and free it when it
5910 * becomes zero.
5911 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00005912 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005913list_unref(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005914 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915{
Bram Moolenaar685295c2006-10-15 20:37:38 +00005916 if (l != NULL && --l->lv_refcount <= 0)
5917 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005918}
5919
5920/*
5921 * Free a list, including all items it points to.
5922 * Ignores the reference count.
5923 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005924 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00005925list_free(l, recurse)
5926 list_T *l;
5927 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005928{
Bram Moolenaar33570922005-01-25 22:26:29 +00005929 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005930
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005931 /* Remove the list from the list of lists for garbage collection. */
5932 if (l->lv_used_prev == NULL)
5933 first_list = l->lv_used_next;
5934 else
5935 l->lv_used_prev->lv_used_next = l->lv_used_next;
5936 if (l->lv_used_next != NULL)
5937 l->lv_used_next->lv_used_prev = l->lv_used_prev;
5938
Bram Moolenaard9fba312005-06-26 22:34:35 +00005939 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00005941 /* Remove the item before deleting it. */
5942 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00005943 if (recurse || (item->li_tv.v_type != VAR_LIST
5944 && item->li_tv.v_type != VAR_DICT))
5945 clear_tv(&item->li_tv);
5946 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 }
5948 vim_free(l);
5949}
5950
5951/*
5952 * Allocate a list item.
5953 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005954 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005955listitem_alloc()
5956{
Bram Moolenaar33570922005-01-25 22:26:29 +00005957 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958}
5959
5960/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00005961 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02005963 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964listitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005965 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005967 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 vim_free(item);
5969}
5970
5971/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005972 * Remove a list item from a List and free it. Also clears the value.
5973 */
Bram Moolenaardb913952012-06-29 12:54:53 +02005974 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005975listitem_remove(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00005976 list_T *l;
5977 listitem_T *item;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00005978{
5979 list_remove(l, item, item);
5980 listitem_free(item);
5981}
5982
5983/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005984 * Get the number of items in a list.
5985 */
5986 static long
5987list_len(l)
Bram Moolenaar33570922005-01-25 22:26:29 +00005988 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005989{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005990 if (l == NULL)
5991 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00005992 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005993}
5994
5995/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00005996 * Return TRUE when two lists have exactly the same values.
5997 */
5998 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01005999list_equal(l1, l2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006000 list_T *l1;
6001 list_T *l2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006002 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006003 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006004{
Bram Moolenaar33570922005-01-25 22:26:29 +00006005 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006006
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006007 if (l1 == NULL || l2 == NULL)
6008 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006009 if (l1 == l2)
6010 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006011 if (list_len(l1) != list_len(l2))
6012 return FALSE;
6013
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006014 for (item1 = l1->lv_first, item2 = l2->lv_first;
6015 item1 != NULL && item2 != NULL;
6016 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006017 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006018 return FALSE;
6019 return item1 == NULL && item2 == NULL;
6020}
6021
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +02006022#if defined(FEAT_RUBY) || defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) \
6023 || defined(FEAT_MZSCHEME) || defined(FEAT_LUA) || defined(PROTO)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006024/*
6025 * Return the dictitem that an entry in a hashtable points to.
6026 */
6027 dictitem_T *
6028dict_lookup(hi)
6029 hashitem_T *hi;
6030{
6031 return HI2DI(hi);
6032}
6033#endif
6034
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006035/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006036 * Return TRUE when two dictionaries have exactly the same key/values.
6037 */
6038 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006039dict_equal(d1, d2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006040 dict_T *d1;
6041 dict_T *d2;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006042 int ic; /* ignore case for strings */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006043 int recursive; /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006044{
Bram Moolenaar33570922005-01-25 22:26:29 +00006045 hashitem_T *hi;
6046 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006047 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006048
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006049 if (d1 == NULL || d2 == NULL)
6050 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006051 if (d1 == d2)
6052 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006053 if (dict_len(d1) != dict_len(d2))
6054 return FALSE;
6055
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006056 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006058 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006059 if (!HASHITEM_EMPTY(hi))
6060 {
6061 item2 = dict_find(d2, hi->hi_key, -1);
6062 if (item2 == NULL)
6063 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006064 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006065 return FALSE;
6066 --todo;
6067 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006068 }
6069 return TRUE;
6070}
6071
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006072static int tv_equal_recurse_limit;
6073
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006074/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006075 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006076 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006077 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006078 */
6079 static int
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006080tv_equal(tv1, tv2, ic, recursive)
Bram Moolenaar33570922005-01-25 22:26:29 +00006081 typval_T *tv1;
6082 typval_T *tv2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006083 int ic; /* ignore case */
6084 int recursive; /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006085{
6086 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006087 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006088 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006089 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006090
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006091 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006092 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006093
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006094 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006095 * recursiveness to a limit. We guess they are equal then.
6096 * A fixed limit has the problem of still taking an awful long time.
6097 * Reduce the limit every time running into it. That should work fine for
6098 * deeply linked structures that are not recursively linked and catch
6099 * recursiveness quickly. */
6100 if (!recursive)
6101 tv_equal_recurse_limit = 1000;
6102 if (recursive_cnt >= tv_equal_recurse_limit)
6103 {
6104 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006105 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006106 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006107
6108 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006110 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006111 ++recursive_cnt;
6112 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6113 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006114 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006115
6116 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006117 ++recursive_cnt;
6118 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6119 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006120 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006121
6122 case VAR_FUNC:
6123 return (tv1->vval.v_string != NULL
6124 && tv2->vval.v_string != NULL
6125 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6126
6127 case VAR_NUMBER:
6128 return tv1->vval.v_number == tv2->vval.v_number;
6129
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006130#ifdef FEAT_FLOAT
6131 case VAR_FLOAT:
6132 return tv1->vval.v_float == tv2->vval.v_float;
6133#endif
6134
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006135 case VAR_STRING:
6136 s1 = get_tv_string_buf(tv1, buf1);
6137 s2 = get_tv_string_buf(tv2, buf2);
6138 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006139 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006140
6141 EMSG2(_(e_intern2), "tv_equal()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006142 return TRUE;
6143}
6144
6145/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006146 * Locate item with index "n" in list "l" and return it.
6147 * A negative index is counted from the end; -1 is the last item.
6148 * Returns NULL when "n" is out of range.
6149 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006150 listitem_T *
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006151list_find(l, n)
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 list_T *l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006153 long n;
6154{
Bram Moolenaar33570922005-01-25 22:26:29 +00006155 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006156 long idx;
6157
6158 if (l == NULL)
6159 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006160
6161 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006162 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006163 n = l->lv_len + n;
6164
6165 /* Check for index out of range. */
6166 if (n < 0 || n >= l->lv_len)
6167 return NULL;
6168
6169 /* When there is a cached index may start search from there. */
6170 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006171 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006172 if (n < l->lv_idx / 2)
6173 {
6174 /* closest to the start of the list */
6175 item = l->lv_first;
6176 idx = 0;
6177 }
6178 else if (n > (l->lv_idx + l->lv_len) / 2)
6179 {
6180 /* closest to the end of the list */
6181 item = l->lv_last;
6182 idx = l->lv_len - 1;
6183 }
6184 else
6185 {
6186 /* closest to the cached index */
6187 item = l->lv_idx_item;
6188 idx = l->lv_idx;
6189 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006190 }
6191 else
6192 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006193 if (n < l->lv_len / 2)
6194 {
6195 /* closest to the start of the list */
6196 item = l->lv_first;
6197 idx = 0;
6198 }
6199 else
6200 {
6201 /* closest to the end of the list */
6202 item = l->lv_last;
6203 idx = l->lv_len - 1;
6204 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006205 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006206
6207 while (n > idx)
6208 {
6209 /* search forward */
6210 item = item->li_next;
6211 ++idx;
6212 }
6213 while (n < idx)
6214 {
6215 /* search backward */
6216 item = item->li_prev;
6217 --idx;
6218 }
6219
6220 /* cache the used index */
6221 l->lv_idx = idx;
6222 l->lv_idx_item = item;
6223
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006224 return item;
6225}
6226
6227/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006228 * Get list item "l[idx]" as a number.
6229 */
6230 static long
6231list_find_nr(l, idx, errorp)
6232 list_T *l;
6233 long idx;
6234 int *errorp; /* set to TRUE when something wrong */
6235{
6236 listitem_T *li;
6237
6238 li = list_find(l, idx);
6239 if (li == NULL)
6240 {
6241 if (errorp != NULL)
6242 *errorp = TRUE;
6243 return -1L;
6244 }
6245 return get_tv_number_chk(&li->li_tv, errorp);
6246}
6247
6248/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006249 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6250 */
6251 char_u *
6252list_find_str(l, idx)
6253 list_T *l;
6254 long idx;
6255{
6256 listitem_T *li;
6257
6258 li = list_find(l, idx - 1);
6259 if (li == NULL)
6260 {
6261 EMSGN(_(e_listidx), idx);
6262 return NULL;
6263 }
6264 return get_tv_string(&li->li_tv);
6265}
6266
6267/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006268 * Locate "item" list "l" and return its index.
6269 * Returns -1 when "item" is not in the list.
6270 */
6271 static long
6272list_idx_of_item(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006273 list_T *l;
6274 listitem_T *item;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006275{
6276 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006277 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006278
6279 if (l == NULL)
6280 return -1;
6281 idx = 0;
6282 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6283 ++idx;
6284 if (li == NULL)
6285 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006286 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006287}
6288
6289/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006290 * Append item "item" to the end of list "l".
6291 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006292 void
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293list_append(l, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006294 list_T *l;
6295 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296{
6297 if (l->lv_last == NULL)
6298 {
6299 /* empty list */
6300 l->lv_first = item;
6301 l->lv_last = item;
6302 item->li_prev = NULL;
6303 }
6304 else
6305 {
6306 l->lv_last->li_next = item;
6307 item->li_prev = l->lv_last;
6308 l->lv_last = item;
6309 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006310 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311 item->li_next = NULL;
6312}
6313
6314/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006315 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006316 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006317 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006318 int
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006319list_append_tv(l, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006320 list_T *l;
6321 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006322{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006323 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006324
Bram Moolenaar05159a02005-02-26 23:04:13 +00006325 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006327 copy_tv(tv, &li->li_tv);
6328 list_append(l, li);
6329 return OK;
6330}
6331
6332/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006333 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006334 * Return FAIL when out of memory.
6335 */
6336 int
6337list_append_dict(list, dict)
6338 list_T *list;
6339 dict_T *dict;
6340{
6341 listitem_T *li = listitem_alloc();
6342
6343 if (li == NULL)
6344 return FAIL;
6345 li->li_tv.v_type = VAR_DICT;
6346 li->li_tv.v_lock = 0;
6347 li->li_tv.vval.v_dict = dict;
6348 list_append(list, li);
6349 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006350 return OK;
6351}
6352
6353/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006354 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006355 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006356 * Returns FAIL when out of memory.
6357 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006358 int
Bram Moolenaar4463f292005-09-25 22:20:24 +00006359list_append_string(l, str, len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006360 list_T *l;
6361 char_u *str;
Bram Moolenaar4463f292005-09-25 22:20:24 +00006362 int len;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006363{
6364 listitem_T *li = listitem_alloc();
6365
6366 if (li == NULL)
6367 return FAIL;
6368 list_append(l, li);
6369 li->li_tv.v_type = VAR_STRING;
6370 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006371 if (str == NULL)
6372 li->li_tv.vval.v_string = NULL;
6373 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006374 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006375 return FAIL;
6376 return OK;
6377}
6378
6379/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006380 * Append "n" to list "l".
6381 * Returns FAIL when out of memory.
6382 */
6383 static int
6384list_append_number(l, n)
6385 list_T *l;
6386 varnumber_T n;
6387{
6388 listitem_T *li;
6389
6390 li = listitem_alloc();
6391 if (li == NULL)
6392 return FAIL;
6393 li->li_tv.v_type = VAR_NUMBER;
6394 li->li_tv.v_lock = 0;
6395 li->li_tv.vval.v_number = n;
6396 list_append(l, li);
6397 return OK;
6398}
6399
6400/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006401 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006402 * If "item" is NULL append at the end.
6403 * Return FAIL when out of memory.
6404 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006405 int
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006406list_insert_tv(l, tv, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00006407 list_T *l;
6408 typval_T *tv;
6409 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006410{
Bram Moolenaar33570922005-01-25 22:26:29 +00006411 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006412
6413 if (ni == NULL)
6414 return FAIL;
6415 copy_tv(tv, &ni->li_tv);
6416 if (item == NULL)
6417 /* Append new item at end of list. */
6418 list_append(l, ni);
6419 else
6420 {
6421 /* Insert new item before existing item. */
6422 ni->li_prev = item->li_prev;
6423 ni->li_next = item;
6424 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006425 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006426 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006427 ++l->lv_idx;
6428 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006429 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006430 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006432 l->lv_idx_item = NULL;
6433 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006434 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006435 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006436 }
6437 return OK;
6438}
6439
6440/*
6441 * Extend "l1" with "l2".
6442 * If "bef" is NULL append at the end, otherwise insert before this item.
6443 * Returns FAIL when out of memory.
6444 */
6445 static int
6446list_extend(l1, l2, bef)
Bram Moolenaar33570922005-01-25 22:26:29 +00006447 list_T *l1;
6448 list_T *l2;
6449 listitem_T *bef;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006450{
Bram Moolenaar33570922005-01-25 22:26:29 +00006451 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006452 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006453
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006454 /* We also quit the loop when we have inserted the original item count of
6455 * the list, avoid a hang when we extend a list with itself. */
6456 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6458 return FAIL;
6459 return OK;
6460}
6461
6462/*
6463 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6464 * Return FAIL when out of memory.
6465 */
6466 static int
6467list_concat(l1, l2, tv)
Bram Moolenaar33570922005-01-25 22:26:29 +00006468 list_T *l1;
6469 list_T *l2;
6470 typval_T *tv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006471{
Bram Moolenaar33570922005-01-25 22:26:29 +00006472 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006473
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006474 if (l1 == NULL || l2 == NULL)
6475 return FAIL;
6476
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006477 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006478 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006479 if (l == NULL)
6480 return FAIL;
6481 tv->v_type = VAR_LIST;
6482 tv->vval.v_list = l;
6483
6484 /* append all items from the second list */
6485 return list_extend(l, l2, NULL);
6486}
6487
6488/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006489 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006490 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006491 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006492 * Returns NULL when out of memory.
6493 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006494 static list_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006495list_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006496 list_T *orig;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006497 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006498 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006499{
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 list_T *copy;
6501 listitem_T *item;
6502 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006503
6504 if (orig == NULL)
6505 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006506
6507 copy = list_alloc();
6508 if (copy != NULL)
6509 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006510 if (copyID != 0)
6511 {
6512 /* Do this before adding the items, because one of the items may
6513 * refer back to this list. */
6514 orig->lv_copyID = copyID;
6515 orig->lv_copylist = copy;
6516 }
6517 for (item = orig->lv_first; item != NULL && !got_int;
6518 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006519 {
6520 ni = listitem_alloc();
6521 if (ni == NULL)
6522 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006523 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006524 {
6525 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6526 {
6527 vim_free(ni);
6528 break;
6529 }
6530 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006531 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006532 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006533 list_append(copy, ni);
6534 }
6535 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006536 if (item != NULL)
6537 {
6538 list_unref(copy);
6539 copy = NULL;
6540 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006541 }
6542
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006543 return copy;
6544}
6545
6546/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006548 * Does not free the listitem or the value!
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006549 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006550 void
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006551list_remove(l, item, item2)
Bram Moolenaar33570922005-01-25 22:26:29 +00006552 list_T *l;
6553 listitem_T *item;
6554 listitem_T *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555{
Bram Moolenaar33570922005-01-25 22:26:29 +00006556 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006557
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558 /* notify watchers */
6559 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006560 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006561 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562 list_fix_watch(l, ip);
6563 if (ip == item2)
6564 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006565 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566
6567 if (item2->li_next == NULL)
6568 l->lv_last = item->li_prev;
6569 else
6570 item2->li_next->li_prev = item->li_prev;
6571 if (item->li_prev == NULL)
6572 l->lv_first = item2->li_next;
6573 else
6574 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006575 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006576}
6577
6578/*
6579 * Return an allocated string with the string representation of a list.
6580 * May return NULL.
6581 */
6582 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006583list2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00006584 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006585 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006586{
6587 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006588
6589 if (tv->vval.v_list == NULL)
6590 return NULL;
6591 ga_init2(&ga, (int)sizeof(char), 80);
6592 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006593 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 {
6595 vim_free(ga.ga_data);
6596 return NULL;
6597 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598 ga_append(&ga, ']');
6599 ga_append(&ga, NUL);
6600 return (char_u *)ga.ga_data;
6601}
6602
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006603typedef struct join_S {
6604 char_u *s;
6605 char_u *tofree;
6606} join_T;
6607
6608 static int
6609list_join_inner(gap, l, sep, echo_style, copyID, join_gap)
6610 garray_T *gap; /* to store the result in */
6611 list_T *l;
6612 char_u *sep;
6613 int echo_style;
6614 int copyID;
6615 garray_T *join_gap; /* to keep each list item string */
6616{
6617 int i;
6618 join_T *p;
6619 int len;
6620 int sumlen = 0;
6621 int first = TRUE;
6622 char_u *tofree;
6623 char_u numbuf[NUMBUFLEN];
6624 listitem_T *item;
6625 char_u *s;
6626
6627 /* Stringify each item in the list. */
6628 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6629 {
6630 if (echo_style)
6631 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6632 else
6633 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6634 if (s == NULL)
6635 return FAIL;
6636
6637 len = (int)STRLEN(s);
6638 sumlen += len;
6639
6640 ga_grow(join_gap, 1);
6641 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6642 if (tofree != NULL || s != numbuf)
6643 {
6644 p->s = s;
6645 p->tofree = tofree;
6646 }
6647 else
6648 {
6649 p->s = vim_strnsave(s, len);
6650 p->tofree = p->s;
6651 }
6652
6653 line_breakcheck();
6654 }
6655
6656 /* Allocate result buffer with its total size, avoid re-allocation and
6657 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6658 if (join_gap->ga_len >= 2)
6659 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6660 if (ga_grow(gap, sumlen + 2) == FAIL)
6661 return FAIL;
6662
6663 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6664 {
6665 if (first)
6666 first = FALSE;
6667 else
6668 ga_concat(gap, sep);
6669 p = ((join_T *)join_gap->ga_data) + i;
6670
6671 if (p->s != NULL)
6672 ga_concat(gap, p->s);
6673 line_breakcheck();
6674 }
6675
6676 return OK;
6677}
6678
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006680 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006681 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006682 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006683 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006684 static int
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006685list_join(gap, l, sep, echo_style, copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006686 garray_T *gap;
Bram Moolenaar33570922005-01-25 22:26:29 +00006687 list_T *l;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006688 char_u *sep;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006689 int echo_style;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006690 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006691{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006692 garray_T join_ga;
6693 int retval;
6694 join_T *p;
6695 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006696
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006697 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6698 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6699
6700 /* Dispose each item in join_ga. */
6701 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006702 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006703 p = (join_T *)join_ga.ga_data;
6704 for (i = 0; i < join_ga.ga_len; ++i)
6705 {
6706 vim_free(p->tofree);
6707 ++p;
6708 }
6709 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006710 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006711
6712 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006713}
6714
6715/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006716 * Garbage collection for lists and dictionaries.
6717 *
6718 * We use reference counts to be able to free most items right away when they
6719 * are no longer used. But for composite items it's possible that it becomes
6720 * unused while the reference count is > 0: When there is a recursive
6721 * reference. Example:
6722 * :let l = [1, 2, 3]
6723 * :let d = {9: l}
6724 * :let l[1] = d
6725 *
6726 * Since this is quite unusual we handle this with garbage collection: every
6727 * once in a while find out which lists and dicts are not referenced from any
6728 * variable.
6729 *
6730 * Here is a good reference text about garbage collection (refers to Python
6731 * but it applies to all reference-counting mechanisms):
6732 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006733 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006734
6735/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006736 * Do garbage collection for lists and dicts.
6737 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006738 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006739 int
6740garbage_collect()
Bram Moolenaard9fba312005-06-26 22:34:35 +00006741{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006742 int copyID;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006743 buf_T *buf;
6744 win_T *wp;
6745 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006746 funccall_T *fc, **pfc;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006747 int did_free;
6748 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006749#ifdef FEAT_WINDOWS
6750 tabpage_T *tp;
6751#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006752
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006753 /* Only do this once. */
6754 want_garbage_collect = FALSE;
6755 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006756 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006757
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006758 /* We advance by two because we add one for items referenced through
6759 * previous_funccal. */
6760 current_copyID += COPYID_INC;
6761 copyID = current_copyID;
6762
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006763 /*
6764 * 1. Go through all accessible variables and mark all lists and dicts
6765 * with copyID.
6766 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006767
6768 /* Don't free variables in the previous_funccal list unless they are only
6769 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006770 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006771 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6772 {
6773 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1);
6774 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1);
6775 }
6776
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006777 /* script-local variables */
6778 for (i = 1; i <= ga_scripts.ga_len; ++i)
6779 set_ref_in_ht(&SCRIPT_VARS(i), copyID);
6780
6781 /* buffer-local variables */
6782 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006783 set_ref_in_item(&buf->b_bufvar.di_tv, copyID);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006784
6785 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006786 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006787 set_ref_in_item(&wp->w_winvar.di_tv, copyID);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006788#ifdef FEAT_AUTOCMD
6789 if (aucmd_win != NULL)
6790 set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID);
6791#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006792
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006793#ifdef FEAT_WINDOWS
6794 /* tabpage-local variables */
6795 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar429fa852013-04-15 12:27:36 +02006796 set_ref_in_item(&tp->tp_winvar.di_tv, copyID);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006797#endif
6798
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006799 /* global variables */
6800 set_ref_in_ht(&globvarht, copyID);
6801
6802 /* function-local variables */
6803 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6804 {
6805 set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID);
6806 set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID);
6807 }
6808
Bram Moolenaard812df62008-11-09 12:46:09 +00006809 /* v: vars */
6810 set_ref_in_ht(&vimvarht, copyID);
6811
Bram Moolenaar1dced572012-04-05 16:54:08 +02006812#ifdef FEAT_LUA
6813 set_ref_in_lua(copyID);
6814#endif
6815
Bram Moolenaardb913952012-06-29 12:54:53 +02006816#ifdef FEAT_PYTHON
6817 set_ref_in_python(copyID);
6818#endif
6819
6820#ifdef FEAT_PYTHON3
6821 set_ref_in_python3(copyID);
6822#endif
6823
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006824 /*
6825 * 2. Free lists and dictionaries that are not referenced.
6826 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006827 did_free = free_unref_items(copyID);
6828
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006829 /*
6830 * 3. Check if any funccal can be freed now.
6831 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006832 for (pfc = &previous_funccal; *pfc != NULL; )
6833 {
6834 if (can_free_funccal(*pfc, copyID))
6835 {
6836 fc = *pfc;
6837 *pfc = fc->caller;
6838 free_funccal(fc, TRUE);
6839 did_free = TRUE;
6840 did_free_funccal = TRUE;
6841 }
6842 else
6843 pfc = &(*pfc)->caller;
6844 }
6845 if (did_free_funccal)
6846 /* When a funccal was freed some more items might be garbage
6847 * collected, so run again. */
6848 (void)garbage_collect();
6849
6850 return did_free;
6851}
6852
6853/*
6854 * Free lists and dictionaries that are no longer referenced.
6855 */
6856 static int
6857free_unref_items(copyID)
6858 int copyID;
6859{
6860 dict_T *dd;
6861 list_T *ll;
6862 int did_free = FALSE;
6863
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006866 */
6867 for (dd = first_dict; dd != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006868 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006870 /* Free the Dictionary and ordinary items it contains, but don't
6871 * recurse into Lists and Dictionaries, they will be in the list
6872 * of dicts or list of lists. */
6873 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874 did_free = TRUE;
6875
6876 /* restart, next dict may also have been freed */
6877 dd = first_dict;
6878 }
6879 else
6880 dd = dd->dv_used_next;
6881
6882 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883 * Go through the list of lists and free items without the copyID.
6884 * But don't free a list that has a watcher (used in a for loop), these
6885 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886 */
6887 for (ll = first_list; ll != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006888 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
6889 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006890 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006891 /* Free the List and ordinary items it contains, but don't recurse
6892 * into Lists and Dictionaries, they will be in the list of dicts
6893 * or list of lists. */
6894 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 did_free = TRUE;
6896
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00006897 /* restart, next list may also have been freed */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006898 ll = first_list;
6899 }
6900 else
6901 ll = ll->lv_used_next;
6902
6903 return did_free;
6904}
6905
6906/*
6907 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
6908 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006909 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910set_ref_in_ht(ht, copyID)
6911 hashtab_T *ht;
6912 int copyID;
6913{
6914 int todo;
6915 hashitem_T *hi;
6916
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006917 todo = (int)ht->ht_used;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006918 for (hi = ht->ht_array; todo > 0; ++hi)
6919 if (!HASHITEM_EMPTY(hi))
6920 {
6921 --todo;
6922 set_ref_in_item(&HI2DI(hi)->di_tv, copyID);
6923 }
6924}
6925
6926/*
6927 * Mark all lists and dicts referenced through list "l" with "copyID".
6928 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006929 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006930set_ref_in_list(l, copyID)
6931 list_T *l;
6932 int copyID;
6933{
6934 listitem_T *li;
6935
6936 for (li = l->lv_first; li != NULL; li = li->li_next)
6937 set_ref_in_item(&li->li_tv, copyID);
6938}
6939
6940/*
6941 * Mark all lists and dicts referenced through typval "tv" with "copyID".
6942 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006943 void
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944set_ref_in_item(tv, copyID)
6945 typval_T *tv;
6946 int copyID;
6947{
6948 dict_T *dd;
6949 list_T *ll;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006950
6951 switch (tv->v_type)
6952 {
6953 case VAR_DICT:
6954 dd = tv->vval.v_dict;
Bram Moolenaard812df62008-11-09 12:46:09 +00006955 if (dd != NULL && dd->dv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006956 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006957 /* Didn't see this dict yet. */
6958 dd->dv_copyID = copyID;
6959 set_ref_in_ht(&dd->dv_hashtab, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006960 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006961 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006962
6963 case VAR_LIST:
6964 ll = tv->vval.v_list;
Bram Moolenaard812df62008-11-09 12:46:09 +00006965 if (ll != NULL && ll->lv_copyID != copyID)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006966 {
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006967 /* Didn't see this list yet. */
6968 ll->lv_copyID = copyID;
6969 set_ref_in_list(ll, copyID);
Bram Moolenaard9fba312005-06-26 22:34:35 +00006970 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006971 break;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006972 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006973 return;
Bram Moolenaard9fba312005-06-26 22:34:35 +00006974}
6975
6976/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00006977 * Allocate an empty header for a dictionary.
6978 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00006979 dict_T *
Bram Moolenaar8c711452005-01-14 21:53:12 +00006980dict_alloc()
6981{
Bram Moolenaar33570922005-01-25 22:26:29 +00006982 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006983
Bram Moolenaar33570922005-01-25 22:26:29 +00006984 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006985 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006986 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006987 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 if (first_dict != NULL)
6989 first_dict->dv_used_prev = d;
6990 d->dv_used_next = first_dict;
6991 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006992 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993
Bram Moolenaar33570922005-01-25 22:26:29 +00006994 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006995 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02006996 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006997 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006998 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00006999 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007000 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007001}
7002
7003/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007004 * Allocate an empty dict for a return value.
7005 * Returns OK or FAIL.
7006 */
7007 static int
7008rettv_dict_alloc(rettv)
7009 typval_T *rettv;
7010{
7011 dict_T *d = dict_alloc();
7012
7013 if (d == NULL)
7014 return FAIL;
7015
7016 rettv->vval.v_dict = d;
7017 rettv->v_type = VAR_DICT;
7018 ++d->dv_refcount;
7019 return OK;
7020}
7021
7022
7023/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007024 * Unreference a Dictionary: decrement the reference count and free it when it
7025 * becomes zero.
7026 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007027 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007028dict_unref(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007029 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007030{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007031 if (d != NULL && --d->dv_refcount <= 0)
7032 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007033}
7034
7035/*
7036 * Free a Dictionary, including all items it contains.
7037 * Ignores the reference count.
7038 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007039 void
Bram Moolenaar685295c2006-10-15 20:37:38 +00007040dict_free(d, recurse)
7041 dict_T *d;
7042 int recurse; /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007043{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007044 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007045 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007046 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007047
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 /* Remove the dict from the list of dicts for garbage collection. */
7049 if (d->dv_used_prev == NULL)
7050 first_dict = d->dv_used_next;
7051 else
7052 d->dv_used_prev->dv_used_next = d->dv_used_next;
7053 if (d->dv_used_next != NULL)
7054 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7055
7056 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007057 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007058 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007059 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007060 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007061 if (!HASHITEM_EMPTY(hi))
7062 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007063 /* Remove the item before deleting it, just in case there is
7064 * something recursive causing trouble. */
7065 di = HI2DI(hi);
7066 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007067 if (recurse || (di->di_tv.v_type != VAR_LIST
7068 && di->di_tv.v_type != VAR_DICT))
7069 clear_tv(&di->di_tv);
7070 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007071 --todo;
7072 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007073 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007074 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007075 vim_free(d);
7076}
7077
7078/*
7079 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007080 * The "key" is copied to the new item.
7081 * Note that the value of the item "di_tv" still needs to be initialized!
7082 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007083 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007084 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007085dictitem_alloc(key)
7086 char_u *key;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007087{
Bram Moolenaar33570922005-01-25 22:26:29 +00007088 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007089
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007090 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007091 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007092 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007093 STRCPY(di->di_key, key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007094 di->di_flags = 0;
7095 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007096 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007097}
7098
7099/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007100 * Make a copy of a Dictionary item.
7101 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007102 static dictitem_T *
Bram Moolenaare9a41262005-01-15 22:18:47 +00007103dictitem_copy(org)
Bram Moolenaar33570922005-01-25 22:26:29 +00007104 dictitem_T *org;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007105{
Bram Moolenaar33570922005-01-25 22:26:29 +00007106 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007107
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007108 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7109 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007110 if (di != NULL)
7111 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007112 STRCPY(di->di_key, org->di_key);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007113 di->di_flags = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007114 copy_tv(&org->di_tv, &di->di_tv);
7115 }
7116 return di;
7117}
7118
7119/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007120 * Remove item "item" from Dictionary "dict" and free it.
7121 */
7122 static void
7123dictitem_remove(dict, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007124 dict_T *dict;
7125 dictitem_T *item;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007126{
Bram Moolenaar33570922005-01-25 22:26:29 +00007127 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007128
Bram Moolenaar33570922005-01-25 22:26:29 +00007129 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007130 if (HASHITEM_EMPTY(hi))
7131 EMSG2(_(e_intern2), "dictitem_remove()");
7132 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007133 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007134 dictitem_free(item);
7135}
7136
7137/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007138 * Free a dict item. Also clears the value.
7139 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007140 void
Bram Moolenaar8c711452005-01-14 21:53:12 +00007141dictitem_free(item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007142 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007143{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007144 clear_tv(&item->di_tv);
7145 vim_free(item);
7146}
7147
7148/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007149 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7150 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007151 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007152 * Returns NULL when out of memory.
7153 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007154 static dict_T *
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007155dict_copy(orig, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007156 dict_T *orig;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007157 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007158 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007159{
Bram Moolenaar33570922005-01-25 22:26:29 +00007160 dict_T *copy;
7161 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007162 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007163 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007164
7165 if (orig == NULL)
7166 return NULL;
7167
7168 copy = dict_alloc();
7169 if (copy != NULL)
7170 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007171 if (copyID != 0)
7172 {
7173 orig->dv_copyID = copyID;
7174 orig->dv_copydict = copy;
7175 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007176 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007177 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007178 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007179 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007180 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007181 --todo;
7182
7183 di = dictitem_alloc(hi->hi_key);
7184 if (di == NULL)
7185 break;
7186 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007187 {
7188 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7189 copyID) == FAIL)
7190 {
7191 vim_free(di);
7192 break;
7193 }
7194 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007195 else
7196 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7197 if (dict_add(copy, di) == FAIL)
7198 {
7199 dictitem_free(di);
7200 break;
7201 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007202 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007203 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007204
Bram Moolenaare9a41262005-01-15 22:18:47 +00007205 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007206 if (todo > 0)
7207 {
7208 dict_unref(copy);
7209 copy = NULL;
7210 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007211 }
7212
7213 return copy;
7214}
7215
7216/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007217 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007218 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007219 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007220 int
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221dict_add(d, item)
Bram Moolenaar33570922005-01-25 22:26:29 +00007222 dict_T *d;
7223 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007224{
Bram Moolenaar33570922005-01-25 22:26:29 +00007225 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007226}
7227
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007229 * Add a number or string entry to dictionary "d".
7230 * When "str" is NULL use number "nr", otherwise use "str".
7231 * Returns FAIL when out of memory and when key already exists.
7232 */
7233 int
7234dict_add_nr_str(d, key, nr, str)
7235 dict_T *d;
7236 char *key;
7237 long nr;
7238 char_u *str;
7239{
7240 dictitem_T *item;
7241
7242 item = dictitem_alloc((char_u *)key);
7243 if (item == NULL)
7244 return FAIL;
7245 item->di_tv.v_lock = 0;
7246 if (str == NULL)
7247 {
7248 item->di_tv.v_type = VAR_NUMBER;
7249 item->di_tv.vval.v_number = nr;
7250 }
7251 else
7252 {
7253 item->di_tv.v_type = VAR_STRING;
7254 item->di_tv.vval.v_string = vim_strsave(str);
7255 }
7256 if (dict_add(d, item) == FAIL)
7257 {
7258 dictitem_free(item);
7259 return FAIL;
7260 }
7261 return OK;
7262}
7263
7264/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007265 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007266 * Returns FAIL when out of memory and when key already exists.
7267 */
7268 int
7269dict_add_list(d, key, list)
7270 dict_T *d;
7271 char *key;
7272 list_T *list;
7273{
7274 dictitem_T *item;
7275
7276 item = dictitem_alloc((char_u *)key);
7277 if (item == NULL)
7278 return FAIL;
7279 item->di_tv.v_lock = 0;
7280 item->di_tv.v_type = VAR_LIST;
7281 item->di_tv.vval.v_list = list;
7282 if (dict_add(d, item) == FAIL)
7283 {
7284 dictitem_free(item);
7285 return FAIL;
7286 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007287 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007288 return OK;
7289}
7290
7291/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007292 * Get the number of items in a Dictionary.
7293 */
7294 static long
7295dict_len(d)
Bram Moolenaar33570922005-01-25 22:26:29 +00007296 dict_T *d;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007297{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007298 if (d == NULL)
7299 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007300 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007301}
7302
7303/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304 * Find item "key[len]" in Dictionary "d".
7305 * If "len" is negative use strlen(key).
7306 * Returns NULL when not found.
7307 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007308 dictitem_T *
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007309dict_find(d, key, len)
Bram Moolenaar33570922005-01-25 22:26:29 +00007310 dict_T *d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007311 char_u *key;
7312 int len;
7313{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007314#define AKEYLEN 200
7315 char_u buf[AKEYLEN];
7316 char_u *akey;
7317 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007318 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007319
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007320 if (len < 0)
7321 akey = key;
7322 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007323 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 tofree = akey = vim_strnsave(key, len);
7325 if (akey == NULL)
7326 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007327 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007328 else
7329 {
7330 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007331 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007332 akey = buf;
7333 }
7334
Bram Moolenaar33570922005-01-25 22:26:29 +00007335 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007336 vim_free(tofree);
7337 if (HASHITEM_EMPTY(hi))
7338 return NULL;
7339 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007340}
7341
7342/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007343 * Get a string item from a dictionary.
7344 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007345 * Returns NULL if the entry doesn't exist or out of memory.
7346 */
7347 char_u *
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007348get_dict_string(d, key, save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007349 dict_T *d;
7350 char_u *key;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007351 int save;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007352{
7353 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007354 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007355
7356 di = dict_find(d, key, -1);
7357 if (di == NULL)
7358 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007359 s = get_tv_string(&di->di_tv);
7360 if (save && s != NULL)
7361 s = vim_strsave(s);
7362 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007363}
7364
7365/*
7366 * Get a number item from a dictionary.
7367 * Returns 0 if the entry doesn't exist or out of memory.
7368 */
7369 long
7370get_dict_number(d, key)
7371 dict_T *d;
7372 char_u *key;
7373{
7374 dictitem_T *di;
7375
7376 di = dict_find(d, key, -1);
7377 if (di == NULL)
7378 return 0;
7379 return get_tv_number(&di->di_tv);
7380}
7381
7382/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007383 * Return an allocated string with the string representation of a Dictionary.
7384 * May return NULL.
7385 */
7386 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007387dict2string(tv, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007388 typval_T *tv;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007389 int copyID;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007390{
7391 garray_T ga;
7392 int first = TRUE;
7393 char_u *tofree;
7394 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007396 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007397 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007399
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007401 return NULL;
7402 ga_init2(&ga, (int)sizeof(char), 80);
7403 ga_append(&ga, '{');
7404
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007405 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007406 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007408 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007410 --todo;
7411
7412 if (first)
7413 first = FALSE;
7414 else
7415 ga_concat(&ga, (char_u *)", ");
7416
7417 tofree = string_quote(hi->hi_key, FALSE);
7418 if (tofree != NULL)
7419 {
7420 ga_concat(&ga, tofree);
7421 vim_free(tofree);
7422 }
7423 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007424 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425 if (s != NULL)
7426 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007427 vim_free(tofree);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007428 if (s == NULL)
7429 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007430 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007431 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007432 if (todo > 0)
7433 {
7434 vim_free(ga.ga_data);
7435 return NULL;
7436 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437
7438 ga_append(&ga, '}');
7439 ga_append(&ga, NUL);
7440 return (char_u *)ga.ga_data;
7441}
7442
7443/*
7444 * Allocate a variable for a Dictionary and fill it from "*arg".
7445 * Return OK or FAIL. Returns NOTDONE for {expr}.
7446 */
7447 static int
7448get_dict_tv(arg, rettv, evaluate)
7449 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007450 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007451 int evaluate;
7452{
Bram Moolenaar33570922005-01-25 22:26:29 +00007453 dict_T *d = NULL;
7454 typval_T tvkey;
7455 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007456 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007457 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007458 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007459 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007460
7461 /*
7462 * First check if it's not a curly-braces thing: {expr}.
7463 * Must do this without evaluating, otherwise a function may be called
7464 * twice. Unfortunately this means we need to call eval1() twice for the
7465 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007466 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007467 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 if (*start != '}')
7469 {
7470 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7471 return FAIL;
7472 if (*start == '}')
7473 return NOTDONE;
7474 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007475
7476 if (evaluate)
7477 {
7478 d = dict_alloc();
7479 if (d == NULL)
7480 return FAIL;
7481 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007482 tvkey.v_type = VAR_UNKNOWN;
7483 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484
7485 *arg = skipwhite(*arg + 1);
7486 while (**arg != '}' && **arg != NUL)
7487 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007488 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489 goto failret;
7490 if (**arg != ':')
7491 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007492 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007493 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 goto failret;
7495 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007496 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007498 key = get_tv_string_buf_chk(&tvkey, buf);
7499 if (key == NULL || *key == NUL)
7500 {
7501 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7502 if (key != NULL)
7503 EMSG(_(e_emptykey));
7504 clear_tv(&tvkey);
7505 goto failret;
7506 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007507 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508
7509 *arg = skipwhite(*arg + 1);
7510 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7511 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007512 if (evaluate)
7513 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007514 goto failret;
7515 }
7516 if (evaluate)
7517 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007518 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007519 if (item != NULL)
7520 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007521 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007522 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007523 clear_tv(&tv);
7524 goto failret;
7525 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007526 item = dictitem_alloc(key);
7527 clear_tv(&tvkey);
7528 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007531 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007532 if (dict_add(d, item) == FAIL)
7533 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007534 }
7535 }
7536
7537 if (**arg == '}')
7538 break;
7539 if (**arg != ',')
7540 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007541 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007542 goto failret;
7543 }
7544 *arg = skipwhite(*arg + 1);
7545 }
7546
7547 if (**arg != '}')
7548 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007549 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550failret:
7551 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007552 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007553 return FAIL;
7554 }
7555
7556 *arg = skipwhite(*arg + 1);
7557 if (evaluate)
7558 {
7559 rettv->v_type = VAR_DICT;
7560 rettv->vval.v_dict = d;
7561 ++d->dv_refcount;
7562 }
7563
7564 return OK;
7565}
7566
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007568 * Return a string with the string representation of a variable.
7569 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007570 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007571 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007572 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007573 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007574 */
7575 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007576echo_string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 typval_T *tv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007578 char_u **tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007579 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007580 int copyID;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007581{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007582 static int recurse = 0;
7583 char_u *r = NULL;
7584
Bram Moolenaar33570922005-01-25 22:26:29 +00007585 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007586 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007587 EMSG(_("E724: variable nested too deep for displaying"));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007588 *tofree = NULL;
7589 return NULL;
7590 }
7591 ++recurse;
7592
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007593 switch (tv->v_type)
7594 {
7595 case VAR_FUNC:
7596 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007597 r = tv->vval.v_string;
7598 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007599
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007600 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007601 if (tv->vval.v_list == NULL)
7602 {
7603 *tofree = NULL;
7604 r = NULL;
7605 }
7606 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7607 {
7608 *tofree = NULL;
7609 r = (char_u *)"[...]";
7610 }
7611 else
7612 {
7613 tv->vval.v_list->lv_copyID = copyID;
7614 *tofree = list2string(tv, copyID);
7615 r = *tofree;
7616 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007617 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007618
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007620 if (tv->vval.v_dict == NULL)
7621 {
7622 *tofree = NULL;
7623 r = NULL;
7624 }
7625 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7626 {
7627 *tofree = NULL;
7628 r = (char_u *)"{...}";
7629 }
7630 else
7631 {
7632 tv->vval.v_dict->dv_copyID = copyID;
7633 *tofree = dict2string(tv, copyID);
7634 r = *tofree;
7635 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007636 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007637
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007638 case VAR_STRING:
7639 case VAR_NUMBER:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007640 *tofree = NULL;
7641 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007642 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007643
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007644#ifdef FEAT_FLOAT
7645 case VAR_FLOAT:
7646 *tofree = NULL;
7647 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7648 r = numbuf;
7649 break;
7650#endif
7651
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007652 default:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007653 EMSG2(_(e_intern2), "echo_string()");
Bram Moolenaare9a41262005-01-15 22:18:47 +00007654 *tofree = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007655 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007656
7657 --recurse;
7658 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007659}
7660
7661/*
7662 * Return a string with the string representation of a variable.
7663 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7664 * "numbuf" is used for a number.
7665 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007666 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007667 */
7668 static char_u *
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007669tv2string(tv, tofree, numbuf, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +00007670 typval_T *tv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007671 char_u **tofree;
7672 char_u *numbuf;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007673 int copyID;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007674{
7675 switch (tv->v_type)
7676 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007677 case VAR_FUNC:
7678 *tofree = string_quote(tv->vval.v_string, TRUE);
7679 return *tofree;
7680 case VAR_STRING:
7681 *tofree = string_quote(tv->vval.v_string, FALSE);
7682 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007683#ifdef FEAT_FLOAT
7684 case VAR_FLOAT:
7685 *tofree = NULL;
7686 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7687 return numbuf;
7688#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007689 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007690 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007691 case VAR_DICT:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007692 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007693 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007694 EMSG2(_(e_intern2), "tv2string()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007695 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007696 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007697}
7698
7699/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007700 * Return string "str" in ' quotes, doubling ' characters.
7701 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007703 */
7704 static char_u *
7705string_quote(str, function)
7706 char_u *str;
7707 int function;
7708{
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007710 char_u *p, *r, *s;
7711
Bram Moolenaar33570922005-01-25 22:26:29 +00007712 len = (function ? 13 : 3);
7713 if (str != NULL)
7714 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007715 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007716 for (p = str; *p != NUL; mb_ptr_adv(p))
7717 if (*p == '\'')
7718 ++len;
7719 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007720 s = r = alloc(len);
7721 if (r != NULL)
7722 {
7723 if (function)
7724 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007726 r += 10;
7727 }
7728 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007730 if (str != NULL)
7731 for (p = str; *p != NUL; )
7732 {
7733 if (*p == '\'')
7734 *r++ = '\'';
7735 MB_COPY_CHAR(p, r);
7736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007738 if (function)
7739 *r++ = ')';
7740 *r++ = NUL;
7741 }
7742 return s;
7743}
7744
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007745#ifdef FEAT_FLOAT
7746/*
7747 * Convert the string "text" to a floating point number.
7748 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7749 * this always uses a decimal point.
7750 * Returns the length of the text that was consumed.
7751 */
7752 static int
7753string2float(text, value)
7754 char_u *text;
7755 float_T *value; /* result stored here */
7756{
7757 char *s = (char *)text;
7758 float_T f;
7759
7760 f = strtod(s, &s);
7761 *value = f;
7762 return (int)((char_u *)s - text);
7763}
7764#endif
7765
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007766/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 * Get the value of an environment variable.
7768 * "arg" is pointing to the '$'. It is advanced to after the name.
7769 * If the environment variable was not set, silently assume it is empty.
7770 * Always return OK.
7771 */
7772 static int
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007773get_env_tv(arg, rettv, evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 char_u **arg;
Bram Moolenaar33570922005-01-25 22:26:29 +00007775 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 int evaluate;
7777{
7778 char_u *string = NULL;
7779 int len;
7780 int cc;
7781 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007782 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783
7784 ++*arg;
7785 name = *arg;
7786 len = get_env_len(arg);
7787 if (evaluate)
7788 {
7789 if (len != 0)
7790 {
7791 cc = name[len];
7792 name[len] = NUL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00007793 /* first try vim_getenv(), fast for normal environment vars */
7794 string = vim_getenv(name, &mustfree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 if (string != NULL && *string != NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007796 {
7797 if (!mustfree)
7798 string = vim_strsave(string);
7799 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007800 else
7801 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00007802 if (mustfree)
7803 vim_free(string);
7804
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 /* next try expanding things like $VIM and ${HOME} */
7806 string = expand_env_save(name - 1);
7807 if (string != NULL && *string == '$')
7808 {
7809 vim_free(string);
7810 string = NULL;
7811 }
7812 }
7813 name[len] = cc;
7814 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007815 rettv->v_type = VAR_STRING;
7816 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817 }
7818
7819 return OK;
7820}
7821
7822/*
7823 * Array with names and number of arguments of all internal functions
7824 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
7825 */
7826static struct fst
7827{
7828 char *f_name; /* function name */
7829 char f_min_argc; /* minimal number of arguments */
7830 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar33570922005-01-25 22:26:29 +00007831 void (*f_func) __ARGS((typval_T *args, typval_T *rvar));
Bram Moolenaarbae0c162007-05-10 19:30:25 +00007832 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007833} functions[] =
7834{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007835#ifdef FEAT_FLOAT
7836 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007837 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007838#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00007839 {"add", 2, 2, f_add},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007840 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 {"append", 2, 2, f_append},
7842 {"argc", 0, 0, f_argc},
7843 {"argidx", 0, 0, f_argidx},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00007844 {"argv", 0, 1, f_argv},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007845#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007846 {"asin", 1, 1, f_asin}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007847 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007848 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007851 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 {"bufexists", 1, 1, f_bufexists},
7853 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
7854 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
7855 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
7856 {"buflisted", 1, 1, f_buflisted},
7857 {"bufloaded", 1, 1, f_bufloaded},
7858 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00007859 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860 {"bufwinnr", 1, 1, f_bufwinnr},
7861 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00007862 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007863 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007864#ifdef FEAT_FLOAT
7865 {"ceil", 1, 1, f_ceil},
7866#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00007867 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01007868 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00007870 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007872#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00007873 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00007874 {"complete_add", 1, 1, f_complete_add},
7875 {"complete_check", 0, 0, f_complete_check},
7876#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007878 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007879#ifdef FEAT_FLOAT
7880 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007881 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007882#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007883 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007884 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00007885 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007886 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 {"delete", 1, 1, f_delete},
7888 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00007889 {"diff_filler", 1, 1, f_diff_filler},
7890 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00007891 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007893 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 {"eventhandler", 0, 0, f_eventhandler},
7895 {"executable", 1, 1, f_executable},
7896 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007897#ifdef FEAT_FLOAT
7898 {"exp", 1, 1, f_exp},
7899#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007900 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007901 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00007902 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
7904 {"filereadable", 1, 1, f_filereadable},
7905 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007906 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00007907 {"finddir", 1, 3, f_finddir},
7908 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007909#ifdef FEAT_FLOAT
7910 {"float2nr", 1, 1, f_float2nr},
7911 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007912 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007913#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00007914 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 {"fnamemodify", 2, 2, f_fnamemodify},
7916 {"foldclosed", 1, 1, f_foldclosed},
7917 {"foldclosedend", 1, 1, f_foldclosedend},
7918 {"foldlevel", 1, 1, f_foldlevel},
7919 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00007920 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007921 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007922 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00007923 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007924 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00007925 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007926 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007927 {"getchar", 0, 1, f_getchar},
7928 {"getcharmod", 0, 0, f_getcharmod},
7929 {"getcmdline", 0, 0, f_getcmdline},
7930 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007931 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932 {"getcwd", 0, 0, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00007933 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007934 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {"getfsize", 1, 1, f_getfsize},
7936 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00007937 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00007938 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00007939 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00007940 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00007941 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00007942 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00007943 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00007944 {"getreg", 0, 2, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007945 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007946 {"gettabvar", 2, 3, f_gettabvar},
7947 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007948 {"getwinposx", 0, 0, f_getwinposx},
7949 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01007950 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaar146e9c32012-03-07 19:18:23 +01007951 {"glob", 1, 3, f_glob},
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +00007952 {"globpath", 2, 3, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007953 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00007954 {"has_key", 2, 2, f_has_key},
Bram Moolenaard267b9c2007-04-26 15:06:45 +00007955 {"haslocaldir", 0, 0, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007956 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 {"highlightID", 1, 1, f_hlID}, /* obsolete */
7958 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
7959 {"histadd", 2, 2, f_histadd},
7960 {"histdel", 1, 2, f_histdel},
7961 {"histget", 1, 2, f_histget},
7962 {"histnr", 1, 1, f_histnr},
7963 {"hlID", 1, 1, f_hlID},
7964 {"hlexists", 1, 1, f_hlexists},
7965 {"hostname", 0, 0, f_hostname},
7966 {"iconv", 3, 3, f_iconv},
7967 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007968 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00007969 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00007971 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 {"inputrestore", 0, 0, f_inputrestore},
7973 {"inputsave", 0, 0, f_inputsave},
7974 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007975 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01007976 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007977 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007978 {"islocked", 1, 1, f_islocked},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007979 {"items", 1, 1, f_items},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007980 {"join", 1, 2, f_join},
Bram Moolenaar8c711452005-01-14 21:53:12 +00007981 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007983 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984 {"libcall", 3, 3, f_libcall},
7985 {"libcallnr", 3, 3, f_libcallnr},
7986 {"line", 1, 1, f_line},
7987 {"line2byte", 1, 1, f_line2byte},
7988 {"lispindent", 1, 1, f_lispindent},
7989 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007990#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02007991 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007992 {"log10", 1, 1, f_log10},
7993#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02007994#ifdef FEAT_LUA
7995 {"luaeval", 1, 2, f_luaeval},
7996#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007997 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02007998 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00007999 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008000 {"match", 2, 4, f_match},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008001 {"matchadd", 2, 4, f_matchadd},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008002 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008003 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008004 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008005 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008006 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008007 {"max", 1, 1, f_max},
8008 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008009#ifdef vim_mkdir
8010 {"mkdir", 1, 3, f_mkdir},
8011#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008012 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008013#ifdef FEAT_MZSCHEME
8014 {"mzeval", 1, 1, f_mzeval},
8015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008017 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008018 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008019 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008020#ifdef FEAT_FLOAT
8021 {"pow", 2, 2, f_pow},
8022#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008024 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008025 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008026#ifdef FEAT_PYTHON3
8027 {"py3eval", 1, 1, f_py3eval},
8028#endif
8029#ifdef FEAT_PYTHON
8030 {"pyeval", 1, 1, f_pyeval},
8031#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008032 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008033 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008034 {"reltime", 0, 2, f_reltime},
8035 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 {"remote_expr", 2, 3, f_remote_expr},
8037 {"remote_foreground", 1, 1, f_remote_foreground},
8038 {"remote_peek", 1, 2, f_remote_peek},
8039 {"remote_read", 1, 1, f_remote_read},
8040 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008041 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008043 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008045 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008046#ifdef FEAT_FLOAT
8047 {"round", 1, 1, f_round},
8048#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008049 {"screenattr", 2, 2, f_screenattr},
8050 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008051 {"screencol", 0, 0, f_screencol},
8052 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008053 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008054 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008055 {"searchpair", 3, 7, f_searchpair},
8056 {"searchpairpos", 3, 7, f_searchpairpos},
8057 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008058 {"server2client", 2, 2, f_server2client},
8059 {"serverlist", 0, 0, f_serverlist},
8060 {"setbufvar", 3, 3, f_setbufvar},
8061 {"setcmdpos", 1, 1, f_setcmdpos},
8062 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008063 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008064 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008065 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008066 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008067 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008068 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008069 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008071#ifdef FEAT_CRYPT
8072 {"sha256", 1, 1, f_sha256},
8073#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008074 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008075 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008077#ifdef FEAT_FLOAT
8078 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008079 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008080#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008081 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008082 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008083 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008084 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008085 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008086#ifdef FEAT_FLOAT
8087 {"sqrt", 1, 1, f_sqrt},
8088 {"str2float", 1, 1, f_str2float},
8089#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008090 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008091 {"strchars", 1, 1, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008092 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093#ifdef HAVE_STRFTIME
8094 {"strftime", 1, 2, f_strftime},
8095#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008096 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008097 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 {"strlen", 1, 1, f_strlen},
8099 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008100 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008102 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 {"submatch", 1, 1, f_submatch},
8104 {"substitute", 4, 4, f_substitute},
8105 {"synID", 3, 3, f_synID},
8106 {"synIDattr", 2, 3, f_synIDattr},
8107 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008108 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008109 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008110 {"system", 1, 2, f_system},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008111 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008112 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008113 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008114 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008115 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008116#ifdef FEAT_FLOAT
8117 {"tan", 1, 1, f_tan},
8118 {"tanh", 1, 1, f_tanh},
8119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008121 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 {"tolower", 1, 1, f_tolower},
8123 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008124 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008125#ifdef FEAT_FLOAT
8126 {"trunc", 1, 1, f_trunc},
8127#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008129 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008130 {"undotree", 0, 0, f_undotree},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008131 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 {"virtcol", 1, 1, f_virtcol},
8133 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008134 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"winbufnr", 1, 1, f_winbufnr},
8136 {"wincol", 0, 0, f_wincol},
8137 {"winheight", 1, 1, f_winheight},
8138 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008139 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008140 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008141 {"winrestview", 1, 1, f_winrestview},
8142 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008144 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008145 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146};
8147
8148#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8149
8150/*
8151 * Function given to ExpandGeneric() to obtain the list of internal
8152 * or user defined function names.
8153 */
8154 char_u *
8155get_function_name(xp, idx)
8156 expand_T *xp;
8157 int idx;
8158{
8159 static int intidx = -1;
8160 char_u *name;
8161
8162 if (idx == 0)
8163 intidx = -1;
8164 if (intidx < 0)
8165 {
8166 name = get_user_func_name(xp, idx);
8167 if (name != NULL)
8168 return name;
8169 }
8170 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8171 {
8172 STRCPY(IObuff, functions[intidx].f_name);
8173 STRCAT(IObuff, "(");
8174 if (functions[intidx].f_max_argc == 0)
8175 STRCAT(IObuff, ")");
8176 return IObuff;
8177 }
8178
8179 return NULL;
8180}
8181
8182/*
8183 * Function given to ExpandGeneric() to obtain the list of internal or
8184 * user defined variable or function names.
8185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 char_u *
8187get_expr_name(xp, idx)
8188 expand_T *xp;
8189 int idx;
8190{
8191 static int intidx = -1;
8192 char_u *name;
8193
8194 if (idx == 0)
8195 intidx = -1;
8196 if (intidx < 0)
8197 {
8198 name = get_function_name(xp, idx);
8199 if (name != NULL)
8200 return name;
8201 }
8202 return get_user_var_name(xp, ++intidx);
8203}
8204
8205#endif /* FEAT_CMDL_COMPL */
8206
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008207#if defined(EBCDIC) || defined(PROTO)
8208/*
8209 * Compare struct fst by function name.
8210 */
8211 static int
8212compare_func_name(s1, s2)
8213 const void *s1;
8214 const void *s2;
8215{
8216 struct fst *p1 = (struct fst *)s1;
8217 struct fst *p2 = (struct fst *)s2;
8218
8219 return STRCMP(p1->f_name, p2->f_name);
8220}
8221
8222/*
8223 * Sort the function table by function name.
8224 * The sorting of the table above is ASCII dependant.
8225 * On machines using EBCDIC we have to sort it.
8226 */
8227 static void
8228sortFunctions()
8229{
8230 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8231
8232 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8233}
8234#endif
8235
8236
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237/*
8238 * Find internal function in table above.
8239 * Return index, or -1 if not found
8240 */
8241 static int
8242find_internal_func(name)
8243 char_u *name; /* name of the function */
8244{
8245 int first = 0;
8246 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8247 int cmp;
8248 int x;
8249
8250 /*
8251 * Find the function name in the table. Binary search.
8252 */
8253 while (first <= last)
8254 {
8255 x = first + ((unsigned)(last - first) >> 1);
8256 cmp = STRCMP(name, functions[x].f_name);
8257 if (cmp < 0)
8258 last = x - 1;
8259 else if (cmp > 0)
8260 first = x + 1;
8261 else
8262 return x;
8263 }
8264 return -1;
8265}
8266
8267/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008268 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8269 * name it contains, otherwise return "name".
8270 */
8271 static char_u *
8272deref_func_name(name, lenp)
8273 char_u *name;
8274 int *lenp;
8275{
Bram Moolenaar33570922005-01-25 22:26:29 +00008276 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008277 int cc;
8278
8279 cc = name[*lenp];
8280 name[*lenp] = NUL;
Bram Moolenaara7043832005-01-21 11:56:39 +00008281 v = find_var(name, NULL);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008282 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008283 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008284 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008285 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008286 {
8287 *lenp = 0;
8288 return (char_u *)""; /* just in case */
8289 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008290 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008291 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008292 }
8293
8294 return name;
8295}
8296
8297/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 * Allocate a variable for the result of a function.
8299 * Return OK or FAIL.
8300 */
8301 static int
Bram Moolenaare9a41262005-01-15 22:18:47 +00008302get_func_tv(name, len, rettv, arg, firstline, lastline, doesrange,
8303 evaluate, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 char_u *name; /* name of the function */
8305 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008306 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 char_u **arg; /* argument, pointing to the '(' */
8308 linenr_T firstline; /* first line of range */
8309 linenr_T lastline; /* last line of range */
8310 int *doesrange; /* return: function handled range */
8311 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008312 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313{
8314 char_u *argp;
8315 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008316 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 int argcount = 0; /* number of arguments found */
8318
8319 /*
8320 * Get the arguments.
8321 */
8322 argp = *arg;
8323 while (argcount < MAX_FUNC_ARGS)
8324 {
8325 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8326 if (*argp == ')' || *argp == ',' || *argp == NUL)
8327 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8329 {
8330 ret = FAIL;
8331 break;
8332 }
8333 ++argcount;
8334 if (*argp != ',')
8335 break;
8336 }
8337 if (*argp == ')')
8338 ++argp;
8339 else
8340 ret = FAIL;
8341
8342 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008343 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008344 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008346 {
8347 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008348 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008349 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008350 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352
8353 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008354 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355
8356 *arg = skipwhite(argp);
8357 return ret;
8358}
8359
8360
8361/*
8362 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008363 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008364 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 */
8366 static int
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008367call_func(funcname, len, rettv, argcount, argvars, firstline, lastline,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008368 doesrange, evaluate, selfdict)
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008369 char_u *funcname; /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +00008371 typval_T *rettv; /* return value goes here */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 int argcount; /* number of "argvars" */
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008373 typval_T *argvars; /* vars for arguments, must have "argcount"
8374 PLUS ONE elements! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 linenr_T firstline; /* first line of range */
8376 linenr_T lastline; /* last line of range */
8377 int *doesrange; /* return: function handled range */
8378 int evaluate;
Bram Moolenaar33570922005-01-25 22:26:29 +00008379 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380{
8381 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382#define ERROR_UNKNOWN 0
8383#define ERROR_TOOMANY 1
8384#define ERROR_TOOFEW 2
8385#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008386#define ERROR_DICT 4
8387#define ERROR_NONE 5
8388#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 int error = ERROR_NONE;
8390 int i;
8391 int llen;
8392 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393#define FLEN_FIXED 40
8394 char_u fname_buf[FLEN_FIXED + 1];
8395 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008396 char_u *name;
8397
8398 /* Make a copy of the name, if it comes from a funcref variable it could
8399 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008400 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008401 if (name == NULL)
8402 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403
8404 /*
8405 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8406 * Change <SNR>123_name() to K_SNR 123_name().
8407 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 llen = eval_fname_script(name);
8410 if (llen > 0)
8411 {
8412 fname_buf[0] = K_SPECIAL;
8413 fname_buf[1] = KS_EXTRA;
8414 fname_buf[2] = (int)KE_SNR;
8415 i = 3;
8416 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8417 {
8418 if (current_SID <= 0)
8419 error = ERROR_SCRIPT;
8420 else
8421 {
8422 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8423 i = (int)STRLEN(fname_buf);
8424 }
8425 }
8426 if (i + STRLEN(name + llen) < FLEN_FIXED)
8427 {
8428 STRCPY(fname_buf + i, name + llen);
8429 fname = fname_buf;
8430 }
8431 else
8432 {
8433 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8434 if (fname == NULL)
8435 error = ERROR_OTHER;
8436 else
8437 {
8438 mch_memmove(fname, fname_buf, (size_t)i);
8439 STRCPY(fname + i, name + llen);
8440 }
8441 }
8442 }
8443 else
8444 fname = name;
8445
8446 *doesrange = FALSE;
8447
8448
8449 /* execute the function if no errors detected and executing */
8450 if (evaluate && error == ERROR_NONE)
8451 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008452 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8453 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 error = ERROR_UNKNOWN;
8455
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008456 if (!builtin_function(fname))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457 {
8458 /*
8459 * User defined function.
8460 */
8461 fp = find_func(fname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008462
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008464 /* Trigger FuncUndefined event, may load the function. */
8465 if (fp == NULL
8466 && apply_autocmds(EVENT_FUNCUNDEFINED,
8467 fname, fname, TRUE, NULL)
8468 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008470 /* executed an autocommand, search for the function again */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471 fp = find_func(fname);
8472 }
8473#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008474 /* Try loading a package. */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00008475 if (fp == NULL && script_autoload(fname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008476 {
8477 /* loaded a package, search for the function again */
8478 fp = find_func(fname);
8479 }
8480
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 if (fp != NULL)
8482 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008483 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008485 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008487 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008489 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008490 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 else
8492 {
8493 /*
8494 * Call the user function.
8495 * Save and restore search patterns, script variables and
8496 * redo buffer.
8497 */
8498 save_search_patterns();
8499 saveRedobuff();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008500 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008501 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008502 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008503 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8504 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8505 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008506 /* Function was unreferenced while being used, free it
8507 * now. */
8508 func_free(fp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 restoreRedobuff();
8510 restore_search_patterns();
8511 error = ERROR_NONE;
8512 }
8513 }
8514 }
8515 else
8516 {
8517 /*
8518 * Find the function name in the table, call its implementation.
8519 */
8520 i = find_internal_func(fname);
8521 if (i >= 0)
8522 {
8523 if (argcount < functions[i].f_min_argc)
8524 error = ERROR_TOOFEW;
8525 else if (argcount > functions[i].f_max_argc)
8526 error = ERROR_TOOMANY;
8527 else
8528 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008529 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008530 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 error = ERROR_NONE;
8532 }
8533 }
8534 }
8535 /*
8536 * The function call (or "FuncUndefined" autocommand sequence) might
8537 * have been aborted by an error, an interrupt, or an explicitly thrown
8538 * exception that has not been caught so far. This situation can be
8539 * tested for by calling aborting(). For an error in an internal
8540 * function or for the "E132" error in call_user_func(), however, the
8541 * throw point at which the "force_abort" flag (temporarily reset by
8542 * emsg()) is normally updated has not been reached yet. We need to
8543 * update that flag first to make aborting() reliable.
8544 */
8545 update_force_abort();
8546 }
8547 if (error == ERROR_NONE)
8548 ret = OK;
8549
8550 /*
8551 * Report an error unless the argument evaluation or function call has been
8552 * cancelled due to an aborting error, an interrupt, or an exception.
8553 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008554 if (!aborting())
8555 {
8556 switch (error)
8557 {
8558 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008559 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008560 break;
8561 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008562 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008563 break;
8564 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008565 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008566 name);
8567 break;
8568 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008569 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008570 name);
8571 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008572 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008573 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008574 name);
8575 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008576 }
8577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578
Bram Moolenaar071d4272004-06-13 20:20:40 +00008579 if (fname != name && fname != fname_buf)
8580 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008581 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582
8583 return ret;
8584}
8585
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008586/*
8587 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008588 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008589 */
8590 static void
Bram Moolenaar89d40322006-08-29 15:30:07 +00008591emsg_funcname(ermsg, name)
8592 char *ermsg;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008593 char_u *name;
8594{
8595 char_u *p;
8596
8597 if (*name == K_SPECIAL)
8598 p = concat_str((char_u *)"<SNR>", name + 3);
8599 else
8600 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008601 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008602 if (p != name)
8603 vim_free(p);
8604}
8605
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008606/*
8607 * Return TRUE for a non-zero Number and a non-empty String.
8608 */
8609 static int
8610non_zero_arg(argvars)
8611 typval_T *argvars;
8612{
8613 return ((argvars[0].v_type == VAR_NUMBER
8614 && argvars[0].vval.v_number != 0)
8615 || (argvars[0].v_type == VAR_STRING
8616 && argvars[0].vval.v_string != NULL
8617 && *argvars[0].vval.v_string != NUL));
8618}
8619
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620/*********************************************
8621 * Implementation of the built-in functions
8622 */
8623
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008624#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008625static int get_float_arg __ARGS((typval_T *argvars, float_T *f));
8626
8627/*
8628 * Get the float value of "argvars[0]" into "f".
8629 * Returns FAIL when the argument is not a Number or Float.
8630 */
8631 static int
8632get_float_arg(argvars, f)
8633 typval_T *argvars;
8634 float_T *f;
8635{
8636 if (argvars[0].v_type == VAR_FLOAT)
8637 {
8638 *f = argvars[0].vval.v_float;
8639 return OK;
8640 }
8641 if (argvars[0].v_type == VAR_NUMBER)
8642 {
8643 *f = (float_T)argvars[0].vval.v_number;
8644 return OK;
8645 }
8646 EMSG(_("E808: Number or Float required"));
8647 return FAIL;
8648}
8649
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008650/*
8651 * "abs(expr)" function
8652 */
8653 static void
8654f_abs(argvars, rettv)
8655 typval_T *argvars;
8656 typval_T *rettv;
8657{
8658 if (argvars[0].v_type == VAR_FLOAT)
8659 {
8660 rettv->v_type = VAR_FLOAT;
8661 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
8662 }
8663 else
8664 {
8665 varnumber_T n;
8666 int error = FALSE;
8667
8668 n = get_tv_number_chk(&argvars[0], &error);
8669 if (error)
8670 rettv->vval.v_number = -1;
8671 else if (n > 0)
8672 rettv->vval.v_number = n;
8673 else
8674 rettv->vval.v_number = -n;
8675 }
8676}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008677
8678/*
8679 * "acos()" function
8680 */
8681 static void
8682f_acos(argvars, rettv)
8683 typval_T *argvars;
8684 typval_T *rettv;
8685{
8686 float_T f;
8687
8688 rettv->v_type = VAR_FLOAT;
8689 if (get_float_arg(argvars, &f) == OK)
8690 rettv->vval.v_float = acos(f);
8691 else
8692 rettv->vval.v_float = 0.0;
8693}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008694#endif
8695
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008697 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 */
8699 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +00008700f_add(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008701 typval_T *argvars;
8702 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703{
Bram Moolenaar33570922005-01-25 22:26:29 +00008704 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008706 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008707 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008709 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +02008710 && !tv_check_lock(l->lv_lock, (char_u *)_("add() argument"))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008711 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008712 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008713 }
8714 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00008715 EMSG(_(e_listreq));
8716}
8717
8718/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008719 * "and(expr, expr)" function
8720 */
8721 static void
8722f_and(argvars, rettv)
8723 typval_T *argvars;
8724 typval_T *rettv;
8725{
8726 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
8727 & get_tv_number_chk(&argvars[1], NULL);
8728}
8729
8730/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00008731 * "append(lnum, string/list)" function
8732 */
8733 static void
8734f_append(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008735 typval_T *argvars;
8736 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008737{
8738 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008739 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00008740 list_T *l = NULL;
8741 listitem_T *li = NULL;
8742 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00008743 long added = 0;
8744
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008745 /* When coming here from Insert mode, sync undo, so that this can be
8746 * undone separately from what was previously inserted. */
8747 if (u_sync_once == 2)
8748 {
8749 u_sync_once = 1; /* notify that u_sync() was called */
8750 u_sync(TRUE);
8751 }
8752
Bram Moolenaar0d660222005-01-07 21:51:51 +00008753 lnum = get_tv_lnum(argvars);
8754 if (lnum >= 0
8755 && lnum <= curbuf->b_ml.ml_line_count
8756 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008757 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008758 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008759 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00008760 l = argvars[1].vval.v_list;
8761 if (l == NULL)
8762 return;
8763 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008764 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00008765 for (;;)
8766 {
8767 if (l == NULL)
8768 tv = &argvars[1]; /* append a string */
8769 else if (li == NULL)
8770 break; /* end of list */
8771 else
8772 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008773 line = get_tv_string_chk(tv);
8774 if (line == NULL) /* type error */
8775 {
8776 rettv->vval.v_number = 1; /* Failed */
8777 break;
8778 }
8779 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00008780 ++added;
8781 if (l == NULL)
8782 break;
8783 li = li->li_next;
8784 }
8785
8786 appended_lines_mark(lnum, added);
8787 if (curwin->w_cursor.lnum > lnum)
8788 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008789 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008790 else
8791 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792}
8793
8794/*
8795 * "argc()" function
8796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008798f_argc(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008799 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008801{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008802 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803}
8804
8805/*
8806 * "argidx()" function
8807 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008809f_argidx(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008810 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008811 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008813 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814}
8815
8816/*
8817 * "argv(nr)" function
8818 */
8819 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008820f_argv(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008821 typval_T *argvars;
8822 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008823{
8824 int idx;
8825
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008826 if (argvars[0].v_type != VAR_UNKNOWN)
8827 {
8828 idx = get_tv_number_chk(&argvars[0], NULL);
8829 if (idx >= 0 && idx < ARGCOUNT)
8830 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
8831 else
8832 rettv->vval.v_string = NULL;
8833 rettv->v_type = VAR_STRING;
8834 }
8835 else if (rettv_list_alloc(rettv) == OK)
8836 for (idx = 0; idx < ARGCOUNT; ++idx)
8837 list_append_string(rettv->vval.v_list,
8838 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839}
8840
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008841#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008842/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008843 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008844 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008845 static void
8846f_asin(argvars, rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008847 typval_T *argvars;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008848 typval_T *rettv;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008849{
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008850 float_T f;
8851
8852 rettv->v_type = VAR_FLOAT;
8853 if (get_float_arg(argvars, &f) == OK)
8854 rettv->vval.v_float = asin(f);
8855 else
8856 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008857}
8858
8859/*
8860 * "atan()" function
8861 */
8862 static void
8863f_atan(argvars, rettv)
8864 typval_T *argvars;
8865 typval_T *rettv;
8866{
8867 float_T f;
8868
8869 rettv->v_type = VAR_FLOAT;
8870 if (get_float_arg(argvars, &f) == OK)
8871 rettv->vval.v_float = atan(f);
8872 else
8873 rettv->vval.v_float = 0.0;
8874}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008875
8876/*
8877 * "atan2()" function
8878 */
8879 static void
8880f_atan2(argvars, rettv)
8881 typval_T *argvars;
8882 typval_T *rettv;
8883{
8884 float_T fx, fy;
8885
8886 rettv->v_type = VAR_FLOAT;
8887 if (get_float_arg(argvars, &fx) == OK
8888 && get_float_arg(&argvars[1], &fy) == OK)
8889 rettv->vval.v_float = atan2(fx, fy);
8890 else
8891 rettv->vval.v_float = 0.0;
8892}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008893#endif
8894
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895/*
8896 * "browse(save, title, initdir, default)" function
8897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008899f_browse(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008900 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008901 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008902{
8903#ifdef FEAT_BROWSE
8904 int save;
8905 char_u *title;
8906 char_u *initdir;
8907 char_u *defname;
8908 char_u buf[NUMBUFLEN];
8909 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008910 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008912 save = get_tv_number_chk(&argvars[0], &error);
8913 title = get_tv_string_chk(&argvars[1]);
8914 initdir = get_tv_string_buf_chk(&argvars[2], buf);
8915 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008917 if (error || title == NULL || initdir == NULL || defname == NULL)
8918 rettv->vval.v_string = NULL;
8919 else
8920 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008921 do_browse(save ? BROWSE_SAVE : 0,
8922 title, defname, NULL, initdir, NULL, curbuf);
8923#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008924 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008925#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008926 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008927}
8928
8929/*
8930 * "browsedir(title, initdir)" function
8931 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008932 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008933f_browsedir(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00008934 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00008935 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008936{
8937#ifdef FEAT_BROWSE
8938 char_u *title;
8939 char_u *initdir;
8940 char_u buf[NUMBUFLEN];
8941
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008942 title = get_tv_string_chk(&argvars[0]);
8943 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008944
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00008945 if (title == NULL || initdir == NULL)
8946 rettv->vval.v_string = NULL;
8947 else
8948 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008949 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008951 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008953 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954}
8955
Bram Moolenaar33570922005-01-25 22:26:29 +00008956static buf_T *find_buffer __ARGS((typval_T *avar));
Bram Moolenaar0d660222005-01-07 21:51:51 +00008957
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958/*
8959 * Find a buffer by number or exact name.
8960 */
8961 static buf_T *
8962find_buffer(avar)
Bram Moolenaar33570922005-01-25 22:26:29 +00008963 typval_T *avar;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964{
8965 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008967 if (avar->v_type == VAR_NUMBER)
8968 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00008969 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008971 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008972 if (buf == NULL)
8973 {
8974 /* No full path name match, try a match with a URL or a "nofile"
8975 * buffer, these don't use the full path. */
8976 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
8977 if (buf->b_fname != NULL
8978 && (path_with_url(buf->b_fname)
8979#ifdef FEAT_QUICKFIX
8980 || bt_nofile(buf)
8981#endif
8982 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008983 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00008984 break;
8985 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 }
8987 return buf;
8988}
8989
8990/*
8991 * "bufexists(expr)" function
8992 */
8993 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008994f_bufexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00008995 typval_T *argvars;
8996 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008997{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008998 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999}
9000
9001/*
9002 * "buflisted(expr)" function
9003 */
9004 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009005f_buflisted(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009006 typval_T *argvars;
9007 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009008{
9009 buf_T *buf;
9010
9011 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009012 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013}
9014
9015/*
9016 * "bufloaded(expr)" function
9017 */
9018 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009019f_bufloaded(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009020 typval_T *argvars;
9021 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022{
9023 buf_T *buf;
9024
9025 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009026 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009027}
9028
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009029static buf_T *get_buf_tv __ARGS((typval_T *tv, int curtab_only));
Bram Moolenaar0d660222005-01-07 21:51:51 +00009030
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031/*
9032 * Get buffer by number or pattern.
9033 */
9034 static buf_T *
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009035get_buf_tv(tv, curtab_only)
Bram Moolenaar33570922005-01-25 22:26:29 +00009036 typval_T *tv;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009037 int curtab_only;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009039 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009040 int save_magic;
9041 char_u *save_cpo;
9042 buf_T *buf;
9043
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009044 if (tv->v_type == VAR_NUMBER)
9045 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009046 if (tv->v_type != VAR_STRING)
9047 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009048 if (name == NULL || *name == NUL)
9049 return curbuf;
9050 if (name[0] == '$' && name[1] == NUL)
9051 return lastbuf;
9052
9053 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9054 save_magic = p_magic;
9055 p_magic = TRUE;
9056 save_cpo = p_cpo;
9057 p_cpo = (char_u *)"";
9058
9059 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009060 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061
9062 p_magic = save_magic;
9063 p_cpo = save_cpo;
9064
9065 /* If not found, try expanding the name, like done for bufexists(). */
9066 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009067 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009068
9069 return buf;
9070}
9071
9072/*
9073 * "bufname(expr)" function
9074 */
9075 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009076f_bufname(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009077 typval_T *argvars;
9078 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079{
9080 buf_T *buf;
9081
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009082 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009084 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009087 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009089 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 --emsg_off;
9091}
9092
9093/*
9094 * "bufnr(expr)" function
9095 */
9096 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009097f_bufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009098 typval_T *argvars;
9099 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100{
9101 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009102 int error = FALSE;
9103 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009105 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009107 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009108 --emsg_off;
9109
9110 /* If the buffer isn't found and the second argument is not zero create a
9111 * new buffer. */
9112 if (buf == NULL
9113 && argvars[1].v_type != VAR_UNKNOWN
9114 && get_tv_number_chk(&argvars[1], &error) != 0
9115 && !error
9116 && (name = get_tv_string_chk(&argvars[0])) != NULL
9117 && !error)
9118 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9119
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009121 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009123 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124}
9125
9126/*
9127 * "bufwinnr(nr)" function
9128 */
9129 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009130f_bufwinnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009131 typval_T *argvars;
9132 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133{
9134#ifdef FEAT_WINDOWS
9135 win_T *wp;
9136 int winnr = 0;
9137#endif
9138 buf_T *buf;
9139
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009140 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009142 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143#ifdef FEAT_WINDOWS
9144 for (wp = firstwin; wp; wp = wp->w_next)
9145 {
9146 ++winnr;
9147 if (wp->w_buffer == buf)
9148 break;
9149 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009150 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009152 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153#endif
9154 --emsg_off;
9155}
9156
9157/*
9158 * "byte2line(byte)" function
9159 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161f_byte2line(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009162 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009163 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164{
9165#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009166 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167#else
9168 long boff = 0;
9169
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009170 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009172 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009174 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175 (linenr_T)0, &boff);
9176#endif
9177}
9178
9179/*
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009180 * "byteidx()" function
9181 */
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009182 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009183f_byteidx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009184 typval_T *argvars;
9185 typval_T *rettv;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009186{
9187#ifdef FEAT_MBYTE
9188 char_u *t;
9189#endif
9190 char_u *str;
9191 long idx;
9192
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009193 str = get_tv_string_chk(&argvars[0]);
9194 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009196 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009197 return;
9198
9199#ifdef FEAT_MBYTE
9200 t = str;
9201 for ( ; idx > 0; idx--)
9202 {
9203 if (*t == NUL) /* EOL reached */
9204 return;
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009205 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009206 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009207 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009208#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009209 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009210 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009211#endif
9212}
9213
Bram Moolenaardb913952012-06-29 12:54:53 +02009214 int
9215func_call(name, args, selfdict, rettv)
9216 char_u *name;
9217 typval_T *args;
9218 dict_T *selfdict;
9219 typval_T *rettv;
9220{
9221 listitem_T *item;
9222 typval_T argv[MAX_FUNC_ARGS + 1];
9223 int argc = 0;
9224 int dummy;
9225 int r = 0;
9226
9227 for (item = args->vval.v_list->lv_first; item != NULL;
9228 item = item->li_next)
9229 {
9230 if (argc == MAX_FUNC_ARGS)
9231 {
9232 EMSG(_("E699: Too many arguments"));
9233 break;
9234 }
9235 /* Make a copy of each argument. This is needed to be able to set
9236 * v_lock to VAR_FIXED in the copy without changing the original list.
9237 */
9238 copy_tv(&item->li_tv, &argv[argc++]);
9239 }
9240
9241 if (item == NULL)
9242 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9243 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9244 &dummy, TRUE, selfdict);
9245
9246 /* Free the arguments. */
9247 while (argc > 0)
9248 clear_tv(&argv[--argc]);
9249
9250 return r;
9251}
9252
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009253/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009254 * "call(func, arglist)" function
9255 */
9256 static void
9257f_call(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009258 typval_T *argvars;
9259 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009260{
9261 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009262 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009263
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009264 if (argvars[1].v_type != VAR_LIST)
9265 {
9266 EMSG(_(e_listreq));
9267 return;
9268 }
9269 if (argvars[1].vval.v_list == NULL)
9270 return;
9271
9272 if (argvars[0].v_type == VAR_FUNC)
9273 func = argvars[0].vval.v_string;
9274 else
9275 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009276 if (*func == NUL)
9277 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009278
Bram Moolenaare9a41262005-01-15 22:18:47 +00009279 if (argvars[2].v_type != VAR_UNKNOWN)
9280 {
9281 if (argvars[2].v_type != VAR_DICT)
9282 {
9283 EMSG(_(e_dictreq));
9284 return;
9285 }
9286 selfdict = argvars[2].vval.v_dict;
9287 }
9288
Bram Moolenaardb913952012-06-29 12:54:53 +02009289 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009290}
9291
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009292#ifdef FEAT_FLOAT
9293/*
9294 * "ceil({float})" function
9295 */
9296 static void
9297f_ceil(argvars, rettv)
9298 typval_T *argvars;
9299 typval_T *rettv;
9300{
9301 float_T f;
9302
9303 rettv->v_type = VAR_FLOAT;
9304 if (get_float_arg(argvars, &f) == OK)
9305 rettv->vval.v_float = ceil(f);
9306 else
9307 rettv->vval.v_float = 0.0;
9308}
9309#endif
9310
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009311/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009312 * "changenr()" function
9313 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009314 static void
9315f_changenr(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009316 typval_T *argvars UNUSED;
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00009317 typval_T *rettv;
9318{
9319 rettv->vval.v_number = curbuf->b_u_seq_cur;
9320}
9321
9322/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323 * "char2nr(string)" function
9324 */
9325 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009326f_char2nr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009327 typval_T *argvars;
9328 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329{
9330#ifdef FEAT_MBYTE
9331 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +01009332 {
9333 int utf8 = 0;
9334
9335 if (argvars[1].v_type != VAR_UNKNOWN)
9336 utf8 = get_tv_number_chk(&argvars[1], NULL);
9337
9338 if (utf8)
9339 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
9340 else
9341 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
9342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009343 else
9344#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009345 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346}
9347
9348/*
9349 * "cindent(lnum)" function
9350 */
9351 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009352f_cindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01009353 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009354 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355{
9356#ifdef FEAT_CINDENT
9357 pos_T pos;
9358 linenr_T lnum;
9359
9360 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009361 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
9363 {
9364 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009365 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366 curwin->w_cursor = pos;
9367 }
9368 else
9369#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009370 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009371}
9372
9373/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009374 * "clearmatches()" function
9375 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009376 static void
9377f_clearmatches(argvars, rettv)
Bram Moolenaar78a15312009-05-15 19:33:18 +00009378 typval_T *argvars UNUSED;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009379 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +00009380{
9381#ifdef FEAT_SEARCH_EXTRA
9382 clear_matches(curwin);
9383#endif
9384}
9385
9386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009387 * "col(string)" function
9388 */
9389 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009390f_col(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009391 typval_T *argvars;
9392 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393{
9394 colnr_T col = 0;
9395 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009396 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009398 fp = var2fpos(&argvars[0], FALSE, &fnum);
9399 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 {
9401 if (fp->col == MAXCOL)
9402 {
9403 /* '> can be MAXCOL, get the length of the line then */
9404 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009405 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009406 else
9407 col = MAXCOL;
9408 }
9409 else
9410 {
9411 col = fp->col + 1;
9412#ifdef FEAT_VIRTUALEDIT
9413 /* col(".") when the cursor is on the NUL at the end of the line
9414 * because of "coladd" can be seen as an extra column. */
9415 if (virtual_active() && fp == &curwin->w_cursor)
9416 {
9417 char_u *p = ml_get_cursor();
9418
9419 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
9420 curwin->w_virtcol - curwin->w_cursor.coladd))
9421 {
9422# ifdef FEAT_MBYTE
9423 int l;
9424
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009425 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 col += l;
9427# else
9428 if (*p != NUL && p[1] == NUL)
9429 ++col;
9430# endif
9431 }
9432 }
9433#endif
9434 }
9435 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009436 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009437}
9438
Bram Moolenaar572cb562005-08-05 21:35:02 +00009439#if defined(FEAT_INS_EXPAND)
9440/*
Bram Moolenaarade00832006-03-10 21:46:58 +00009441 * "complete()" function
9442 */
Bram Moolenaarade00832006-03-10 21:46:58 +00009443 static void
9444f_complete(argvars, rettv)
9445 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009446 typval_T *rettv UNUSED;
Bram Moolenaarade00832006-03-10 21:46:58 +00009447{
9448 int startcol;
9449
9450 if ((State & INSERT) == 0)
9451 {
9452 EMSG(_("E785: complete() can only be used in Insert mode"));
9453 return;
9454 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +00009455
9456 /* Check for undo allowed here, because if something was already inserted
9457 * the line was already saved for undo and this check isn't done. */
9458 if (!undo_allowed())
9459 return;
9460
Bram Moolenaarade00832006-03-10 21:46:58 +00009461 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
9462 {
9463 EMSG(_(e_invarg));
9464 return;
9465 }
9466
9467 startcol = get_tv_number_chk(&argvars[0], NULL);
9468 if (startcol <= 0)
9469 return;
9470
9471 set_completion(startcol - 1, argvars[1].vval.v_list);
9472}
9473
9474/*
Bram Moolenaar572cb562005-08-05 21:35:02 +00009475 * "complete_add()" function
9476 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009477 static void
9478f_complete_add(argvars, rettv)
9479 typval_T *argvars;
9480 typval_T *rettv;
9481{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +00009482 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +00009483}
9484
9485/*
9486 * "complete_check()" function
9487 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00009488 static void
9489f_complete_check(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009490 typval_T *argvars UNUSED;
Bram Moolenaar572cb562005-08-05 21:35:02 +00009491 typval_T *rettv;
9492{
9493 int saved = RedrawingDisabled;
9494
9495 RedrawingDisabled = 0;
9496 ins_compl_check_keys(0);
9497 rettv->vval.v_number = compl_interrupted;
9498 RedrawingDisabled = saved;
9499}
9500#endif
9501
Bram Moolenaar071d4272004-06-13 20:20:40 +00009502/*
9503 * "confirm(message, buttons[, default [, type]])" function
9504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009506f_confirm(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009507 typval_T *argvars UNUSED;
9508 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509{
9510#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
9511 char_u *message;
9512 char_u *buttons = NULL;
9513 char_u buf[NUMBUFLEN];
9514 char_u buf2[NUMBUFLEN];
9515 int def = 1;
9516 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009517 char_u *typestr;
9518 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009519
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009520 message = get_tv_string_chk(&argvars[0]);
9521 if (message == NULL)
9522 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009523 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009525 buttons = get_tv_string_buf_chk(&argvars[1], buf);
9526 if (buttons == NULL)
9527 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009528 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009530 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009531 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009533 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
9534 if (typestr == NULL)
9535 error = TRUE;
9536 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009538 switch (TOUPPER_ASC(*typestr))
9539 {
9540 case 'E': type = VIM_ERROR; break;
9541 case 'Q': type = VIM_QUESTION; break;
9542 case 'I': type = VIM_INFO; break;
9543 case 'W': type = VIM_WARNING; break;
9544 case 'G': type = VIM_GENERIC; break;
9545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009546 }
9547 }
9548 }
9549 }
9550
9551 if (buttons == NULL || *buttons == NUL)
9552 buttons = (char_u *)_("&Ok");
9553
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009554 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009555 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +01009556 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009557#endif
9558}
9559
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009560/*
9561 * "copy()" function
9562 */
9563 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564f_copy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009565 typval_T *argvars;
9566 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009567{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009568 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009569}
Bram Moolenaar071d4272004-06-13 20:20:40 +00009570
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009571#ifdef FEAT_FLOAT
9572/*
9573 * "cos()" function
9574 */
9575 static void
9576f_cos(argvars, rettv)
9577 typval_T *argvars;
9578 typval_T *rettv;
9579{
9580 float_T f;
9581
9582 rettv->v_type = VAR_FLOAT;
9583 if (get_float_arg(argvars, &f) == OK)
9584 rettv->vval.v_float = cos(f);
9585 else
9586 rettv->vval.v_float = 0.0;
9587}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009588
9589/*
9590 * "cosh()" function
9591 */
9592 static void
9593f_cosh(argvars, rettv)
9594 typval_T *argvars;
9595 typval_T *rettv;
9596{
9597 float_T f;
9598
9599 rettv->v_type = VAR_FLOAT;
9600 if (get_float_arg(argvars, &f) == OK)
9601 rettv->vval.v_float = cosh(f);
9602 else
9603 rettv->vval.v_float = 0.0;
9604}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009605#endif
9606
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009608 * "count()" function
9609 */
9610 static void
9611f_count(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009612 typval_T *argvars;
9613 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009614{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009615 long n = 0;
9616 int ic = FALSE;
9617
Bram Moolenaare9a41262005-01-15 22:18:47 +00009618 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009619 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009620 listitem_T *li;
9621 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009622 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009623
Bram Moolenaare9a41262005-01-15 22:18:47 +00009624 if ((l = argvars[0].vval.v_list) != NULL)
9625 {
9626 li = l->lv_first;
9627 if (argvars[2].v_type != VAR_UNKNOWN)
9628 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009629 int error = FALSE;
9630
9631 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009632 if (argvars[3].v_type != VAR_UNKNOWN)
9633 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009634 idx = get_tv_number_chk(&argvars[3], &error);
9635 if (!error)
9636 {
9637 li = list_find(l, idx);
9638 if (li == NULL)
9639 EMSGN(_(e_listidx), idx);
9640 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009641 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009642 if (error)
9643 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009644 }
9645
9646 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009647 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +00009648 ++n;
9649 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009650 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009651 else if (argvars[0].v_type == VAR_DICT)
9652 {
Bram Moolenaar33570922005-01-25 22:26:29 +00009653 int todo;
9654 dict_T *d;
9655 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009656
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009657 if ((d = argvars[0].vval.v_dict) != NULL)
9658 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009659 int error = FALSE;
9660
Bram Moolenaare9a41262005-01-15 22:18:47 +00009661 if (argvars[2].v_type != VAR_UNKNOWN)
9662 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009663 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +00009664 if (argvars[3].v_type != VAR_UNKNOWN)
9665 EMSG(_(e_invarg));
9666 }
9667
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009668 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00009669 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009670 {
9671 if (!HASHITEM_EMPTY(hi))
9672 {
9673 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01009674 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00009675 ++n;
9676 }
9677 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00009678 }
9679 }
9680 else
9681 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009682 rettv->vval.v_number = n;
9683}
9684
9685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
9687 *
9688 * Checks the existence of a cscope connection.
9689 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009691f_cscope_connection(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009692 typval_T *argvars UNUSED;
9693 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694{
9695#ifdef FEAT_CSCOPE
9696 int num = 0;
9697 char_u *dbpath = NULL;
9698 char_u *prepend = NULL;
9699 char_u buf[NUMBUFLEN];
9700
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009701 if (argvars[0].v_type != VAR_UNKNOWN
9702 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 num = (int)get_tv_number(&argvars[0]);
9705 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009706 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009707 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708 }
9709
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009710 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711#endif
9712}
9713
9714/*
9715 * "cursor(lnum, col)" function
9716 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009717 * Moves the cursor to the specified line and column.
9718 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00009719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009721f_cursor(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009722 typval_T *argvars;
9723 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724{
9725 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009726#ifdef FEAT_VIRTUALEDIT
9727 long coladd = 0;
9728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009730 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +00009731 if (argvars[1].v_type == VAR_UNKNOWN)
9732 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009733 pos_T pos;
Bram Moolenaara5525202006-03-02 22:52:09 +00009734
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009735 if (list2fpos(argvars, &pos, NULL) == FAIL)
Bram Moolenaara5525202006-03-02 22:52:09 +00009736 return;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009737 line = pos.lnum;
9738 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +00009739#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009740 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +00009741#endif
9742 }
9743 else
9744 {
9745 line = get_tv_lnum(argvars);
9746 col = get_tv_number_chk(&argvars[1], NULL);
9747#ifdef FEAT_VIRTUALEDIT
9748 if (argvars[2].v_type != VAR_UNKNOWN)
9749 coladd = get_tv_number_chk(&argvars[2], NULL);
9750#endif
9751 }
9752 if (line < 0 || col < 0
9753#ifdef FEAT_VIRTUALEDIT
9754 || coladd < 0
9755#endif
9756 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009757 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 if (line > 0)
9759 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 if (col > 0)
9761 curwin->w_cursor.col = col - 1;
9762#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +00009763 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764#endif
9765
9766 /* Make sure the cursor is in a valid position. */
9767 check_cursor();
9768#ifdef FEAT_MBYTE
9769 /* Correct cursor for multi-byte character. */
9770 if (has_mbyte)
9771 mb_adjust_cursor();
9772#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00009773
9774 curwin->w_set_curswant = TRUE;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00009775 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776}
9777
9778/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009779 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 */
9781 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782f_deepcopy(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009783 typval_T *argvars;
9784 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785{
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009786 int noref = 0;
9787
9788 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009789 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009790 if (noref < 0 || noref > 1)
9791 EMSG(_(e_invarg));
9792 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00009793 {
9794 current_copyID += COPYID_INC;
9795 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
9796 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797}
9798
9799/*
9800 * "delete()" function
9801 */
9802 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009803f_delete(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009804 typval_T *argvars;
9805 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806{
9807 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009808 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009809 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009810 rettv->vval.v_number = mch_remove(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811}
9812
9813/*
9814 * "did_filetype()" function
9815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009817f_did_filetype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009818 typval_T *argvars UNUSED;
9819 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820{
9821#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009822 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823#endif
9824}
9825
9826/*
Bram Moolenaar47136d72004-10-12 20:02:24 +00009827 * "diff_filler()" function
9828 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009829 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009830f_diff_filler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009831 typval_T *argvars UNUSED;
9832 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009833{
9834#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009835 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +00009836#endif
9837}
9838
9839/*
9840 * "diff_hlID()" function
9841 */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009842 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009843f_diff_hlID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009844 typval_T *argvars UNUSED;
9845 typval_T *rettv UNUSED;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009846{
9847#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009848 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +00009849 static linenr_T prev_lnum = 0;
9850 static int changedtick = 0;
9851 static int fnum = 0;
9852 static int change_start = 0;
9853 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +00009854 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009855 int filler_lines;
9856 int col;
9857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009858 if (lnum < 0) /* ignore type error in {lnum} arg */
9859 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009860 if (lnum != prev_lnum
9861 || changedtick != curbuf->b_changedtick
9862 || fnum != curbuf->b_fnum)
9863 {
9864 /* New line, buffer, change: need to get the values. */
9865 filler_lines = diff_check(curwin, lnum);
9866 if (filler_lines < 0)
9867 {
9868 if (filler_lines == -1)
9869 {
9870 change_start = MAXCOL;
9871 change_end = -1;
9872 if (diff_find_change(curwin, lnum, &change_start, &change_end))
9873 hlID = HLF_ADD; /* added line */
9874 else
9875 hlID = HLF_CHD; /* changed line */
9876 }
9877 else
9878 hlID = HLF_ADD; /* added line */
9879 }
9880 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009881 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009882 prev_lnum = lnum;
9883 changedtick = curbuf->b_changedtick;
9884 fnum = curbuf->b_fnum;
9885 }
9886
9887 if (hlID == HLF_CHD || hlID == HLF_TXD)
9888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009889 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +00009890 if (col >= change_start && col <= change_end)
9891 hlID = HLF_TXD; /* changed text */
9892 else
9893 hlID = HLF_CHD; /* changed line */
9894 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00009895 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +00009896#endif
9897}
9898
9899/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009900 * "empty({expr})" function
9901 */
9902 static void
9903f_empty(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009904 typval_T *argvars;
9905 typval_T *rettv;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009906{
9907 int n;
9908
9909 switch (argvars[0].v_type)
9910 {
9911 case VAR_STRING:
9912 case VAR_FUNC:
9913 n = argvars[0].vval.v_string == NULL
9914 || *argvars[0].vval.v_string == NUL;
9915 break;
9916 case VAR_NUMBER:
9917 n = argvars[0].vval.v_number == 0;
9918 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009919#ifdef FEAT_FLOAT
9920 case VAR_FLOAT:
9921 n = argvars[0].vval.v_float == 0.0;
9922 break;
9923#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009924 case VAR_LIST:
9925 n = argvars[0].vval.v_list == NULL
9926 || argvars[0].vval.v_list->lv_first == NULL;
9927 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009928 case VAR_DICT:
9929 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +00009930 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009931 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +00009932 default:
9933 EMSG2(_(e_intern2), "f_empty()");
9934 n = 0;
9935 }
9936
9937 rettv->vval.v_number = n;
9938}
9939
9940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 * "escape({string}, {chars})" function
9942 */
9943 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944f_escape(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009945 typval_T *argvars;
9946 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947{
9948 char_u buf[NUMBUFLEN];
9949
Bram Moolenaar758711c2005-02-02 23:11:38 +00009950 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
9951 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009952 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009953}
9954
9955/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009956 * "eval()" function
9957 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009958 static void
9959f_eval(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009960 typval_T *argvars;
9961 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009962{
9963 char_u *s;
9964
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009965 s = get_tv_string_chk(&argvars[0]);
9966 if (s != NULL)
9967 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009968
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009969 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
9970 {
9971 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009972 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009973 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00009974 else if (*s != NUL)
9975 EMSG(_(e_trailing));
9976}
9977
9978/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 * "eventhandler()" function
9980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009981 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009982f_eventhandler(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +00009983 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +00009984 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009986 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009987}
9988
9989/*
9990 * "executable()" function
9991 */
9992 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009993f_executable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +00009994 typval_T *argvars;
9995 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009997 rettv->vval.v_number = mch_can_exe(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998}
9999
10000/*
10001 * "exists()" function
10002 */
10003 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010004f_exists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010005 typval_T *argvars;
10006 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010007{
10008 char_u *p;
10009 char_u *name;
10010 int n = FALSE;
10011 int len = 0;
10012
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010013 no_autoload = TRUE;
10014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010015 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016 if (*p == '$') /* environment variable */
10017 {
10018 /* first try "normal" environment variables (fast) */
10019 if (mch_getenv(p + 1) != NULL)
10020 n = TRUE;
10021 else
10022 {
10023 /* try expanding things like $VIM and ${HOME} */
10024 p = expand_env_save(p);
10025 if (p != NULL && *p != '$')
10026 n = TRUE;
10027 vim_free(p);
10028 }
10029 }
10030 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010032 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010033 if (*skipwhite(p) != NUL)
10034 n = FALSE; /* trailing garbage */
10035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036 else if (*p == '*') /* internal or user defined function */
10037 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010038 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010039 }
10040 else if (*p == ':')
10041 {
10042 n = cmd_exists(p + 1);
10043 }
10044 else if (*p == '#')
10045 {
10046#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010047 if (p[1] == '#')
10048 n = autocmd_supported(p + 2);
10049 else
10050 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051#endif
10052 }
10053 else /* internal variable */
10054 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010055 char_u *tofree;
10056 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010058 /* get_name_len() takes care of expanding curly braces */
10059 name = p;
10060 len = get_name_len(&p, &tofree, TRUE, FALSE);
10061 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010062 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010063 if (tofree != NULL)
10064 name = tofree;
10065 n = (get_var_tv(name, len, &tv, FALSE) == OK);
10066 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010068 /* handle d.key, l[idx], f(expr) */
10069 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10070 if (n)
10071 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 }
10073 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010074 if (*p != NUL)
10075 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010077 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010078 }
10079
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010080 rettv->vval.v_number = n;
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020010081
10082 no_autoload = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083}
10084
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010085#ifdef FEAT_FLOAT
10086/*
10087 * "exp()" function
10088 */
10089 static void
10090f_exp(argvars, rettv)
10091 typval_T *argvars;
10092 typval_T *rettv;
10093{
10094 float_T f;
10095
10096 rettv->v_type = VAR_FLOAT;
10097 if (get_float_arg(argvars, &f) == OK)
10098 rettv->vval.v_float = exp(f);
10099 else
10100 rettv->vval.v_float = 0.0;
10101}
10102#endif
10103
Bram Moolenaar071d4272004-06-13 20:20:40 +000010104/*
10105 * "expand()" function
10106 */
10107 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010108f_expand(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010109 typval_T *argvars;
10110 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111{
10112 char_u *s;
10113 int len;
10114 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010115 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010117 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010118 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010120 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010121 if (argvars[1].v_type != VAR_UNKNOWN
10122 && argvars[2].v_type != VAR_UNKNOWN
10123 && get_tv_number_chk(&argvars[2], &error)
10124 && !error)
10125 {
10126 rettv->v_type = VAR_LIST;
10127 rettv->vval.v_list = NULL;
10128 }
10129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010130 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010131 if (*s == '%' || *s == '#' || *s == '<')
10132 {
10133 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010134 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010135 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010136 if (rettv->v_type == VAR_LIST)
10137 {
10138 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10139 list_append_string(rettv->vval.v_list, result, -1);
10140 else
10141 vim_free(result);
10142 }
10143 else
10144 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010145 }
10146 else
10147 {
10148 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000010149 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010150 if (argvars[1].v_type != VAR_UNKNOWN
10151 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010152 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010153 if (!error)
10154 {
10155 ExpandInit(&xpc);
10156 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010157 if (p_wic)
10158 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010159 if (rettv->v_type == VAR_STRING)
10160 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
10161 options, WILD_ALL);
10162 else if (rettv_list_alloc(rettv) != FAIL)
10163 {
10164 int i;
10165
10166 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
10167 for (i = 0; i < xpc.xp_numfiles; i++)
10168 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
10169 ExpandCleanup(&xpc);
10170 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010171 }
10172 else
10173 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 }
10175}
10176
10177/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020010178 * Go over all entries in "d2" and add them to "d1".
10179 * When "action" is "error" then a duplicate key is an error.
10180 * When "action" is "force" then a duplicate key is overwritten.
10181 * Otherwise duplicate keys are ignored ("action" is "keep").
10182 */
10183 void
10184dict_extend(d1, d2, action)
10185 dict_T *d1;
10186 dict_T *d2;
10187 char_u *action;
10188{
10189 dictitem_T *di1;
10190 hashitem_T *hi2;
10191 int todo;
10192
10193 todo = (int)d2->dv_hashtab.ht_used;
10194 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
10195 {
10196 if (!HASHITEM_EMPTY(hi2))
10197 {
10198 --todo;
10199 di1 = dict_find(d1, hi2->hi_key, -1);
10200 if (d1->dv_scope != 0)
10201 {
10202 /* Disallow replacing a builtin function in l: and g:.
10203 * Check the key to be valid when adding to any
10204 * scope. */
10205 if (d1->dv_scope == VAR_DEF_SCOPE
10206 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
10207 && var_check_func_name(hi2->hi_key,
10208 di1 == NULL))
10209 break;
10210 if (!valid_varname(hi2->hi_key))
10211 break;
10212 }
10213 if (di1 == NULL)
10214 {
10215 di1 = dictitem_copy(HI2DI(hi2));
10216 if (di1 != NULL && dict_add(d1, di1) == FAIL)
10217 dictitem_free(di1);
10218 }
10219 else if (*action == 'e')
10220 {
10221 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
10222 break;
10223 }
10224 else if (*action == 'f' && HI2DI(hi2) != di1)
10225 {
10226 clear_tv(&di1->di_tv);
10227 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
10228 }
10229 }
10230 }
10231}
10232
10233/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010234 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000010235 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010236 */
10237 static void
10238f_extend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010239 typval_T *argvars;
10240 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010241{
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010242 char *arg_errmsg = N_("extend() argument");
10243
Bram Moolenaare9a41262005-01-15 22:18:47 +000010244 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010245 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010246 list_T *l1, *l2;
10247 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010248 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010249 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010250
Bram Moolenaare9a41262005-01-15 22:18:47 +000010251 l1 = argvars[0].vval.v_list;
10252 l2 = argvars[1].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010253 if (l1 != NULL && !tv_check_lock(l1->lv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010254 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010255 {
10256 if (argvars[2].v_type != VAR_UNKNOWN)
10257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010258 before = get_tv_number_chk(&argvars[2], &error);
10259 if (error)
10260 return; /* type error; errmsg already given */
10261
Bram Moolenaar758711c2005-02-02 23:11:38 +000010262 if (before == l1->lv_len)
10263 item = NULL;
10264 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000010265 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000010266 item = list_find(l1, before);
10267 if (item == NULL)
10268 {
10269 EMSGN(_(e_listidx), before);
10270 return;
10271 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010272 }
10273 }
10274 else
10275 item = NULL;
10276 list_extend(l1, l2, item);
10277
Bram Moolenaare9a41262005-01-15 22:18:47 +000010278 copy_tv(&argvars[0], rettv);
10279 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010280 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010281 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
10282 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020010283 dict_T *d1, *d2;
10284 char_u *action;
10285 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010286
10287 d1 = argvars[0].vval.v_dict;
10288 d2 = argvars[1].vval.v_dict;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010289 if (d1 != NULL && !tv_check_lock(d1->dv_lock, (char_u *)_(arg_errmsg))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010290 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010291 {
10292 /* Check the third argument. */
10293 if (argvars[2].v_type != VAR_UNKNOWN)
10294 {
10295 static char *(av[]) = {"keep", "force", "error"};
10296
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010297 action = get_tv_string_chk(&argvars[2]);
10298 if (action == NULL)
10299 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010300 for (i = 0; i < 3; ++i)
10301 if (STRCMP(action, av[i]) == 0)
10302 break;
10303 if (i == 3)
10304 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000010305 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010306 return;
10307 }
10308 }
10309 else
10310 action = (char_u *)"force";
10311
Bram Moolenaara9922d62013-05-30 13:01:18 +020010312 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010313
Bram Moolenaare9a41262005-01-15 22:18:47 +000010314 copy_tv(&argvars[0], rettv);
10315 }
10316 }
10317 else
10318 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010319}
10320
10321/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010322 * "feedkeys()" function
10323 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010324 static void
10325f_feedkeys(argvars, rettv)
10326 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010327 typval_T *rettv UNUSED;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010328{
10329 int remap = TRUE;
10330 char_u *keys, *flags;
10331 char_u nbuf[NUMBUFLEN];
10332 int typed = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010333 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010334
Bram Moolenaar3d43a662007-04-27 20:15:55 +000010335 /* This is not allowed in the sandbox. If the commands would still be
10336 * executed in the sandbox it would be OK, but it probably happens later,
10337 * when "sandbox" is no longer set. */
10338 if (check_secure())
10339 return;
10340
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010341 keys = get_tv_string(&argvars[0]);
10342 if (*keys != NUL)
10343 {
10344 if (argvars[1].v_type != VAR_UNKNOWN)
10345 {
10346 flags = get_tv_string_buf(&argvars[1], nbuf);
10347 for ( ; *flags != NUL; ++flags)
10348 {
10349 switch (*flags)
10350 {
10351 case 'n': remap = FALSE; break;
10352 case 'm': remap = TRUE; break;
10353 case 't': typed = TRUE; break;
10354 }
10355 }
10356 }
10357
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010358 /* Need to escape K_SPECIAL and CSI before putting the string in the
10359 * typeahead buffer. */
10360 keys_esc = vim_strsave_escape_csi(keys);
10361 if (keys_esc != NULL)
10362 {
10363 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010364 typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010365 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000010366 if (vgetc_busy)
10367 typebuf_was_filled = TRUE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010368 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000010369 }
10370}
10371
10372/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010373 * "filereadable()" function
10374 */
10375 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010376f_filereadable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010377 typval_T *argvars;
10378 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010379{
Bram Moolenaarc236c162008-07-13 17:41:49 +000010380 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010381 char_u *p;
10382 int n;
10383
Bram Moolenaarc236c162008-07-13 17:41:49 +000010384#ifndef O_NONBLOCK
10385# define O_NONBLOCK 0
10386#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010387 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000010388 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
10389 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010390 {
10391 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000010392 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010393 }
10394 else
10395 n = FALSE;
10396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010397 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398}
10399
10400/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010401 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000010402 * rights to write into.
10403 */
10404 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010405f_filewritable(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010406 typval_T *argvars;
10407 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010408{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000010409 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410}
10411
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010412static void findfilendir __ARGS((typval_T *argvars, typval_T *rettv, int find_what));
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010413
10414 static void
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010415findfilendir(argvars, rettv, find_what)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010416 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010417 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010418 int find_what UNUSED;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010419{
10420#ifdef FEAT_SEARCHPATH
10421 char_u *fname;
10422 char_u *fresult = NULL;
10423 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
10424 char_u *p;
10425 char_u pathbuf[NUMBUFLEN];
10426 int count = 1;
10427 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010428 int error = FALSE;
10429#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010430
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010431 rettv->vval.v_string = NULL;
10432 rettv->v_type = VAR_STRING;
10433
10434#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010435 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010436
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010437 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010439 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
10440 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010441 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010442 else
10443 {
10444 if (*p != NUL)
10445 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010447 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010448 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010449 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010450 }
10451
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010452 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
10453 error = TRUE;
10454
10455 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010457 do
10458 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020010459 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010460 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010461 fresult = find_file_in_path_option(first ? fname : NULL,
10462 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010463 0, first, path,
10464 find_what,
10465 curbuf->b_ffname,
10466 find_what == FINDFILE_DIR
10467 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010468 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010469
10470 if (fresult != NULL && rettv->v_type == VAR_LIST)
10471 list_append_string(rettv->vval.v_list, fresult, -1);
10472
10473 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010474 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010475
Bram Moolenaar899dddf2006-03-26 21:06:50 +000010476 if (rettv->v_type == VAR_STRING)
10477 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010478#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010479}
10480
Bram Moolenaar33570922005-01-25 22:26:29 +000010481static void filter_map __ARGS((typval_T *argvars, typval_T *rettv, int map));
10482static int filter_map_one __ARGS((typval_T *tv, char_u *expr, int map, int *remp));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010483
10484/*
10485 * Implementation of map() and filter().
10486 */
10487 static void
10488filter_map(argvars, rettv, map)
Bram Moolenaar33570922005-01-25 22:26:29 +000010489 typval_T *argvars;
10490 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010491 int map;
10492{
10493 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000010494 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000010495 listitem_T *li, *nli;
10496 list_T *l = NULL;
10497 dictitem_T *di;
10498 hashtab_T *ht;
10499 hashitem_T *hi;
10500 dict_T *d = NULL;
10501 typval_T save_val;
10502 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010503 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010504 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010505 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
10506 char *arg_errmsg = (map ? N_("map() argument")
10507 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010508 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010509 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010510
Bram Moolenaare9a41262005-01-15 22:18:47 +000010511 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010512 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010513 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010514 || tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010515 return;
10516 }
10517 else if (argvars[0].v_type == VAR_DICT)
10518 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010519 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010520 || tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010521 return;
10522 }
10523 else
10524 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000010525 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010526 return;
10527 }
10528
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010529 expr = get_tv_string_buf_chk(&argvars[1], buf);
10530 /* On type errors, the preceding call has already displayed an error
10531 * message. Avoid a misleading error message for an empty string that
10532 * was not passed as argument. */
10533 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010534 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010535 prepare_vimvar(VV_VAL, &save_val);
10536 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010537
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010538 /* We reset "did_emsg" to be able to detect whether an error
10539 * occurred during evaluation of the expression. */
10540 save_did_emsg = did_emsg;
10541 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010542
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010543 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010544 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010545 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010546 vimvars[VV_KEY].vv_type = VAR_STRING;
10547
10548 ht = &d->dv_hashtab;
10549 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010550 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010551 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010552 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010553 if (!HASHITEM_EMPTY(hi))
10554 {
10555 --todo;
10556 di = HI2DI(hi);
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010557 if (tv_check_lock(di->di_tv.v_lock,
10558 (char_u *)_(arg_errmsg)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010559 break;
10560 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010561 if (filter_map_one(&di->di_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010562 || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010563 break;
10564 if (!map && rem)
10565 dictitem_remove(d, di);
10566 clear_tv(&vimvars[VV_KEY].vv_tv);
10567 }
10568 }
10569 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 }
10571 else
10572 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010573 vimvars[VV_KEY].vv_type = VAR_NUMBER;
10574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010575 for (li = l->lv_first; li != NULL; li = nli)
10576 {
Bram Moolenaar32f649e2011-04-11 13:46:13 +020010577 if (tv_check_lock(li->li_tv.v_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000010578 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010579 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010580 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000010581 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010582 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010583 break;
10584 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010585 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020010586 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010587 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010588 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010589
Bram Moolenaar627b1d32009-11-17 11:20:35 +000010590 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010591 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000010592
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000010593 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010594 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010595
10596 copy_tv(&argvars[0], rettv);
10597}
10598
10599 static int
10600filter_map_one(tv, expr, map, remp)
Bram Moolenaar33570922005-01-25 22:26:29 +000010601 typval_T *tv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010602 char_u *expr;
10603 int map;
10604 int *remp;
10605{
Bram Moolenaar33570922005-01-25 22:26:29 +000010606 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010607 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010608 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010609
Bram Moolenaar33570922005-01-25 22:26:29 +000010610 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010611 s = expr;
10612 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010613 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010614 if (*s != NUL) /* check for trailing chars after expr */
10615 {
10616 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010617 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010618 }
10619 if (map)
10620 {
10621 /* map(): replace the list item value */
10622 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000010623 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010624 *tv = rettv;
10625 }
10626 else
10627 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010628 int error = FALSE;
10629
Bram Moolenaare9a41262005-01-15 22:18:47 +000010630 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010631 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010632 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010633 /* On type error, nothing has been removed; return FAIL to stop the
10634 * loop. The error message was given by get_tv_number_chk(). */
10635 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010636 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010637 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010638 retval = OK;
10639theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000010640 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000010641 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010642}
10643
10644/*
10645 * "filter()" function
10646 */
10647 static void
10648f_filter(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010649 typval_T *argvars;
10650 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010651{
10652 filter_map(argvars, rettv, FALSE);
10653}
10654
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000010655/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010656 * "finddir({fname}[, {path}[, {count}]])" function
10657 */
10658 static void
10659f_finddir(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010660 typval_T *argvars;
10661 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010662{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010663 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010664}
10665
10666/*
10667 * "findfile({fname}[, {path}[, {count}]])" function
10668 */
10669 static void
10670f_findfile(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010671 typval_T *argvars;
10672 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010673{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000010674 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000010675}
10676
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010677#ifdef FEAT_FLOAT
10678/*
10679 * "float2nr({float})" function
10680 */
10681 static void
10682f_float2nr(argvars, rettv)
10683 typval_T *argvars;
10684 typval_T *rettv;
10685{
10686 float_T f;
10687
10688 if (get_float_arg(argvars, &f) == OK)
10689 {
10690 if (f < -0x7fffffff)
10691 rettv->vval.v_number = -0x7fffffff;
10692 else if (f > 0x7fffffff)
10693 rettv->vval.v_number = 0x7fffffff;
10694 else
10695 rettv->vval.v_number = (varnumber_T)f;
10696 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010697}
10698
10699/*
10700 * "floor({float})" function
10701 */
10702 static void
10703f_floor(argvars, rettv)
10704 typval_T *argvars;
10705 typval_T *rettv;
10706{
10707 float_T f;
10708
10709 rettv->v_type = VAR_FLOAT;
10710 if (get_float_arg(argvars, &f) == OK)
10711 rettv->vval.v_float = floor(f);
10712 else
10713 rettv->vval.v_float = 0.0;
10714}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010715
10716/*
10717 * "fmod()" function
10718 */
10719 static void
10720f_fmod(argvars, rettv)
10721 typval_T *argvars;
10722 typval_T *rettv;
10723{
10724 float_T fx, fy;
10725
10726 rettv->v_type = VAR_FLOAT;
10727 if (get_float_arg(argvars, &fx) == OK
10728 && get_float_arg(&argvars[1], &fy) == OK)
10729 rettv->vval.v_float = fmod(fx, fy);
10730 else
10731 rettv->vval.v_float = 0.0;
10732}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010733#endif
10734
Bram Moolenaar0d660222005-01-07 21:51:51 +000010735/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000010736 * "fnameescape({string})" function
10737 */
10738 static void
10739f_fnameescape(argvars, rettv)
10740 typval_T *argvars;
10741 typval_T *rettv;
10742{
10743 rettv->vval.v_string = vim_strsave_fnameescape(
10744 get_tv_string(&argvars[0]), FALSE);
10745 rettv->v_type = VAR_STRING;
10746}
10747
10748/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010749 * "fnamemodify({fname}, {mods})" function
10750 */
10751 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752f_fnamemodify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010753 typval_T *argvars;
10754 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010755{
10756 char_u *fname;
10757 char_u *mods;
10758 int usedlen = 0;
10759 int len;
10760 char_u *fbuf = NULL;
10761 char_u buf[NUMBUFLEN];
10762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010763 fname = get_tv_string_chk(&argvars[0]);
10764 mods = get_tv_string_buf_chk(&argvars[1], buf);
10765 if (fname == NULL || mods == NULL)
10766 fname = NULL;
10767 else
10768 {
10769 len = (int)STRLEN(fname);
10770 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
10771 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010773 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010774 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010775 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010777 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 vim_free(fbuf);
10779}
10780
Bram Moolenaar33570922005-01-25 22:26:29 +000010781static void foldclosed_both __ARGS((typval_T *argvars, typval_T *rettv, int end));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782
10783/*
10784 * "foldclosed()" function
10785 */
10786 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010787foldclosed_both(argvars, rettv, end)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010788 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010789 typval_T *rettv;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010790 int end UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791{
10792#ifdef FEAT_FOLDING
10793 linenr_T lnum;
10794 linenr_T first, last;
10795
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010796 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010797 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10798 {
10799 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
10800 {
10801 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010802 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010803 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010804 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010805 return;
10806 }
10807 }
10808#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010809 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010810}
10811
10812/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000010813 * "foldclosed()" function
10814 */
10815 static void
10816f_foldclosed(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010817 typval_T *argvars;
10818 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010819{
10820 foldclosed_both(argvars, rettv, FALSE);
10821}
10822
10823/*
10824 * "foldclosedend()" function
10825 */
10826 static void
10827f_foldclosedend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010828 typval_T *argvars;
10829 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000010830{
10831 foldclosed_both(argvars, rettv, TRUE);
10832}
10833
10834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835 * "foldlevel()" function
10836 */
10837 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010838f_foldlevel(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010010839 typval_T *argvars UNUSED;
10840 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010841{
10842#ifdef FEAT_FOLDING
10843 linenr_T lnum;
10844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010845 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010846 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010847 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010849}
10850
10851/*
10852 * "foldtext()" function
10853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010855f_foldtext(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010856 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010857 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010858{
10859#ifdef FEAT_FOLDING
10860 linenr_T lnum;
10861 char_u *s;
10862 char_u *r;
10863 int len;
10864 char *txt;
10865#endif
10866
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010867 rettv->v_type = VAR_STRING;
10868 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010869#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000010870 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
10871 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
10872 <= curbuf->b_ml.ml_line_count
10873 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010874 {
10875 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000010876 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
10877 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010878 {
10879 if (!linewhite(lnum))
10880 break;
10881 ++lnum;
10882 }
10883
10884 /* Find interesting text in this line. */
10885 s = skipwhite(ml_get(lnum));
10886 /* skip C comment-start */
10887 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010888 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000010889 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010890 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000010891 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000010892 {
10893 s = skipwhite(ml_get(lnum + 1));
10894 if (*s == '*')
10895 s = skipwhite(s + 1);
10896 }
10897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010898 txt = _("+-%s%3ld lines: ");
10899 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000010900 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 + 20 /* for %3ld */
10902 + STRLEN(s))); /* concatenated */
10903 if (r != NULL)
10904 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000010905 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
10906 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
10907 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000010908 len = (int)STRLEN(r);
10909 STRCAT(r, s);
10910 /* remove 'foldmarker' and 'commentstring' */
10911 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010912 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 }
10914 }
10915#endif
10916}
10917
10918/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010919 * "foldtextresult(lnum)" function
10920 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010921 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010922f_foldtextresult(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010923 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000010924 typval_T *rettv;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010925{
10926#ifdef FEAT_FOLDING
10927 linenr_T lnum;
10928 char_u *text;
10929 char_u buf[51];
10930 foldinfo_T foldinfo;
10931 int fold_count;
10932#endif
10933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010934 rettv->v_type = VAR_STRING;
10935 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010936#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010937 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010938 /* treat illegal types and illegal string values for {lnum} the same */
10939 if (lnum < 0)
10940 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010941 fold_count = foldedCount(curwin, lnum, &foldinfo);
10942 if (fold_count > 0)
10943 {
10944 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
10945 &foldinfo, buf);
10946 if (text == buf)
10947 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010948 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000010949 }
10950#endif
10951}
10952
10953/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010954 * "foreground()" function
10955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010956 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010957f_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000010958 typval_T *argvars UNUSED;
10959 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960{
Bram Moolenaar071d4272004-06-13 20:20:40 +000010961#ifdef FEAT_GUI
10962 if (gui.in_use)
10963 gui_mch_set_foreground();
10964#else
10965# ifdef WIN32
10966 win32_set_foreground();
10967# endif
10968#endif
10969}
10970
10971/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010972 * "function()" function
10973 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010974 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975f_function(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000010976 typval_T *argvars;
10977 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010978{
10979 char_u *s;
10980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010981 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010982 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010983 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010984 /* Don't check an autoload name for existence here. */
10985 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010986 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010987 else
10988 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010989 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010990 {
10991 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010992 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010993
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020010994 /* Expand s: and <SID> into <SNR>nr_, so that the function can
10995 * also be called from another script. Using trans_function_name()
10996 * would also work, but some plugins depend on the name being
10997 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020010998 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020010999 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011000 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011001 if (rettv->vval.v_string != NULL)
11002 {
11003 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011004 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011005 }
11006 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011007 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011008 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011009 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011010 }
11011}
11012
11013/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011014 * "garbagecollect()" function
11015 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011016 static void
11017f_garbagecollect(argvars, rettv)
11018 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011019 typval_T *rettv UNUSED;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011020{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000011021 /* This is postponed until we are back at the toplevel, because we may be
11022 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
11023 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000011024
11025 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
11026 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000011027}
11028
11029/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011030 * "get()" function
11031 */
11032 static void
11033f_get(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011034 typval_T *argvars;
11035 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011036{
Bram Moolenaar33570922005-01-25 22:26:29 +000011037 listitem_T *li;
11038 list_T *l;
11039 dictitem_T *di;
11040 dict_T *d;
11041 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011042
Bram Moolenaare9a41262005-01-15 22:18:47 +000011043 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011044 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011045 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011046 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011047 int error = FALSE;
11048
11049 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
11050 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011051 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011052 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011053 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011054 else if (argvars[0].v_type == VAR_DICT)
11055 {
11056 if ((d = argvars[0].vval.v_dict) != NULL)
11057 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011058 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011059 if (di != NULL)
11060 tv = &di->di_tv;
11061 }
11062 }
11063 else
11064 EMSG2(_(e_listdictarg), "get()");
11065
11066 if (tv == NULL)
11067 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011068 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011069 copy_tv(&argvars[2], rettv);
11070 }
11071 else
11072 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011073}
11074
Bram Moolenaar342337a2005-07-21 21:11:17 +000011075static void get_buffer_lines __ARGS((buf_T *buf, linenr_T start, linenr_T end, int retlist, typval_T *rettv));
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011076
11077/*
11078 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000011079 * Return a range (from start to end) of lines in rettv from the specified
11080 * buffer.
11081 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011082 */
11083 static void
Bram Moolenaar342337a2005-07-21 21:11:17 +000011084get_buffer_lines(buf, start, end, retlist, rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011085 buf_T *buf;
11086 linenr_T start;
11087 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011088 int retlist;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011089 typval_T *rettv;
11090{
11091 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011092
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011093 if (retlist && rettv_list_alloc(rettv) == FAIL)
11094 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011095
11096 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
11097 return;
11098
11099 if (!retlist)
11100 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011101 if (start >= 1 && start <= buf->b_ml.ml_line_count)
11102 p = ml_get_buf(buf, start, FALSE);
11103 else
11104 p = (char_u *)"";
11105
11106 rettv->v_type = VAR_STRING;
11107 rettv->vval.v_string = vim_strsave(p);
11108 }
11109 else
11110 {
11111 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011112 return;
11113
11114 if (start < 1)
11115 start = 1;
11116 if (end > buf->b_ml.ml_line_count)
11117 end = buf->b_ml.ml_line_count;
11118 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011119 if (list_append_string(rettv->vval.v_list,
11120 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011121 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011122 }
11123}
11124
11125/*
11126 * "getbufline()" function
11127 */
11128 static void
11129f_getbufline(argvars, rettv)
11130 typval_T *argvars;
11131 typval_T *rettv;
11132{
11133 linenr_T lnum;
11134 linenr_T end;
11135 buf_T *buf;
11136
11137 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11138 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011139 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011140 --emsg_off;
11141
Bram Moolenaar661b1822005-07-28 22:36:45 +000011142 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011143 if (argvars[2].v_type == VAR_UNKNOWN)
11144 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011145 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000011146 end = get_tv_lnum_buf(&argvars[2], buf);
11147
Bram Moolenaar342337a2005-07-21 21:11:17 +000011148 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011149}
11150
Bram Moolenaar0d660222005-01-07 21:51:51 +000011151/*
11152 * "getbufvar()" function
11153 */
11154 static void
11155f_getbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011156 typval_T *argvars;
11157 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011158{
11159 buf_T *buf;
11160 buf_T *save_curbuf;
11161 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011162 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011163 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011164
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011165 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
11166 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011167 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010011168 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011169
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011170 rettv->v_type = VAR_STRING;
11171 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011172
11173 if (buf != NULL && varname != NULL)
11174 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000011175 /* set curbuf to be our buf, temporarily */
11176 save_curbuf = curbuf;
11177 curbuf = buf;
11178
Bram Moolenaar0d660222005-01-07 21:51:51 +000011179 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011180 {
11181 if (get_option_tv(&varname, rettv, TRUE) == OK)
11182 done = TRUE;
11183 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010011184 else if (STRCMP(varname, "changedtick") == 0)
11185 {
11186 rettv->v_type = VAR_NUMBER;
11187 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011188 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010011189 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011190 else
11191 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011192 /* Look up the variable. */
11193 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
11194 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
11195 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011196 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011197 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011198 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011199 done = TRUE;
11200 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011201 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000011202
11203 /* restore previous notion of curbuf */
11204 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011205 }
11206
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011207 if (!done && argvars[2].v_type != VAR_UNKNOWN)
11208 /* use the default value */
11209 copy_tv(&argvars[2], rettv);
11210
Bram Moolenaar0d660222005-01-07 21:51:51 +000011211 --emsg_off;
11212}
11213
11214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011215 * "getchar()" function
11216 */
11217 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011218f_getchar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011219 typval_T *argvars;
11220 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011221{
11222 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011223 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011224
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000011225 /* Position the cursor. Needed after a message that ends in a space. */
11226 windgoto(msg_row, msg_col);
11227
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 ++no_mapping;
11229 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000011230 for (;;)
11231 {
11232 if (argvars[0].v_type == VAR_UNKNOWN)
11233 /* getchar(): blocking wait. */
11234 n = safe_vgetc();
11235 else if (get_tv_number_chk(&argvars[0], &error) == 1)
11236 /* getchar(1): only check if char avail */
11237 n = vpeekc();
11238 else if (error || vpeekc() == NUL)
11239 /* illegal argument or getchar(0) and no char avail: return zero */
11240 n = 0;
11241 else
11242 /* getchar(0) and char avail: return char */
11243 n = safe_vgetc();
11244 if (n == K_IGNORE)
11245 continue;
11246 break;
11247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 --no_mapping;
11249 --allow_keys;
11250
Bram Moolenaar219b8702006-11-01 14:32:36 +000011251 vimvars[VV_MOUSE_WIN].vv_nr = 0;
11252 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
11253 vimvars[VV_MOUSE_COL].vv_nr = 0;
11254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011255 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011256 if (IS_SPECIAL(n) || mod_mask != 0)
11257 {
11258 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
11259 int i = 0;
11260
11261 /* Turn a special key into three bytes, plus modifier. */
11262 if (mod_mask != 0)
11263 {
11264 temp[i++] = K_SPECIAL;
11265 temp[i++] = KS_MODIFIER;
11266 temp[i++] = mod_mask;
11267 }
11268 if (IS_SPECIAL(n))
11269 {
11270 temp[i++] = K_SPECIAL;
11271 temp[i++] = K_SECOND(n);
11272 temp[i++] = K_THIRD(n);
11273 }
11274#ifdef FEAT_MBYTE
11275 else if (has_mbyte)
11276 i += (*mb_char2bytes)(n, temp + i);
11277#endif
11278 else
11279 temp[i++] = n;
11280 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011281 rettv->v_type = VAR_STRING;
11282 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000011283
11284#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010011285 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000011286 {
11287 int row = mouse_row;
11288 int col = mouse_col;
11289 win_T *win;
11290 linenr_T lnum;
11291# ifdef FEAT_WINDOWS
11292 win_T *wp;
11293# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011294 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011295
11296 if (row >= 0 && col >= 0)
11297 {
11298 /* Find the window at the mouse coordinates and compute the
11299 * text position. */
11300 win = mouse_find_win(&row, &col);
11301 (void)mouse_comp_pos(win, &row, &col, &lnum);
11302# ifdef FEAT_WINDOWS
11303 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011304 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011305# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000011306 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000011307 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
11308 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
11309 }
11310 }
11311#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011312 }
11313}
11314
11315/*
11316 * "getcharmod()" function
11317 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011318 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011319f_getcharmod(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011320 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011321 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011322{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011323 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011324}
11325
11326/*
11327 * "getcmdline()" function
11328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011330f_getcmdline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011331 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011332 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011334 rettv->v_type = VAR_STRING;
11335 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336}
11337
11338/*
11339 * "getcmdpos()" function
11340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011341 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011342f_getcmdpos(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011343 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011344 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011346 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011347}
11348
11349/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011350 * "getcmdtype()" function
11351 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011352 static void
11353f_getcmdtype(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011354 typval_T *argvars UNUSED;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000011355 typval_T *rettv;
11356{
11357 rettv->v_type = VAR_STRING;
11358 rettv->vval.v_string = alloc(2);
11359 if (rettv->vval.v_string != NULL)
11360 {
11361 rettv->vval.v_string[0] = get_cmdline_type();
11362 rettv->vval.v_string[1] = NUL;
11363 }
11364}
11365
11366/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011367 * "getcwd()" function
11368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011369 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011370f_getcwd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011371 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011372 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373{
Bram Moolenaard9462e32011-04-11 21:35:11 +020011374 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011376 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020011377 rettv->vval.v_string = NULL;
11378 cwd = alloc(MAXPATHL);
11379 if (cwd != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011380 {
Bram Moolenaard9462e32011-04-11 21:35:11 +020011381 if (mch_dirname(cwd, MAXPATHL) != FAIL)
11382 {
11383 rettv->vval.v_string = vim_strsave(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011384#ifdef BACKSLASH_IN_FILENAME
Bram Moolenaard9462e32011-04-11 21:35:11 +020011385 if (rettv->vval.v_string != NULL)
11386 slash_adjust(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387#endif
Bram Moolenaard9462e32011-04-11 21:35:11 +020011388 }
11389 vim_free(cwd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011390 }
11391}
11392
11393/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011394 * "getfontname()" function
11395 */
11396 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011397f_getfontname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011398 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011399 typval_T *rettv;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011400{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 rettv->v_type = VAR_STRING;
11402 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011403#ifdef FEAT_GUI
11404 if (gui.in_use)
11405 {
11406 GuiFont font;
11407 char_u *name = NULL;
11408
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011409 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011410 {
11411 /* Get the "Normal" font. Either the name saved by
11412 * hl_set_font_name() or from the font ID. */
11413 font = gui.norm_font;
11414 name = hl_get_font_name();
11415 }
11416 else
11417 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011418 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011419 if (STRCMP(name, "*") == 0) /* don't use font dialog */
11420 return;
11421 font = gui_mch_get_font(name, FALSE);
11422 if (font == NOFONT)
11423 return; /* Invalid font name, return empty string. */
11424 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011425 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011426 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000011427 gui_mch_free_font(font);
11428 }
11429#endif
11430}
11431
11432/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011433 * "getfperm({fname})" function
11434 */
11435 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011436f_getfperm(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011437 typval_T *argvars;
11438 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011439{
11440 char_u *fname;
11441 struct stat st;
11442 char_u *perm = NULL;
11443 char_u flags[] = "rwx";
11444 int i;
11445
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011446 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011447
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011448 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011449 if (mch_stat((char *)fname, &st) >= 0)
11450 {
11451 perm = vim_strsave((char_u *)"---------");
11452 if (perm != NULL)
11453 {
11454 for (i = 0; i < 9; i++)
11455 {
11456 if (st.st_mode & (1 << (8 - i)))
11457 perm[i] = flags[i % 3];
11458 }
11459 }
11460 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011461 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011462}
11463
11464/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011465 * "getfsize({fname})" function
11466 */
11467 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011468f_getfsize(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011469 typval_T *argvars;
11470 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011471{
11472 char_u *fname;
11473 struct stat st;
11474
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011475 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011476
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011477 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478
11479 if (mch_stat((char *)fname, &st) >= 0)
11480 {
11481 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011482 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011483 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000011484 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011485 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000011486
11487 /* non-perfect check for overflow */
11488 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
11489 rettv->vval.v_number = -2;
11490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491 }
11492 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011493 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494}
11495
11496/*
11497 * "getftime({fname})" function
11498 */
11499 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011500f_getftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011501 typval_T *argvars;
11502 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011503{
11504 char_u *fname;
11505 struct stat st;
11506
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011507 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011508
11509 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011510 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011512 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513}
11514
11515/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011516 * "getftype({fname})" function
11517 */
11518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011519f_getftype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011520 typval_T *argvars;
11521 typval_T *rettv;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011522{
11523 char_u *fname;
11524 struct stat st;
11525 char_u *type = NULL;
11526 char *t;
11527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011528 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011529
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011530 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011531 if (mch_lstat((char *)fname, &st) >= 0)
11532 {
11533#ifdef S_ISREG
11534 if (S_ISREG(st.st_mode))
11535 t = "file";
11536 else if (S_ISDIR(st.st_mode))
11537 t = "dir";
11538# ifdef S_ISLNK
11539 else if (S_ISLNK(st.st_mode))
11540 t = "link";
11541# endif
11542# ifdef S_ISBLK
11543 else if (S_ISBLK(st.st_mode))
11544 t = "bdev";
11545# endif
11546# ifdef S_ISCHR
11547 else if (S_ISCHR(st.st_mode))
11548 t = "cdev";
11549# endif
11550# ifdef S_ISFIFO
11551 else if (S_ISFIFO(st.st_mode))
11552 t = "fifo";
11553# endif
11554# ifdef S_ISSOCK
11555 else if (S_ISSOCK(st.st_mode))
11556 t = "fifo";
11557# endif
11558 else
11559 t = "other";
11560#else
11561# ifdef S_IFMT
11562 switch (st.st_mode & S_IFMT)
11563 {
11564 case S_IFREG: t = "file"; break;
11565 case S_IFDIR: t = "dir"; break;
11566# ifdef S_IFLNK
11567 case S_IFLNK: t = "link"; break;
11568# endif
11569# ifdef S_IFBLK
11570 case S_IFBLK: t = "bdev"; break;
11571# endif
11572# ifdef S_IFCHR
11573 case S_IFCHR: t = "cdev"; break;
11574# endif
11575# ifdef S_IFIFO
11576 case S_IFIFO: t = "fifo"; break;
11577# endif
11578# ifdef S_IFSOCK
11579 case S_IFSOCK: t = "socket"; break;
11580# endif
11581 default: t = "other";
11582 }
11583# else
11584 if (mch_isdir(fname))
11585 t = "dir";
11586 else
11587 t = "file";
11588# endif
11589#endif
11590 type = vim_strsave((char_u *)t);
11591 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011592 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000011593}
11594
11595/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011596 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000011597 */
11598 static void
11599f_getline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011600 typval_T *argvars;
11601 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011602{
11603 linenr_T lnum;
11604 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011605 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000011606
11607 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011608 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000011609 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011610 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000011611 retlist = FALSE;
11612 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000011613 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000011614 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000011615 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000011616 retlist = TRUE;
11617 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000011618
Bram Moolenaar342337a2005-07-21 21:11:17 +000011619 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011620}
11621
11622/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011623 * "getmatches()" function
11624 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011625 static void
11626f_getmatches(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011627 typval_T *argvars UNUSED;
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010011628 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011629{
11630#ifdef FEAT_SEARCH_EXTRA
11631 dict_T *dict;
11632 matchitem_T *cur = curwin->w_match_head;
11633
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011634 if (rettv_list_alloc(rettv) == OK)
11635 {
11636 while (cur != NULL)
11637 {
11638 dict = dict_alloc();
11639 if (dict == NULL)
11640 return;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000011641 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
11642 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
11643 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
11644 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
11645 list_append_dict(rettv->vval.v_list, dict);
11646 cur = cur->next;
11647 }
11648 }
11649#endif
11650}
11651
11652/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000011653 * "getpid()" function
11654 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000011655 static void
11656f_getpid(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011657 typval_T *argvars UNUSED;
Bram Moolenaar18081e32008-02-20 19:11:07 +000011658 typval_T *rettv;
11659{
11660 rettv->vval.v_number = mch_get_pid();
11661}
11662
11663/*
Bram Moolenaara5525202006-03-02 22:52:09 +000011664 * "getpos(string)" function
11665 */
11666 static void
11667f_getpos(argvars, rettv)
11668 typval_T *argvars;
11669 typval_T *rettv;
11670{
11671 pos_T *fp;
11672 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011673 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011674
11675 if (rettv_list_alloc(rettv) == OK)
11676 {
11677 l = rettv->vval.v_list;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011678 fp = var2fpos(&argvars[0], TRUE, &fnum);
11679 if (fnum != -1)
11680 list_append_number(l, (varnumber_T)fnum);
11681 else
11682 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000011683 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
11684 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000011685 list_append_number(l, (fp != NULL)
11686 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000011687 : (varnumber_T)0);
11688 list_append_number(l,
11689#ifdef FEAT_VIRTUALEDIT
11690 (fp != NULL) ? (varnumber_T)fp->coladd :
11691#endif
11692 (varnumber_T)0);
11693 }
11694 else
11695 rettv->vval.v_number = FALSE;
11696}
11697
11698/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000011699 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000011700 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000011701 static void
Bram Moolenaar280f1262006-01-30 00:14:18 +000011702f_getqflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011703 typval_T *argvars UNUSED;
11704 typval_T *rettv UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011705{
11706#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000011707 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000011708#endif
11709
Bram Moolenaar2641f772005-03-25 21:58:17 +000011710#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011711 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000011712 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000011713 wp = NULL;
11714 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
11715 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011716 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011717 if (wp == NULL)
11718 return;
11719 }
11720
Bram Moolenaareddf53b2006-02-27 00:11:10 +000011721 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000011722 }
11723#endif
11724}
11725
11726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 * "getreg()" function
11728 */
11729 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730f_getreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011731 typval_T *argvars;
11732 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733{
11734 char_u *strregname;
11735 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011736 int arg2 = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011737 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011739 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011741 strregname = get_tv_string_chk(&argvars[0]);
11742 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011743 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011744 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000011745 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011746 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011747 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011748 regname = (strregname == NULL ? '"' : *strregname);
11749 if (regname == 0)
11750 regname = '"';
11751
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011753 rettv->vval.v_string = error ? NULL :
11754 get_reg_contents(regname, TRUE, arg2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755}
11756
11757/*
11758 * "getregtype()" function
11759 */
11760 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761f_getregtype(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011762 typval_T *argvars;
11763 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764{
11765 char_u *strregname;
11766 int regname;
11767 char_u buf[NUMBUFLEN + 2];
11768 long reglen = 0;
11769
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011770 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011771 {
11772 strregname = get_tv_string_chk(&argvars[0]);
11773 if (strregname == NULL) /* type error; errmsg already given */
11774 {
11775 rettv->v_type = VAR_STRING;
11776 rettv->vval.v_string = NULL;
11777 return;
11778 }
11779 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780 else
11781 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011782 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783
11784 regname = (strregname == NULL ? '"' : *strregname);
11785 if (regname == 0)
11786 regname = '"';
11787
11788 buf[0] = NUL;
11789 buf[1] = NUL;
11790 switch (get_reg_type(regname, &reglen))
11791 {
11792 case MLINE: buf[0] = 'V'; break;
11793 case MCHAR: buf[0] = 'v'; break;
11794#ifdef FEAT_VISUAL
11795 case MBLOCK:
11796 buf[0] = Ctrl_V;
11797 sprintf((char *)buf + 1, "%ld", reglen + 1);
11798 break;
11799#endif
11800 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011801 rettv->v_type = VAR_STRING;
11802 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011803}
11804
11805/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011806 * "gettabvar()" function
11807 */
11808 static void
11809f_gettabvar(argvars, rettv)
11810 typval_T *argvars;
11811 typval_T *rettv;
11812{
11813 tabpage_T *tp;
11814 dictitem_T *v;
11815 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011816 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011817
11818 rettv->v_type = VAR_STRING;
11819 rettv->vval.v_string = NULL;
11820
11821 varname = get_tv_string_chk(&argvars[1]);
11822 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11823 if (tp != NULL && varname != NULL)
11824 {
11825 /* look up the variable */
Bram Moolenaar332ac062013-04-15 13:06:21 +020011826 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 0, varname, FALSE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011827 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011828 {
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011829 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011830 done = TRUE;
11831 }
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011832 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011833
11834 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010011835 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020011836}
11837
11838/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011839 * "gettabwinvar()" function
11840 */
11841 static void
11842f_gettabwinvar(argvars, rettv)
11843 typval_T *argvars;
11844 typval_T *rettv;
11845{
11846 getwinvar(argvars, rettv, 1);
11847}
11848
11849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011850 * "getwinposx()" function
11851 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011853f_getwinposx(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011854 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011855 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011857 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858#ifdef FEAT_GUI
11859 if (gui.in_use)
11860 {
11861 int x, y;
11862
11863 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011864 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 }
11866#endif
11867}
11868
11869/*
11870 * "getwinposy()" function
11871 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011873f_getwinposy(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000011874 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000011875 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011877 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011878#ifdef FEAT_GUI
11879 if (gui.in_use)
11880 {
11881 int x, y;
11882
11883 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011884 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011885 }
11886#endif
11887}
11888
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011889/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011890 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011891 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011892 static win_T *
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011893find_win_by_nr(vp, tp)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011894 typval_T *vp;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011895 tabpage_T *tp UNUSED; /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000011896{
11897#ifdef FEAT_WINDOWS
11898 win_T *wp;
11899#endif
11900 int nr;
11901
11902 nr = get_tv_number_chk(vp, NULL);
11903
11904#ifdef FEAT_WINDOWS
11905 if (nr < 0)
11906 return NULL;
11907 if (nr == 0)
11908 return curwin;
11909
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011910 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
11911 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000011912 if (--nr <= 0)
11913 break;
11914 return wp;
11915#else
11916 if (nr == 0 || nr == 1)
11917 return curwin;
11918 return NULL;
11919#endif
11920}
11921
Bram Moolenaar071d4272004-06-13 20:20:40 +000011922/*
11923 * "getwinvar()" function
11924 */
11925 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011926f_getwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000011927 typval_T *argvars;
11928 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011929{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011930 getwinvar(argvars, rettv, 0);
11931}
11932
11933/*
11934 * getwinvar() and gettabwinvar()
11935 */
11936 static void
11937getwinvar(argvars, rettv, off)
11938 typval_T *argvars;
11939 typval_T *rettv;
11940 int off; /* 1 for gettabwinvar() */
11941{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011942 win_T *win, *oldcurwin;
11943 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000011944 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020011945 tabpage_T *tp = NULL;
11946 tabpage_T *oldtabpage;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011947 int done = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011948
Bram Moolenaar99ebf042006-04-15 20:28:54 +000011949#ifdef FEAT_WINDOWS
11950 if (off == 1)
11951 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
11952 else
11953 tp = curtab;
11954#endif
11955 win = find_win_by_nr(&argvars[off], tp);
11956 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011957 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011958
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011959 rettv->v_type = VAR_STRING;
11960 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011961
11962 if (win != NULL && varname != NULL)
11963 {
Bram Moolenaar105bc352013-05-17 16:03:57 +020011964 /* Set curwin to be our win, temporarily. Also set the tabpage,
11965 * otherwise the window is not valid. */
Bram Moolenaard6949742013-06-16 14:18:28 +020011966 switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE);
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011967
Bram Moolenaar071d4272004-06-13 20:20:40 +000011968 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011969 {
11970 if (get_option_tv(&varname, rettv, 1) == OK)
11971 done = TRUE;
11972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011973 else
11974 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020011975 /* Look up the variable. */
11976 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
11977 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w', varname, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011978 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011979 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011980 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011981 done = TRUE;
11982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011983 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000011984
11985 /* restore previous notion of curwin */
Bram Moolenaard6949742013-06-16 14:18:28 +020011986 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011987 }
11988
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020011989 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
11990 /* use the default return value */
11991 copy_tv(&argvars[off + 2], rettv);
11992
Bram Moolenaar071d4272004-06-13 20:20:40 +000011993 --emsg_off;
11994}
11995
11996/*
11997 * "glob()" function
11998 */
11999 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012000f_glob(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012001 typval_T *argvars;
12002 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012003{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012004 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012005 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012006 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012007
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012008 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012009 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012010 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012011 if (argvars[1].v_type != VAR_UNKNOWN)
12012 {
12013 if (get_tv_number_chk(&argvars[1], &error))
12014 options |= WILD_KEEP_ALL;
12015 if (argvars[2].v_type != VAR_UNKNOWN
12016 && get_tv_number_chk(&argvars[2], &error))
12017 {
12018 rettv->v_type = VAR_LIST;
12019 rettv->vval.v_list = NULL;
12020 }
12021 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012022 if (!error)
12023 {
12024 ExpandInit(&xpc);
12025 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012026 if (p_wic)
12027 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012028 if (rettv->v_type == VAR_STRING)
12029 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010012030 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010012031 else if (rettv_list_alloc(rettv) != FAIL)
12032 {
12033 int i;
12034
12035 ExpandOne(&xpc, get_tv_string(&argvars[0]),
12036 NULL, options, WILD_ALL_KEEP);
12037 for (i = 0; i < xpc.xp_numfiles; i++)
12038 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
12039
12040 ExpandCleanup(&xpc);
12041 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012042 }
12043 else
12044 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012045}
12046
12047/*
12048 * "globpath()" function
12049 */
12050 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012051f_globpath(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012052 typval_T *argvars;
12053 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012054{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012055 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012056 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012057 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012058 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012059
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012060 /* When the optional second argument is non-zero, don't remove matches
12061 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
12062 if (argvars[2].v_type != VAR_UNKNOWN
12063 && get_tv_number_chk(&argvars[2], &error))
12064 flags |= WILD_KEEP_ALL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012065 rettv->v_type = VAR_STRING;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012066 if (file == NULL || error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012067 rettv->vval.v_string = NULL;
12068 else
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000012069 rettv->vval.v_string = globpath(get_tv_string(&argvars[0]), file,
12070 flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012071}
12072
12073/*
12074 * "has()" function
12075 */
12076 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012077f_has(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012078 typval_T *argvars;
12079 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012080{
12081 int i;
12082 char_u *name;
12083 int n = FALSE;
12084 static char *(has_list[]) =
12085 {
12086#ifdef AMIGA
12087 "amiga",
12088# ifdef FEAT_ARP
12089 "arp",
12090# endif
12091#endif
12092#ifdef __BEOS__
12093 "beos",
12094#endif
12095#ifdef MSDOS
12096# ifdef DJGPP
12097 "dos32",
12098# else
12099 "dos16",
12100# endif
12101#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000012102#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000012103 "mac",
12104#endif
12105#if defined(MACOS_X_UNIX)
12106 "macunix",
12107#endif
12108#ifdef OS2
12109 "os2",
12110#endif
12111#ifdef __QNX__
12112 "qnx",
12113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012114#ifdef UNIX
12115 "unix",
12116#endif
12117#ifdef VMS
12118 "vms",
12119#endif
12120#ifdef WIN16
12121 "win16",
12122#endif
12123#ifdef WIN32
12124 "win32",
12125#endif
12126#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
12127 "win32unix",
12128#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010012129#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012130 "win64",
12131#endif
12132#ifdef EBCDIC
12133 "ebcdic",
12134#endif
12135#ifndef CASE_INSENSITIVE_FILENAME
12136 "fname_case",
12137#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012138#ifdef HAVE_ACL
12139 "acl",
12140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141#ifdef FEAT_ARABIC
12142 "arabic",
12143#endif
12144#ifdef FEAT_AUTOCMD
12145 "autocmd",
12146#endif
12147#ifdef FEAT_BEVAL
12148 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000012149# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
12150 "balloon_multiline",
12151# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012152#endif
12153#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
12154 "builtin_terms",
12155# ifdef ALL_BUILTIN_TCAPS
12156 "all_builtin_terms",
12157# endif
12158#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020012159#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
12160 || defined(FEAT_GUI_W32) \
12161 || defined(FEAT_GUI_MOTIF))
12162 "browsefilter",
12163#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164#ifdef FEAT_BYTEOFF
12165 "byte_offset",
12166#endif
12167#ifdef FEAT_CINDENT
12168 "cindent",
12169#endif
12170#ifdef FEAT_CLIENTSERVER
12171 "clientserver",
12172#endif
12173#ifdef FEAT_CLIPBOARD
12174 "clipboard",
12175#endif
12176#ifdef FEAT_CMDL_COMPL
12177 "cmdline_compl",
12178#endif
12179#ifdef FEAT_CMDHIST
12180 "cmdline_hist",
12181#endif
12182#ifdef FEAT_COMMENTS
12183 "comments",
12184#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012185#ifdef FEAT_CONCEAL
12186 "conceal",
12187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188#ifdef FEAT_CRYPT
12189 "cryptv",
12190#endif
12191#ifdef FEAT_CSCOPE
12192 "cscope",
12193#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020012194#ifdef FEAT_CURSORBIND
12195 "cursorbind",
12196#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000012197#ifdef CURSOR_SHAPE
12198 "cursorshape",
12199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200#ifdef DEBUG
12201 "debug",
12202#endif
12203#ifdef FEAT_CON_DIALOG
12204 "dialog_con",
12205#endif
12206#ifdef FEAT_GUI_DIALOG
12207 "dialog_gui",
12208#endif
12209#ifdef FEAT_DIFF
12210 "diff",
12211#endif
12212#ifdef FEAT_DIGRAPHS
12213 "digraphs",
12214#endif
12215#ifdef FEAT_DND
12216 "dnd",
12217#endif
12218#ifdef FEAT_EMACS_TAGS
12219 "emacs_tags",
12220#endif
12221 "eval", /* always present, of course! */
12222#ifdef FEAT_EX_EXTRA
12223 "ex_extra",
12224#endif
12225#ifdef FEAT_SEARCH_EXTRA
12226 "extra_search",
12227#endif
12228#ifdef FEAT_FKMAP
12229 "farsi",
12230#endif
12231#ifdef FEAT_SEARCHPATH
12232 "file_in_path",
12233#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020012234#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000012235 "filterpipe",
12236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012237#ifdef FEAT_FIND_ID
12238 "find_in_path",
12239#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012240#ifdef FEAT_FLOAT
12241 "float",
12242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012243#ifdef FEAT_FOLDING
12244 "folding",
12245#endif
12246#ifdef FEAT_FOOTER
12247 "footer",
12248#endif
12249#if !defined(USE_SYSTEM) && defined(UNIX)
12250 "fork",
12251#endif
12252#ifdef FEAT_GETTEXT
12253 "gettext",
12254#endif
12255#ifdef FEAT_GUI
12256 "gui",
12257#endif
12258#ifdef FEAT_GUI_ATHENA
12259# ifdef FEAT_GUI_NEXTAW
12260 "gui_neXtaw",
12261# else
12262 "gui_athena",
12263# endif
12264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012265#ifdef FEAT_GUI_GTK
12266 "gui_gtk",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012267 "gui_gtk2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000012268#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000012269#ifdef FEAT_GUI_GNOME
12270 "gui_gnome",
12271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012272#ifdef FEAT_GUI_MAC
12273 "gui_mac",
12274#endif
12275#ifdef FEAT_GUI_MOTIF
12276 "gui_motif",
12277#endif
12278#ifdef FEAT_GUI_PHOTON
12279 "gui_photon",
12280#endif
12281#ifdef FEAT_GUI_W16
12282 "gui_win16",
12283#endif
12284#ifdef FEAT_GUI_W32
12285 "gui_win32",
12286#endif
12287#ifdef FEAT_HANGULIN
12288 "hangul_input",
12289#endif
12290#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
12291 "iconv",
12292#endif
12293#ifdef FEAT_INS_EXPAND
12294 "insert_expand",
12295#endif
12296#ifdef FEAT_JUMPLIST
12297 "jumplist",
12298#endif
12299#ifdef FEAT_KEYMAP
12300 "keymap",
12301#endif
12302#ifdef FEAT_LANGMAP
12303 "langmap",
12304#endif
12305#ifdef FEAT_LIBCALL
12306 "libcall",
12307#endif
12308#ifdef FEAT_LINEBREAK
12309 "linebreak",
12310#endif
12311#ifdef FEAT_LISP
12312 "lispindent",
12313#endif
12314#ifdef FEAT_LISTCMDS
12315 "listcmds",
12316#endif
12317#ifdef FEAT_LOCALMAP
12318 "localmap",
12319#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012320#ifdef FEAT_LUA
12321# ifndef DYNAMIC_LUA
12322 "lua",
12323# endif
12324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012325#ifdef FEAT_MENU
12326 "menu",
12327#endif
12328#ifdef FEAT_SESSION
12329 "mksession",
12330#endif
12331#ifdef FEAT_MODIFY_FNAME
12332 "modify_fname",
12333#endif
12334#ifdef FEAT_MOUSE
12335 "mouse",
12336#endif
12337#ifdef FEAT_MOUSESHAPE
12338 "mouseshape",
12339#endif
12340#if defined(UNIX) || defined(VMS)
12341# ifdef FEAT_MOUSE_DEC
12342 "mouse_dec",
12343# endif
12344# ifdef FEAT_MOUSE_GPM
12345 "mouse_gpm",
12346# endif
12347# ifdef FEAT_MOUSE_JSB
12348 "mouse_jsbterm",
12349# endif
12350# ifdef FEAT_MOUSE_NET
12351 "mouse_netterm",
12352# endif
12353# ifdef FEAT_MOUSE_PTERM
12354 "mouse_pterm",
12355# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012356# ifdef FEAT_MOUSE_SGR
12357 "mouse_sgr",
12358# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012359# ifdef FEAT_SYSMOUSE
12360 "mouse_sysmouse",
12361# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020012362# ifdef FEAT_MOUSE_URXVT
12363 "mouse_urxvt",
12364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365# ifdef FEAT_MOUSE_XTERM
12366 "mouse_xterm",
12367# endif
12368#endif
12369#ifdef FEAT_MBYTE
12370 "multi_byte",
12371#endif
12372#ifdef FEAT_MBYTE_IME
12373 "multi_byte_ime",
12374#endif
12375#ifdef FEAT_MULTI_LANG
12376 "multi_lang",
12377#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012378#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000012379#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000012380 "mzscheme",
12381#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012382#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012383#ifdef FEAT_OLE
12384 "ole",
12385#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012386#ifdef FEAT_PATH_EXTRA
12387 "path_extra",
12388#endif
12389#ifdef FEAT_PERL
12390#ifndef DYNAMIC_PERL
12391 "perl",
12392#endif
12393#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020012394#ifdef FEAT_PERSISTENT_UNDO
12395 "persistent_undo",
12396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397#ifdef FEAT_PYTHON
12398#ifndef DYNAMIC_PYTHON
12399 "python",
12400#endif
12401#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012402#ifdef FEAT_PYTHON3
12403#ifndef DYNAMIC_PYTHON3
12404 "python3",
12405#endif
12406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407#ifdef FEAT_POSTSCRIPT
12408 "postscript",
12409#endif
12410#ifdef FEAT_PRINTER
12411 "printer",
12412#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000012413#ifdef FEAT_PROFILE
12414 "profile",
12415#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000012416#ifdef FEAT_RELTIME
12417 "reltime",
12418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012419#ifdef FEAT_QUICKFIX
12420 "quickfix",
12421#endif
12422#ifdef FEAT_RIGHTLEFT
12423 "rightleft",
12424#endif
12425#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
12426 "ruby",
12427#endif
12428#ifdef FEAT_SCROLLBIND
12429 "scrollbind",
12430#endif
12431#ifdef FEAT_CMDL_INFO
12432 "showcmd",
12433 "cmdline_info",
12434#endif
12435#ifdef FEAT_SIGNS
12436 "signs",
12437#endif
12438#ifdef FEAT_SMARTINDENT
12439 "smartindent",
12440#endif
12441#ifdef FEAT_SNIFF
12442 "sniff",
12443#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000012444#ifdef STARTUPTIME
12445 "startuptime",
12446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447#ifdef FEAT_STL_OPT
12448 "statusline",
12449#endif
12450#ifdef FEAT_SUN_WORKSHOP
12451 "sun_workshop",
12452#endif
12453#ifdef FEAT_NETBEANS_INTG
12454 "netbeans_intg",
12455#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000012456#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000012457 "spell",
12458#endif
12459#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000012460 "syntax",
12461#endif
12462#if defined(USE_SYSTEM) || !defined(UNIX)
12463 "system",
12464#endif
12465#ifdef FEAT_TAG_BINS
12466 "tag_binary",
12467#endif
12468#ifdef FEAT_TAG_OLDSTATIC
12469 "tag_old_static",
12470#endif
12471#ifdef FEAT_TAG_ANYWHITE
12472 "tag_any_white",
12473#endif
12474#ifdef FEAT_TCL
12475# ifndef DYNAMIC_TCL
12476 "tcl",
12477# endif
12478#endif
12479#ifdef TERMINFO
12480 "terminfo",
12481#endif
12482#ifdef FEAT_TERMRESPONSE
12483 "termresponse",
12484#endif
12485#ifdef FEAT_TEXTOBJ
12486 "textobjects",
12487#endif
12488#ifdef HAVE_TGETENT
12489 "tgetent",
12490#endif
12491#ifdef FEAT_TITLE
12492 "title",
12493#endif
12494#ifdef FEAT_TOOLBAR
12495 "toolbar",
12496#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010012497#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
12498 "unnamedplus",
12499#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500#ifdef FEAT_USR_CMDS
12501 "user-commands", /* was accidentally included in 5.4 */
12502 "user_commands",
12503#endif
12504#ifdef FEAT_VIMINFO
12505 "viminfo",
12506#endif
12507#ifdef FEAT_VERTSPLIT
12508 "vertsplit",
12509#endif
12510#ifdef FEAT_VIRTUALEDIT
12511 "virtualedit",
12512#endif
12513#ifdef FEAT_VISUAL
12514 "visual",
12515#endif
12516#ifdef FEAT_VISUALEXTRA
12517 "visualextra",
12518#endif
12519#ifdef FEAT_VREPLACE
12520 "vreplace",
12521#endif
12522#ifdef FEAT_WILDIGN
12523 "wildignore",
12524#endif
12525#ifdef FEAT_WILDMENU
12526 "wildmenu",
12527#endif
12528#ifdef FEAT_WINDOWS
12529 "windows",
12530#endif
12531#ifdef FEAT_WAK
12532 "winaltkeys",
12533#endif
12534#ifdef FEAT_WRITEBACKUP
12535 "writebackup",
12536#endif
12537#ifdef FEAT_XIM
12538 "xim",
12539#endif
12540#ifdef FEAT_XFONTSET
12541 "xfontset",
12542#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012543#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020012544 "xpm",
12545 "xpm_w32", /* for backward compatibility */
12546#else
12547# if defined(HAVE_XPM)
12548 "xpm",
12549# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010012550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012551#ifdef USE_XSMP
12552 "xsmp",
12553#endif
12554#ifdef USE_XSMP_INTERACT
12555 "xsmp_interact",
12556#endif
12557#ifdef FEAT_XCLIPBOARD
12558 "xterm_clipboard",
12559#endif
12560#ifdef FEAT_XTERM_SAVE
12561 "xterm_save",
12562#endif
12563#if defined(UNIX) && defined(FEAT_X11)
12564 "X11",
12565#endif
12566 NULL
12567 };
12568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012569 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012570 for (i = 0; has_list[i] != NULL; ++i)
12571 if (STRICMP(name, has_list[i]) == 0)
12572 {
12573 n = TRUE;
12574 break;
12575 }
12576
12577 if (n == FALSE)
12578 {
12579 if (STRNICMP(name, "patch", 5) == 0)
12580 n = has_patch(atoi((char *)name + 5));
12581 else if (STRICMP(name, "vim_starting") == 0)
12582 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000012583#ifdef FEAT_MBYTE
12584 else if (STRICMP(name, "multi_byte_encoding") == 0)
12585 n = has_mbyte;
12586#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000012587#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
12588 else if (STRICMP(name, "balloon_multiline") == 0)
12589 n = multiline_balloon_available();
12590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012591#ifdef DYNAMIC_TCL
12592 else if (STRICMP(name, "tcl") == 0)
12593 n = tcl_enabled(FALSE);
12594#endif
12595#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
12596 else if (STRICMP(name, "iconv") == 0)
12597 n = iconv_enabled(FALSE);
12598#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020012599#ifdef DYNAMIC_LUA
12600 else if (STRICMP(name, "lua") == 0)
12601 n = lua_enabled(FALSE);
12602#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000012603#ifdef DYNAMIC_MZSCHEME
12604 else if (STRICMP(name, "mzscheme") == 0)
12605 n = mzscheme_enabled(FALSE);
12606#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012607#ifdef DYNAMIC_RUBY
12608 else if (STRICMP(name, "ruby") == 0)
12609 n = ruby_enabled(FALSE);
12610#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012611#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000012612#ifdef DYNAMIC_PYTHON
12613 else if (STRICMP(name, "python") == 0)
12614 n = python_enabled(FALSE);
12615#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020012616#endif
12617#ifdef FEAT_PYTHON3
12618#ifdef DYNAMIC_PYTHON3
12619 else if (STRICMP(name, "python3") == 0)
12620 n = python3_enabled(FALSE);
12621#endif
12622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623#ifdef DYNAMIC_PERL
12624 else if (STRICMP(name, "perl") == 0)
12625 n = perl_enabled(FALSE);
12626#endif
12627#ifdef FEAT_GUI
12628 else if (STRICMP(name, "gui_running") == 0)
12629 n = (gui.in_use || gui.starting);
12630# ifdef FEAT_GUI_W32
12631 else if (STRICMP(name, "gui_win32s") == 0)
12632 n = gui_is_win32s();
12633# endif
12634# ifdef FEAT_BROWSE
12635 else if (STRICMP(name, "browse") == 0)
12636 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
12637# endif
12638#endif
12639#ifdef FEAT_SYN_HL
12640 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020012641 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012642#endif
12643#if defined(WIN3264)
12644 else if (STRICMP(name, "win95") == 0)
12645 n = mch_windows95();
12646#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012647#ifdef FEAT_NETBEANS_INTG
12648 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020012649 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000012650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012651 }
12652
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012653 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654}
12655
12656/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000012657 * "has_key()" function
12658 */
12659 static void
12660f_has_key(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012661 typval_T *argvars;
12662 typval_T *rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012663{
Bram Moolenaare9a41262005-01-15 22:18:47 +000012664 if (argvars[0].v_type != VAR_DICT)
12665 {
12666 EMSG(_(e_dictreq));
12667 return;
12668 }
12669 if (argvars[0].vval.v_dict == NULL)
12670 return;
12671
12672 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012673 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012674}
12675
12676/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012677 * "haslocaldir()" function
12678 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012679 static void
12680f_haslocaldir(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012681 typval_T *argvars UNUSED;
Bram Moolenaard267b9c2007-04-26 15:06:45 +000012682 typval_T *rettv;
12683{
12684 rettv->vval.v_number = (curwin->w_localdir != NULL);
12685}
12686
12687/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012688 * "hasmapto()" function
12689 */
12690 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012691f_hasmapto(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012692 typval_T *argvars;
12693 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012694{
12695 char_u *name;
12696 char_u *mode;
12697 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000012698 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012699
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012700 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012701 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012702 mode = (char_u *)"nvo";
12703 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000012704 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012705 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000012706 if (argvars[2].v_type != VAR_UNKNOWN)
12707 abbr = get_tv_number(&argvars[2]);
12708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012709
Bram Moolenaar2c932302006-03-18 21:42:09 +000012710 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012713 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012714}
12715
12716/*
12717 * "histadd()" function
12718 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012719 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012720f_histadd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012721 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012722 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012723{
12724#ifdef FEAT_CMDHIST
12725 int histype;
12726 char_u *str;
12727 char_u buf[NUMBUFLEN];
12728#endif
12729
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012730 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012731 if (check_restricted() || check_secure())
12732 return;
12733#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012734 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12735 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 if (histype >= 0)
12737 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012738 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012739 if (*str != NUL)
12740 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000012741 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012742 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012743 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012744 return;
12745 }
12746 }
12747#endif
12748}
12749
12750/*
12751 * "histdel()" function
12752 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012753 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012754f_histdel(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012755 typval_T *argvars UNUSED;
12756 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012757{
12758#ifdef FEAT_CMDHIST
12759 int n;
12760 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012761 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012763 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12764 if (str == NULL)
12765 n = 0;
12766 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012768 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012769 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012770 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012771 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012772 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 else
12774 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012775 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012776 get_tv_string_buf(&argvars[1], buf));
12777 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012778#endif
12779}
12780
12781/*
12782 * "histget()" function
12783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012785f_histget(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012786 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788{
12789#ifdef FEAT_CMDHIST
12790 int type;
12791 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012792 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012794 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
12795 if (str == NULL)
12796 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012797 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012798 {
12799 type = get_histtype(str);
12800 if (argvars[1].v_type == VAR_UNKNOWN)
12801 idx = get_history_idx(type);
12802 else
12803 idx = (int)get_tv_number_chk(&argvars[1], NULL);
12804 /* -1 on type error */
12805 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
12806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012808 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012809#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012810 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811}
12812
12813/*
12814 * "histnr()" function
12815 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817f_histnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012818 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012819 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012820{
12821 int i;
12822
12823#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012824 char_u *history = get_tv_string_chk(&argvars[0]);
12825
12826 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012827 if (i >= HIST_CMD && i < HIST_COUNT)
12828 i = get_history_idx(i);
12829 else
12830#endif
12831 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012832 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012833}
12834
12835/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012836 * "highlightID(name)" function
12837 */
12838 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012839f_hlID(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012840 typval_T *argvars;
12841 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012842{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012843 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012844}
12845
12846/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012847 * "highlight_exists()" function
12848 */
12849 static void
12850f_hlexists(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012851 typval_T *argvars;
12852 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012853{
12854 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
12855}
12856
12857/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012858 * "hostname()" function
12859 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012861f_hostname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012862 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012863 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012864{
12865 char_u hostname[256];
12866
12867 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012868 rettv->v_type = VAR_STRING;
12869 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012870}
12871
12872/*
12873 * iconv() function
12874 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012876f_iconv(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000012877 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000012878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879{
12880#ifdef FEAT_MBYTE
12881 char_u buf1[NUMBUFLEN];
12882 char_u buf2[NUMBUFLEN];
12883 char_u *from, *to, *str;
12884 vimconv_T vimconv;
12885#endif
12886
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012887 rettv->v_type = VAR_STRING;
12888 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889
12890#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012891 str = get_tv_string(&argvars[0]);
12892 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
12893 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894 vimconv.vc_type = CONV_NONE;
12895 convert_setup(&vimconv, from, to);
12896
12897 /* If the encodings are equal, no conversion needed. */
12898 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012899 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902
12903 convert_setup(&vimconv, NULL, NULL);
12904 vim_free(from);
12905 vim_free(to);
12906#endif
12907}
12908
12909/*
12910 * "indent()" function
12911 */
12912 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012913f_indent(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012914 typval_T *argvars;
12915 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916{
12917 linenr_T lnum;
12918
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012919 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012921 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012923 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012924}
12925
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012926/*
12927 * "index()" function
12928 */
12929 static void
12930f_index(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000012931 typval_T *argvars;
12932 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012933{
Bram Moolenaar33570922005-01-25 22:26:29 +000012934 list_T *l;
12935 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012936 long idx = 0;
12937 int ic = FALSE;
12938
12939 rettv->vval.v_number = -1;
12940 if (argvars[0].v_type != VAR_LIST)
12941 {
12942 EMSG(_(e_listreq));
12943 return;
12944 }
12945 l = argvars[0].vval.v_list;
12946 if (l != NULL)
12947 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000012948 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012949 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012950 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012951 int error = FALSE;
12952
Bram Moolenaar758711c2005-02-02 23:11:38 +000012953 /* Start at specified item. Use the cached index that list_find()
12954 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012955 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000012956 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012957 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012958 ic = get_tv_number_chk(&argvars[3], &error);
12959 if (error)
12960 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012961 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012962
Bram Moolenaar758711c2005-02-02 23:11:38 +000012963 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010012964 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000012965 {
12966 rettv->vval.v_number = idx;
12967 break;
12968 }
12969 }
12970}
12971
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972static int inputsecret_flag = 0;
12973
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012974static void get_user_input __ARGS((typval_T *argvars, typval_T *rettv, int inputdialog));
12975
Bram Moolenaar071d4272004-06-13 20:20:40 +000012976/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012977 * This function is used by f_input() and f_inputdialog() functions. The third
12978 * argument to f_input() specifies the type of completion to use at the
12979 * prompt. The third argument to f_inputdialog() specifies the value to return
12980 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 */
12982 static void
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012983get_user_input(argvars, rettv, inputdialog)
Bram Moolenaar33570922005-01-25 22:26:29 +000012984 typval_T *argvars;
12985 typval_T *rettv;
Bram Moolenaarecbaf552006-07-13 06:31:00 +000012986 int inputdialog;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012987{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012988 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012989 char_u *p = NULL;
12990 int c;
12991 char_u buf[NUMBUFLEN];
12992 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012993 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012994 int xp_type = EXPAND_NOTHING;
12995 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012997 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000012998 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012999
13000#ifdef NO_CONSOLE_INPUT
13001 /* While starting up, there is no place to enter text. */
13002 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004#endif
13005
13006 cmd_silent = FALSE; /* Want to see the prompt. */
13007 if (prompt != NULL)
13008 {
13009 /* Only the part of the message after the last NL is considered as
13010 * prompt for the command line */
13011 p = vim_strrchr(prompt, '\n');
13012 if (p == NULL)
13013 p = prompt;
13014 else
13015 {
13016 ++p;
13017 c = *p;
13018 *p = NUL;
13019 msg_start();
13020 msg_clr_eos();
13021 msg_puts_attr(prompt, echo_attr);
13022 msg_didout = FALSE;
13023 msg_starthere();
13024 *p = c;
13025 }
13026 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013028 if (argvars[1].v_type != VAR_UNKNOWN)
13029 {
13030 defstr = get_tv_string_buf_chk(&argvars[1], buf);
13031 if (defstr != NULL)
13032 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013033
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013034 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000013035 {
13036 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000013037 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000013038 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013039
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013040 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000013041 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013042
Bram Moolenaar4463f292005-09-25 22:20:24 +000013043 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
13044 if (xp_name == NULL)
13045 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013046
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013047 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013048
Bram Moolenaar4463f292005-09-25 22:20:24 +000013049 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
13050 &xp_arg) == FAIL)
13051 return;
13052 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013053 }
13054
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013055 if (defstr != NULL)
13056 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013057 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
13058 xp_type, xp_arg);
Bram Moolenaar04b27512012-08-08 14:33:21 +020013059 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020013060 && argvars[1].v_type != VAR_UNKNOWN
13061 && argvars[2].v_type != VAR_UNKNOWN)
13062 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
13063 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000013064
13065 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013066
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013067 /* since the user typed this, no need to wait for return */
13068 need_wait_return = FALSE;
13069 msg_didout = FALSE;
13070 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013071 cmd_silent = cmd_silent_save;
13072}
13073
13074/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013075 * "input()" function
13076 * Also handles inputsecret() when inputsecret is set.
13077 */
13078 static void
13079f_input(argvars, rettv)
13080 typval_T *argvars;
13081 typval_T *rettv;
13082{
13083 get_user_input(argvars, rettv, FALSE);
13084}
13085
13086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013087 * "inputdialog()" function
13088 */
13089 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013090f_inputdialog(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013091 typval_T *argvars;
13092 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013093{
13094#if defined(FEAT_GUI_TEXTDIALOG)
13095 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
13096 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
13097 {
13098 char_u *message;
13099 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013100 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000013101
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013102 message = get_tv_string_chk(&argvars[0]);
13103 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000013104 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000013105 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106 else
13107 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013108 if (message != NULL && defstr != NULL
13109 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010013110 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013111 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013112 else
13113 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013114 if (message != NULL && defstr != NULL
13115 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013116 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013117 rettv->vval.v_string = vim_strsave(
13118 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013120 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013122 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013123 }
13124 else
13125#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000013126 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013127}
13128
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013129/*
13130 * "inputlist()" function
13131 */
13132 static void
13133f_inputlist(argvars, rettv)
13134 typval_T *argvars;
13135 typval_T *rettv;
13136{
13137 listitem_T *li;
13138 int selected;
13139 int mouse_used;
13140
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013141#ifdef NO_CONSOLE_INPUT
13142 /* While starting up, there is no place to enter text. */
13143 if (no_console_input())
13144 return;
13145#endif
13146 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
13147 {
13148 EMSG2(_(e_listarg), "inputlist()");
13149 return;
13150 }
13151
13152 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000013153 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000013154 lines_left = Rows; /* avoid more prompt */
13155 msg_scroll = TRUE;
13156 msg_clr_eos();
13157
13158 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
13159 {
13160 msg_puts(get_tv_string(&li->li_tv));
13161 msg_putchar('\n');
13162 }
13163
13164 /* Ask for choice. */
13165 selected = prompt_for_number(&mouse_used);
13166 if (mouse_used)
13167 selected -= lines_left;
13168
13169 rettv->vval.v_number = selected;
13170}
13171
13172
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
13174
13175/*
13176 * "inputrestore()" function
13177 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013179f_inputrestore(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013180 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013181 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013182{
13183 if (ga_userinput.ga_len > 0)
13184 {
13185 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
13187 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013188 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189 }
13190 else if (p_verbose > 1)
13191 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000013192 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013193 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013194 }
13195}
13196
13197/*
13198 * "inputsave()" function
13199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013200 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013201f_inputsave(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013202 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013203 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013205 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206 if (ga_grow(&ga_userinput, 1) == OK)
13207 {
13208 save_typeahead((tasave_T *)(ga_userinput.ga_data)
13209 + ga_userinput.ga_len);
13210 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013211 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212 }
13213 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013214 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013215}
13216
13217/*
13218 * "inputsecret()" function
13219 */
13220 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013221f_inputsecret(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013222 typval_T *argvars;
13223 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224{
13225 ++cmdline_star;
13226 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013227 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228 --cmdline_star;
13229 --inputsecret_flag;
13230}
13231
13232/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013233 * "insert()" function
13234 */
13235 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013236f_insert(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013237 typval_T *argvars;
13238 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013239{
13240 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013241 listitem_T *item;
13242 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013243 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013244
13245 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013246 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013247 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020013248 && !tv_check_lock(l->lv_lock, (char_u *)_("insert() argument")))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013249 {
13250 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013251 before = get_tv_number_chk(&argvars[2], &error);
13252 if (error)
13253 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013254
Bram Moolenaar758711c2005-02-02 23:11:38 +000013255 if (before == l->lv_len)
13256 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013257 else
13258 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013259 item = list_find(l, before);
13260 if (item == NULL)
13261 {
13262 EMSGN(_(e_listidx), before);
13263 l = NULL;
13264 }
13265 }
13266 if (l != NULL)
13267 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013268 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000013269 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013270 }
13271 }
13272}
13273
13274/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010013275 * "invert(expr)" function
13276 */
13277 static void
13278f_invert(argvars, rettv)
13279 typval_T *argvars;
13280 typval_T *rettv;
13281{
13282 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
13283}
13284
13285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286 * "isdirectory()" function
13287 */
13288 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013289f_isdirectory(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013290 typval_T *argvars;
13291 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013293 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013294}
13295
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013296/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013297 * "islocked()" function
13298 */
13299 static void
13300f_islocked(argvars, rettv)
13301 typval_T *argvars;
13302 typval_T *rettv;
13303{
13304 lval_T lv;
13305 char_u *end;
13306 dictitem_T *di;
13307
13308 rettv->vval.v_number = -1;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000013309 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE, FALSE,
13310 FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013311 if (end != NULL && lv.ll_name != NULL)
13312 {
13313 if (*end != NUL)
13314 EMSG(_(e_trailing));
13315 else
13316 {
13317 if (lv.ll_tv == NULL)
13318 {
13319 if (check_changedtick(lv.ll_name))
13320 rettv->vval.v_number = 1; /* always locked */
13321 else
13322 {
13323 di = find_var(lv.ll_name, NULL);
13324 if (di != NULL)
13325 {
13326 /* Consider a variable locked when:
13327 * 1. the variable itself is locked
13328 * 2. the value of the variable is locked.
13329 * 3. the List or Dict value is locked.
13330 */
13331 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
13332 || tv_islocked(&di->di_tv));
13333 }
13334 }
13335 }
13336 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000013337 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013338 else if (lv.ll_newkey != NULL)
13339 EMSG2(_(e_dictkey), lv.ll_newkey);
13340 else if (lv.ll_list != NULL)
13341 /* List item. */
13342 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
13343 else
13344 /* Dictionary item. */
13345 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
13346 }
13347 }
13348
13349 clear_lval(&lv);
13350}
13351
Bram Moolenaar33570922005-01-25 22:26:29 +000013352static void dict_list __ARGS((typval_T *argvars, typval_T *rettv, int what));
Bram Moolenaar8c711452005-01-14 21:53:12 +000013353
13354/*
13355 * Turn a dict into a list:
13356 * "what" == 0: list of keys
13357 * "what" == 1: list of values
13358 * "what" == 2: list of items
13359 */
13360 static void
13361dict_list(argvars, rettv, what)
Bram Moolenaar33570922005-01-25 22:26:29 +000013362 typval_T *argvars;
13363 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013364 int what;
13365{
Bram Moolenaar33570922005-01-25 22:26:29 +000013366 list_T *l2;
13367 dictitem_T *di;
13368 hashitem_T *hi;
13369 listitem_T *li;
13370 listitem_T *li2;
13371 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013372 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013373
Bram Moolenaar8c711452005-01-14 21:53:12 +000013374 if (argvars[0].v_type != VAR_DICT)
13375 {
13376 EMSG(_(e_dictreq));
13377 return;
13378 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013379 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013380 return;
13381
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013382 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013383 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013384
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013385 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000013386 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013387 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013388 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000013389 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013390 --todo;
13391 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013392
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013393 li = listitem_alloc();
13394 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000013395 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013396 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000013397
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013398 if (what == 0)
13399 {
13400 /* keys() */
13401 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013402 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013403 li->li_tv.vval.v_string = vim_strsave(di->di_key);
13404 }
13405 else if (what == 1)
13406 {
13407 /* values() */
13408 copy_tv(&di->di_tv, &li->li_tv);
13409 }
13410 else
13411 {
13412 /* items() */
13413 l2 = list_alloc();
13414 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013415 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013416 li->li_tv.vval.v_list = l2;
13417 if (l2 == NULL)
13418 break;
13419 ++l2->lv_refcount;
13420
13421 li2 = listitem_alloc();
13422 if (li2 == NULL)
13423 break;
13424 list_append(l2, li2);
13425 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000013426 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013427 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
13428
13429 li2 = listitem_alloc();
13430 if (li2 == NULL)
13431 break;
13432 list_append(l2, li2);
13433 copy_tv(&di->di_tv, &li2->li_tv);
13434 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000013435 }
13436 }
13437}
13438
13439/*
13440 * "items(dict)" function
13441 */
13442 static void
13443f_items(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013444 typval_T *argvars;
13445 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013446{
13447 dict_list(argvars, rettv, 2);
13448}
13449
Bram Moolenaar071d4272004-06-13 20:20:40 +000013450/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013451 * "join()" function
13452 */
13453 static void
13454f_join(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013455 typval_T *argvars;
13456 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013457{
13458 garray_T ga;
13459 char_u *sep;
13460
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013461 if (argvars[0].v_type != VAR_LIST)
13462 {
13463 EMSG(_(e_listreq));
13464 return;
13465 }
13466 if (argvars[0].vval.v_list == NULL)
13467 return;
13468 if (argvars[1].v_type == VAR_UNKNOWN)
13469 sep = (char_u *)" ";
13470 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013471 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013472
13473 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013474
13475 if (sep != NULL)
13476 {
13477 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000013478 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013479 ga_append(&ga, NUL);
13480 rettv->vval.v_string = (char_u *)ga.ga_data;
13481 }
13482 else
13483 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013484}
13485
13486/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000013487 * "keys()" function
13488 */
13489 static void
13490f_keys(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013491 typval_T *argvars;
13492 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000013493{
13494 dict_list(argvars, rettv, 0);
13495}
13496
13497/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 * "last_buffer_nr()" function.
13499 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013500 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013501f_last_buffer_nr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013502 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013503 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504{
13505 int n = 0;
13506 buf_T *buf;
13507
13508 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
13509 if (n < buf->b_fnum)
13510 n = buf->b_fnum;
13511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013512 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013513}
13514
13515/*
13516 * "len()" function
13517 */
13518 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519f_len(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013520 typval_T *argvars;
13521 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013522{
13523 switch (argvars[0].v_type)
13524 {
13525 case VAR_STRING:
13526 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013527 rettv->vval.v_number = (varnumber_T)STRLEN(
13528 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013529 break;
13530 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013531 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013532 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013533 case VAR_DICT:
13534 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
13535 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013536 default:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000013537 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013538 break;
13539 }
13540}
13541
Bram Moolenaar33570922005-01-25 22:26:29 +000013542static void libcall_common __ARGS((typval_T *argvars, typval_T *rettv, int type));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013543
13544 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013545libcall_common(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013546 typval_T *argvars;
13547 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013548 int type;
13549{
13550#ifdef FEAT_LIBCALL
13551 char_u *string_in;
13552 char_u **string_result;
13553 int nr_result;
13554#endif
13555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013556 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000013557 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013558 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013559
13560 if (check_restricted() || check_secure())
13561 return;
13562
13563#ifdef FEAT_LIBCALL
13564 /* The first two args must be strings, otherwise its meaningless */
13565 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
13566 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000013567 string_in = NULL;
13568 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013569 string_in = argvars[2].vval.v_string;
13570 if (type == VAR_NUMBER)
13571 string_result = NULL;
13572 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013573 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013574 if (mch_libcall(argvars[0].vval.v_string,
13575 argvars[1].vval.v_string,
13576 string_in,
13577 argvars[2].vval.v_number,
13578 string_result,
13579 &nr_result) == OK
13580 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013581 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013582 }
13583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584}
13585
13586/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013587 * "libcall()" function
13588 */
13589 static void
13590f_libcall(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013591 typval_T *argvars;
13592 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013593{
13594 libcall_common(argvars, rettv, VAR_STRING);
13595}
13596
13597/*
13598 * "libcallnr()" function
13599 */
13600 static void
13601f_libcallnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013602 typval_T *argvars;
13603 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013604{
13605 libcall_common(argvars, rettv, VAR_NUMBER);
13606}
13607
13608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013609 * "line(string)" function
13610 */
13611 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013612f_line(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013613 typval_T *argvars;
13614 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013615{
13616 linenr_T lnum = 0;
13617 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013618 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013619
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013620 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013621 if (fp != NULL)
13622 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013623 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013624}
13625
13626/*
13627 * "line2byte(lnum)" function
13628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013629 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013630f_line2byte(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013631 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013632 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633{
13634#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013635 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013636#else
13637 linenr_T lnum;
13638
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013639 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013640 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013641 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013642 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013643 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
13644 if (rettv->vval.v_number >= 0)
13645 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646#endif
13647}
13648
13649/*
13650 * "lispindent(lnum)" function
13651 */
13652 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013653f_lispindent(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010013654 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013655 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013656{
13657#ifdef FEAT_LISP
13658 pos_T pos;
13659 linenr_T lnum;
13660
13661 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013662 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013663 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
13664 {
13665 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013666 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667 curwin->w_cursor = pos;
13668 }
13669 else
13670#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013671 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672}
13673
13674/*
13675 * "localtime()" function
13676 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013678f_localtime(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000013679 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000013680 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013681{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013682 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683}
13684
Bram Moolenaar33570922005-01-25 22:26:29 +000013685static void get_maparg __ARGS((typval_T *argvars, typval_T *rettv, int exact));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686
13687 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013688get_maparg(argvars, rettv, exact)
Bram Moolenaar33570922005-01-25 22:26:29 +000013689 typval_T *argvars;
13690 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013691 int exact;
13692{
13693 char_u *keys;
13694 char_u *which;
13695 char_u buf[NUMBUFLEN];
13696 char_u *keys_buf = NULL;
13697 char_u *rhs;
13698 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000013699 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010013700 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020013701 mapblock_T *mp;
13702 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013703
13704 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013705 rettv->v_type = VAR_STRING;
13706 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013708 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013709 if (*keys == NUL)
13710 return;
13711
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013712 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000013713 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013714 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013715 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020013716 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000013717 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013718 if (argvars[3].v_type != VAR_UNKNOWN)
13719 get_dict = get_tv_number(&argvars[3]);
13720 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000013721 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013722 else
13723 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013724 if (which == NULL)
13725 return;
13726
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727 mode = get_map_mode(&which, 0);
13728
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000013729 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013730 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013731 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013732
13733 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013734 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020013735 /* Return a string. */
13736 if (rhs != NULL)
13737 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013738
Bram Moolenaarbd743252010-10-20 21:23:33 +020013739 }
13740 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
13741 {
13742 /* Return a dictionary. */
13743 char_u *lhs = str2special_save(mp->m_keys, TRUE);
13744 char_u *mapmode = map_mode_to_chars(mp->m_mode);
13745 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013746
Bram Moolenaarbd743252010-10-20 21:23:33 +020013747 dict_add_nr_str(dict, "lhs", 0L, lhs);
13748 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
13749 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
13750 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
13751 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
13752 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
13753 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020013754 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020013755 dict_add_nr_str(dict, "mode", 0L, mapmode);
13756
13757 vim_free(lhs);
13758 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759 }
13760}
13761
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013762#ifdef FEAT_FLOAT
13763/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020013764 * "log()" function
13765 */
13766 static void
13767f_log(argvars, rettv)
13768 typval_T *argvars;
13769 typval_T *rettv;
13770{
13771 float_T f;
13772
13773 rettv->v_type = VAR_FLOAT;
13774 if (get_float_arg(argvars, &f) == OK)
13775 rettv->vval.v_float = log(f);
13776 else
13777 rettv->vval.v_float = 0.0;
13778}
13779
13780/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013781 * "log10()" function
13782 */
13783 static void
13784f_log10(argvars, rettv)
13785 typval_T *argvars;
13786 typval_T *rettv;
13787{
13788 float_T f;
13789
13790 rettv->v_type = VAR_FLOAT;
13791 if (get_float_arg(argvars, &f) == OK)
13792 rettv->vval.v_float = log10(f);
13793 else
13794 rettv->vval.v_float = 0.0;
13795}
13796#endif
13797
Bram Moolenaar1dced572012-04-05 16:54:08 +020013798#ifdef FEAT_LUA
13799/*
13800 * "luaeval()" function
13801 */
13802 static void
13803f_luaeval(argvars, rettv)
13804 typval_T *argvars;
13805 typval_T *rettv;
13806{
13807 char_u *str;
13808 char_u buf[NUMBUFLEN];
13809
13810 str = get_tv_string_buf(&argvars[0], buf);
13811 do_luaeval(str, argvars + 1, rettv);
13812}
13813#endif
13814
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013816 * "map()" function
13817 */
13818 static void
13819f_map(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013820 typval_T *argvars;
13821 typval_T *rettv;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013822{
13823 filter_map(argvars, rettv, TRUE);
13824}
13825
13826/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013827 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828 */
13829 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013830f_maparg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013831 typval_T *argvars;
13832 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013833{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013834 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835}
13836
13837/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000013838 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 */
13840 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000013841f_mapcheck(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000013842 typval_T *argvars;
13843 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013844{
Bram Moolenaar0d660222005-01-07 21:51:51 +000013845 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013846}
13847
Bram Moolenaar33570922005-01-25 22:26:29 +000013848static void find_some_match __ARGS((typval_T *argvars, typval_T *rettv, int start));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013849
13850 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013851find_some_match(argvars, rettv, type)
Bram Moolenaar33570922005-01-25 22:26:29 +000013852 typval_T *argvars;
13853 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854 int type;
13855{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013856 char_u *str = NULL;
13857 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013858 char_u *pat;
13859 regmatch_T regmatch;
13860 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013861 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 char_u *save_cpo;
13863 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013864 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013865 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013866 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000013867 list_T *l = NULL;
13868 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013869 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013870 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871
13872 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
13873 save_cpo = p_cpo;
13874 p_cpo = (char_u *)"";
13875
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013876 rettv->vval.v_number = -1;
13877 if (type == 3)
13878 {
13879 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013880 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013881 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013882 }
13883 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013884 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013885 rettv->v_type = VAR_STRING;
13886 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013889 if (argvars[0].v_type == VAR_LIST)
13890 {
13891 if ((l = argvars[0].vval.v_list) == NULL)
13892 goto theend;
13893 li = l->lv_first;
13894 }
13895 else
13896 expr = str = get_tv_string(&argvars[0]);
13897
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013898 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
13899 if (pat == NULL)
13900 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013901
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013902 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013904 int error = FALSE;
13905
13906 start = get_tv_number_chk(&argvars[2], &error);
13907 if (error)
13908 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013909 if (l != NULL)
13910 {
13911 li = list_find(l, start);
13912 if (li == NULL)
13913 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000013914 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013915 }
13916 else
13917 {
13918 if (start < 0)
13919 start = 0;
13920 if (start > (long)STRLEN(str))
13921 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013922 /* When "count" argument is there ignore matches before "start",
13923 * otherwise skip part of the string. Differs when pattern is "^"
13924 * or "\<". */
13925 if (argvars[3].v_type != VAR_UNKNOWN)
13926 startcol = start;
13927 else
13928 str += start;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013929 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013930
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013931 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013932 nth = get_tv_number_chk(&argvars[3], &error);
13933 if (error)
13934 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935 }
13936
13937 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
13938 if (regmatch.regprog != NULL)
13939 {
13940 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013941
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000013942 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013943 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013944 if (l != NULL)
13945 {
13946 if (li == NULL)
13947 {
13948 match = FALSE;
13949 break;
13950 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013951 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013952 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000013953 if (str == NULL)
13954 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013955 }
13956
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000013957 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013958
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013959 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013960 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013961 if (l == NULL && !match)
13962 break;
13963
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013964 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013965 if (l != NULL)
13966 {
13967 li = li->li_next;
13968 ++idx;
13969 }
13970 else
13971 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013972#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000013973 startcol = (colnr_T)(regmatch.startp[0]
13974 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013975#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020013976 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013977#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013978 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000013979 }
13980
13981 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013983 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000013984 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000013985 int i;
13986
13987 /* return list with matched string and submatches */
13988 for (i = 0; i < NSUBEXP; ++i)
13989 {
13990 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000013991 {
13992 if (list_append_string(rettv->vval.v_list,
13993 (char_u *)"", 0) == FAIL)
13994 break;
13995 }
13996 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000013997 regmatch.startp[i],
13998 (int)(regmatch.endp[i] - regmatch.startp[i]))
13999 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014000 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014001 }
14002 }
14003 else if (type == 2)
14004 {
14005 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014006 if (l != NULL)
14007 copy_tv(&li->li_tv, rettv);
14008 else
14009 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000014010 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014011 }
14012 else if (l != NULL)
14013 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014014 else
14015 {
14016 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014017 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 (varnumber_T)(regmatch.startp[0] - str);
14019 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014020 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014022 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014023 }
14024 }
Bram Moolenaar473de612013-06-08 18:19:48 +020014025 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026 }
14027
14028theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014029 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030 p_cpo = save_cpo;
14031}
14032
14033/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014034 * "match()" function
14035 */
14036 static void
14037f_match(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014038 typval_T *argvars;
14039 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014040{
14041 find_some_match(argvars, rettv, 1);
14042}
14043
14044/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014045 * "matchadd()" function
14046 */
14047 static void
14048f_matchadd(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014049 typval_T *argvars UNUSED;
14050 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014051{
14052#ifdef FEAT_SEARCH_EXTRA
14053 char_u buf[NUMBUFLEN];
14054 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
14055 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
14056 int prio = 10; /* default priority */
14057 int id = -1;
14058 int error = FALSE;
14059
14060 rettv->vval.v_number = -1;
14061
14062 if (grp == NULL || pat == NULL)
14063 return;
14064 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014065 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014066 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000014067 if (argvars[3].v_type != VAR_UNKNOWN)
14068 id = get_tv_number_chk(&argvars[3], &error);
14069 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014070 if (error == TRUE)
14071 return;
14072 if (id >= 1 && id <= 3)
14073 {
14074 EMSGN("E798: ID is reserved for \":match\": %ld", id);
14075 return;
14076 }
14077
14078 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id);
14079#endif
14080}
14081
14082/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014083 * "matcharg()" function
14084 */
14085 static void
14086f_matcharg(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014087 typval_T *argvars UNUSED;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014088 typval_T *rettv;
14089{
14090 if (rettv_list_alloc(rettv) == OK)
14091 {
14092#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014093 int id = get_tv_number(&argvars[0]);
14094 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014095
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014096 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014097 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014098 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
14099 {
14100 list_append_string(rettv->vval.v_list,
14101 syn_id2name(m->hlg_id), -1);
14102 list_append_string(rettv->vval.v_list, m->pattern, -1);
14103 }
14104 else
14105 {
14106 list_append_string(rettv->vval.v_list, NUL, -1);
14107 list_append_string(rettv->vval.v_list, NUL, -1);
14108 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014109 }
14110#endif
14111 }
14112}
14113
14114/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014115 * "matchdelete()" function
14116 */
14117 static void
14118f_matchdelete(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014119 typval_T *argvars UNUSED;
14120 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000014121{
14122#ifdef FEAT_SEARCH_EXTRA
14123 rettv->vval.v_number = match_delete(curwin,
14124 (int)get_tv_number(&argvars[0]), TRUE);
14125#endif
14126}
14127
14128/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014129 * "matchend()" function
14130 */
14131 static void
14132f_matchend(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014133 typval_T *argvars;
14134 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014135{
14136 find_some_match(argvars, rettv, 0);
14137}
14138
14139/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014140 * "matchlist()" function
14141 */
14142 static void
14143f_matchlist(argvars, rettv)
14144 typval_T *argvars;
14145 typval_T *rettv;
14146{
14147 find_some_match(argvars, rettv, 3);
14148}
14149
14150/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014151 * "matchstr()" function
14152 */
14153 static void
14154f_matchstr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014155 typval_T *argvars;
14156 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014157{
14158 find_some_match(argvars, rettv, 2);
14159}
14160
Bram Moolenaar33570922005-01-25 22:26:29 +000014161static void max_min __ARGS((typval_T *argvars, typval_T *rettv, int domax));
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014162
14163 static void
14164max_min(argvars, rettv, domax)
Bram Moolenaar33570922005-01-25 22:26:29 +000014165 typval_T *argvars;
14166 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014167 int domax;
14168{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014169 long n = 0;
14170 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014171 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014172
14173 if (argvars[0].v_type == VAR_LIST)
14174 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014175 list_T *l;
14176 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014177
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014178 l = argvars[0].vval.v_list;
14179 if (l != NULL)
14180 {
14181 li = l->lv_first;
14182 if (li != NULL)
14183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014184 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000014185 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014186 {
14187 li = li->li_next;
14188 if (li == NULL)
14189 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014190 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014191 if (domax ? i > n : i < n)
14192 n = i;
14193 }
14194 }
14195 }
14196 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000014197 else if (argvars[0].v_type == VAR_DICT)
14198 {
Bram Moolenaar33570922005-01-25 22:26:29 +000014199 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014200 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000014201 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014202 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014203
14204 d = argvars[0].vval.v_dict;
14205 if (d != NULL)
14206 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014207 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014208 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014209 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014210 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000014211 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014212 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014213 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014214 if (first)
14215 {
14216 n = i;
14217 first = FALSE;
14218 }
14219 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014220 n = i;
14221 }
14222 }
14223 }
14224 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014225 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000014226 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014227 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014228}
14229
14230/*
14231 * "max()" function
14232 */
14233 static void
14234f_max(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014235 typval_T *argvars;
14236 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014237{
14238 max_min(argvars, rettv, TRUE);
14239}
14240
14241/*
14242 * "min()" function
14243 */
14244 static void
14245f_min(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014246 typval_T *argvars;
14247 typval_T *rettv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000014248{
14249 max_min(argvars, rettv, FALSE);
14250}
14251
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014252static int mkdir_recurse __ARGS((char_u *dir, int prot));
14253
14254/*
14255 * Create the directory in which "dir" is located, and higher levels when
14256 * needed.
14257 */
14258 static int
14259mkdir_recurse(dir, prot)
14260 char_u *dir;
14261 int prot;
14262{
14263 char_u *p;
14264 char_u *updir;
14265 int r = FAIL;
14266
14267 /* Get end of directory name in "dir".
14268 * We're done when it's "/" or "c:/". */
14269 p = gettail_sep(dir);
14270 if (p <= get_past_head(dir))
14271 return OK;
14272
14273 /* If the directory exists we're done. Otherwise: create it.*/
14274 updir = vim_strnsave(dir, (int)(p - dir));
14275 if (updir == NULL)
14276 return FAIL;
14277 if (mch_isdir(updir))
14278 r = OK;
14279 else if (mkdir_recurse(updir, prot) == OK)
14280 r = vim_mkdir_emsg(updir, prot);
14281 vim_free(updir);
14282 return r;
14283}
14284
14285#ifdef vim_mkdir
14286/*
14287 * "mkdir()" function
14288 */
14289 static void
14290f_mkdir(argvars, rettv)
14291 typval_T *argvars;
14292 typval_T *rettv;
14293{
14294 char_u *dir;
14295 char_u buf[NUMBUFLEN];
14296 int prot = 0755;
14297
14298 rettv->vval.v_number = FAIL;
14299 if (check_restricted() || check_secure())
14300 return;
14301
14302 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014303 if (*dir == NUL)
14304 rettv->vval.v_number = FAIL;
14305 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014306 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020014307 if (*gettail(dir) == NUL)
14308 /* remove trailing slashes */
14309 *gettail_sep(dir) = NUL;
14310
14311 if (argvars[1].v_type != VAR_UNKNOWN)
14312 {
14313 if (argvars[2].v_type != VAR_UNKNOWN)
14314 prot = get_tv_number_chk(&argvars[2], NULL);
14315 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
14316 mkdir_recurse(dir, prot);
14317 }
14318 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014319 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014320}
14321#endif
14322
Bram Moolenaar0d660222005-01-07 21:51:51 +000014323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014324 * "mode()" function
14325 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014326 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327f_mode(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014328 typval_T *argvars;
14329 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014330{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014331 char_u buf[3];
14332
14333 buf[1] = NUL;
14334 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335
14336#ifdef FEAT_VISUAL
14337 if (VIsual_active)
14338 {
14339 if (VIsual_select)
14340 buf[0] = VIsual_mode + 's' - 'v';
14341 else
14342 buf[0] = VIsual_mode;
14343 }
14344 else
14345#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014346 if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
14347 || State == CONFIRM)
14348 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014350 if (State == ASKMORE)
14351 buf[1] = 'm';
14352 else if (State == CONFIRM)
14353 buf[1] = '?';
14354 }
14355 else if (State == EXTERNCMD)
14356 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000014357 else if (State & INSERT)
14358 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014359#ifdef FEAT_VREPLACE
14360 if (State & VREPLACE_FLAG)
14361 {
14362 buf[0] = 'R';
14363 buf[1] = 'v';
14364 }
14365 else
14366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 if (State & REPLACE_FLAG)
14368 buf[0] = 'R';
14369 else
14370 buf[0] = 'i';
14371 }
14372 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014373 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014374 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014375 if (exmode_active)
14376 buf[1] = 'v';
14377 }
14378 else if (exmode_active)
14379 {
14380 buf[0] = 'c';
14381 buf[1] = 'e';
14382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000014385 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014386 if (finish_op)
14387 buf[1] = 'o';
14388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389
Bram Moolenaar05bb9532008-07-04 09:44:11 +000014390 /* Clear out the minor mode when the argument is not a non-zero number or
14391 * non-empty string. */
14392 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014393 buf[1] = NUL;
14394
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014395 rettv->vval.v_string = vim_strsave(buf);
14396 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397}
14398
Bram Moolenaar429fa852013-04-15 12:27:36 +020014399#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014400/*
14401 * "mzeval()" function
14402 */
14403 static void
14404f_mzeval(argvars, rettv)
14405 typval_T *argvars;
14406 typval_T *rettv;
14407{
14408 char_u *str;
14409 char_u buf[NUMBUFLEN];
14410
14411 str = get_tv_string_buf(&argvars[0], buf);
14412 do_mzeval(str, rettv);
14413}
Bram Moolenaar75676462013-01-30 14:55:42 +010014414
14415 void
14416mzscheme_call_vim(name, args, rettv)
14417 char_u *name;
14418 typval_T *args;
14419 typval_T *rettv;
14420{
14421 typval_T argvars[3];
14422
14423 argvars[0].v_type = VAR_STRING;
14424 argvars[0].vval.v_string = name;
14425 copy_tv(args, &argvars[1]);
14426 argvars[2].v_type = VAR_UNKNOWN;
14427 f_call(argvars, rettv);
14428 clear_tv(&argvars[1]);
14429}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010014430#endif
14431
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014433 * "nextnonblank()" function
14434 */
14435 static void
14436f_nextnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014437 typval_T *argvars;
14438 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014439{
14440 linenr_T lnum;
14441
14442 for (lnum = get_tv_lnum(argvars); ; ++lnum)
14443 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014444 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014445 {
14446 lnum = 0;
14447 break;
14448 }
14449 if (*skipwhite(ml_get(lnum)) != NUL)
14450 break;
14451 }
14452 rettv->vval.v_number = lnum;
14453}
14454
14455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014456 * "nr2char()" function
14457 */
14458 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014459f_nr2char(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014460 typval_T *argvars;
14461 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462{
14463 char_u buf[NUMBUFLEN];
14464
14465#ifdef FEAT_MBYTE
14466 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010014467 {
14468 int utf8 = 0;
14469
14470 if (argvars[1].v_type != VAR_UNKNOWN)
14471 utf8 = get_tv_number_chk(&argvars[1], NULL);
14472 if (utf8)
14473 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14474 else
14475 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
14476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477 else
14478#endif
14479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014480 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014481 buf[1] = NUL;
14482 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014483 rettv->v_type = VAR_STRING;
14484 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014485}
14486
14487/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014488 * "or(expr, expr)" function
14489 */
14490 static void
14491f_or(argvars, rettv)
14492 typval_T *argvars;
14493 typval_T *rettv;
14494{
14495 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
14496 | get_tv_number_chk(&argvars[1], NULL);
14497}
14498
14499/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014500 * "pathshorten()" function
14501 */
14502 static void
14503f_pathshorten(argvars, rettv)
14504 typval_T *argvars;
14505 typval_T *rettv;
14506{
14507 char_u *p;
14508
14509 rettv->v_type = VAR_STRING;
14510 p = get_tv_string_chk(&argvars[0]);
14511 if (p == NULL)
14512 rettv->vval.v_string = NULL;
14513 else
14514 {
14515 p = vim_strsave(p);
14516 rettv->vval.v_string = p;
14517 if (p != NULL)
14518 shorten_dir(p);
14519 }
14520}
14521
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014522#ifdef FEAT_FLOAT
14523/*
14524 * "pow()" function
14525 */
14526 static void
14527f_pow(argvars, rettv)
14528 typval_T *argvars;
14529 typval_T *rettv;
14530{
14531 float_T fx, fy;
14532
14533 rettv->v_type = VAR_FLOAT;
14534 if (get_float_arg(argvars, &fx) == OK
14535 && get_float_arg(&argvars[1], &fy) == OK)
14536 rettv->vval.v_float = pow(fx, fy);
14537 else
14538 rettv->vval.v_float = 0.0;
14539}
14540#endif
14541
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014542/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014543 * "prevnonblank()" function
14544 */
14545 static void
14546f_prevnonblank(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014547 typval_T *argvars;
14548 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000014549{
14550 linenr_T lnum;
14551
14552 lnum = get_tv_lnum(argvars);
14553 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
14554 lnum = 0;
14555 else
14556 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
14557 --lnum;
14558 rettv->vval.v_number = lnum;
14559}
14560
Bram Moolenaara6c840d2005-08-22 22:59:46 +000014561#ifdef HAVE_STDARG_H
14562/* This dummy va_list is here because:
14563 * - passing a NULL pointer doesn't work when va_list isn't a pointer
14564 * - locally in the function results in a "used before set" warning
14565 * - using va_start() to initialize it gives "function with fixed args" error */
14566static va_list ap;
14567#endif
14568
Bram Moolenaar8c711452005-01-14 21:53:12 +000014569/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014570 * "printf()" function
14571 */
14572 static void
14573f_printf(argvars, rettv)
14574 typval_T *argvars;
14575 typval_T *rettv;
14576{
14577 rettv->v_type = VAR_STRING;
14578 rettv->vval.v_string = NULL;
Bram Moolenaard52d9742005-08-21 22:20:28 +000014579#ifdef HAVE_STDARG_H /* only very old compilers can't do this */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014580 {
14581 char_u buf[NUMBUFLEN];
14582 int len;
14583 char_u *s;
14584 int saved_did_emsg = did_emsg;
14585 char *fmt;
14586
14587 /* Get the required length, allocate the buffer and do it for real. */
14588 did_emsg = FALSE;
14589 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014590 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014591 if (!did_emsg)
14592 {
14593 s = alloc(len + 1);
14594 if (s != NULL)
14595 {
14596 rettv->vval.v_string = s;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000014597 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000014598 }
14599 }
14600 did_emsg |= saved_did_emsg;
14601 }
14602#endif
14603}
14604
14605/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014606 * "pumvisible()" function
14607 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014608 static void
14609f_pumvisible(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000014610 typval_T *argvars UNUSED;
14611 typval_T *rettv UNUSED;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014612{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014613#ifdef FEAT_INS_EXPAND
14614 if (pum_visible())
14615 rettv->vval.v_number = 1;
14616#endif
14617}
14618
Bram Moolenaardb913952012-06-29 12:54:53 +020014619#ifdef FEAT_PYTHON3
14620/*
14621 * "py3eval()" function
14622 */
14623 static void
14624f_py3eval(argvars, rettv)
14625 typval_T *argvars;
14626 typval_T *rettv;
14627{
14628 char_u *str;
14629 char_u buf[NUMBUFLEN];
14630
14631 str = get_tv_string_buf(&argvars[0], buf);
14632 do_py3eval(str, rettv);
14633}
14634#endif
14635
14636#ifdef FEAT_PYTHON
14637/*
14638 * "pyeval()" function
14639 */
14640 static void
14641f_pyeval(argvars, rettv)
14642 typval_T *argvars;
14643 typval_T *rettv;
14644{
14645 char_u *str;
14646 char_u buf[NUMBUFLEN];
14647
14648 str = get_tv_string_buf(&argvars[0], buf);
14649 do_pyeval(str, rettv);
14650}
14651#endif
14652
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000014653/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014654 * "range()" function
14655 */
14656 static void
14657f_range(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000014658 typval_T *argvars;
14659 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014660{
14661 long start;
14662 long end;
14663 long stride = 1;
14664 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014665 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014667 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014668 if (argvars[1].v_type == VAR_UNKNOWN)
14669 {
14670 end = start - 1;
14671 start = 0;
14672 }
14673 else
14674 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014675 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014676 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014677 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014678 }
14679
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014680 if (error)
14681 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000014682 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014683 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000014684 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000014685 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000014686 else
14687 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014688 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014689 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014690 if (list_append_number(rettv->vval.v_list,
14691 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014692 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014693 }
14694}
14695
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014696/*
14697 * "readfile()" function
14698 */
14699 static void
14700f_readfile(argvars, rettv)
14701 typval_T *argvars;
14702 typval_T *rettv;
14703{
14704 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014705 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014706 char_u *fname;
14707 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014708 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
14709 int io_size = sizeof(buf);
14710 int readlen; /* size of last fread() */
14711 char_u *prev = NULL; /* previously read bytes, if any */
14712 long prevlen = 0; /* length of data in prev */
14713 long prevsize = 0; /* size of prev buffer */
14714 long maxline = MAXLNUM;
14715 long cnt = 0;
14716 char_u *p; /* position in buf */
14717 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014718
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014719 if (argvars[1].v_type != VAR_UNKNOWN)
14720 {
14721 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
14722 binary = TRUE;
14723 if (argvars[2].v_type != VAR_UNKNOWN)
14724 maxline = get_tv_number(&argvars[2]);
14725 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014726
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014727 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014728 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014729
14730 /* Always open the file in binary mode, library functions have a mind of
14731 * their own about CR-LF conversion. */
14732 fname = get_tv_string(&argvars[0]);
14733 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
14734 {
14735 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
14736 return;
14737 }
14738
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014739 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014740 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014741 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014742
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014743 /* This for loop processes what was read, but is also entered at end
14744 * of file so that either:
14745 * - an incomplete line gets written
14746 * - a "binary" file gets an empty line at the end if it ends in a
14747 * newline. */
14748 for (p = buf, start = buf;
14749 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
14750 ++p)
14751 {
14752 if (*p == '\n' || readlen <= 0)
14753 {
14754 listitem_T *li;
14755 char_u *s = NULL;
14756 long_u len = p - start;
14757
14758 /* Finished a line. Remove CRs before NL. */
14759 if (readlen > 0 && !binary)
14760 {
14761 while (len > 0 && start[len - 1] == '\r')
14762 --len;
14763 /* removal may cross back to the "prev" string */
14764 if (len == 0)
14765 while (prevlen > 0 && prev[prevlen - 1] == '\r')
14766 --prevlen;
14767 }
14768 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014769 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014770 else
14771 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014772 /* Change "prev" buffer to be the right size. This way
14773 * the bytes are only copied once, and very long lines are
14774 * allocated only once. */
14775 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014776 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014777 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014778 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014779 prev = NULL; /* the list will own the string */
14780 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014781 }
14782 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014783 if (s == NULL)
14784 {
14785 do_outofmem_msg((long_u) prevlen + len + 1);
14786 failed = TRUE;
14787 break;
14788 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014789
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014790 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014791 {
14792 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014793 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014794 break;
14795 }
14796 li->li_tv.v_type = VAR_STRING;
14797 li->li_tv.v_lock = 0;
14798 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014799 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014800
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014801 start = p + 1; /* step over newline */
14802 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014803 break;
14804 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014805 else if (*p == NUL)
14806 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020014807#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014808 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
14809 * when finding the BF and check the previous two bytes. */
14810 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020014811 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014812 /* Find the two bytes before the 0xbf. If p is at buf, or buf
14813 * + 1, these may be in the "prev" string. */
14814 char_u back1 = p >= buf + 1 ? p[-1]
14815 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
14816 char_u back2 = p >= buf + 2 ? p[-2]
14817 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
14818 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014819
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014820 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014821 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014822 char_u *dest = p - 2;
14823
14824 /* Usually a BOM is at the beginning of a file, and so at
14825 * the beginning of a line; then we can just step over it.
14826 */
14827 if (start == dest)
14828 start = p + 1;
14829 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020014830 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014831 /* have to shuffle buf to close gap */
14832 int adjust_prevlen = 0;
14833
14834 if (dest < buf)
14835 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014836 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014837 dest = buf;
14838 }
14839 if (readlen > p - buf + 1)
14840 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
14841 readlen -= 3 - adjust_prevlen;
14842 prevlen -= adjust_prevlen;
14843 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020014844 }
14845 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014846 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014847#endif
14848 } /* for */
14849
14850 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
14851 break;
14852 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014853 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014854 /* There's part of a line in buf, store it in "prev". */
14855 if (p - start + prevlen >= prevsize)
14856 {
14857 /* need bigger "prev" buffer */
14858 char_u *newprev;
14859
14860 /* A common use case is ordinary text files and "prev" gets a
14861 * fragment of a line, so the first allocation is made
14862 * small, to avoid repeatedly 'allocing' large and
14863 * 'reallocing' small. */
14864 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014865 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014866 else
14867 {
14868 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014869 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014870 prevsize = grow50pc > growmin ? grow50pc : growmin;
14871 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020014872 newprev = prev == NULL ? alloc(prevsize)
14873 : vim_realloc(prev, prevsize);
14874 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014875 {
14876 do_outofmem_msg((long_u)prevsize);
14877 failed = TRUE;
14878 break;
14879 }
14880 prev = newprev;
14881 }
14882 /* Add the line part to end of "prev". */
14883 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010014884 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014885 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014886 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014887
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014888 /*
14889 * For a negative line count use only the lines at the end of the file,
14890 * free the rest.
14891 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014892 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014893 while (cnt > -maxline)
14894 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014895 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000014896 --cnt;
14897 }
14898
Bram Moolenaara489e1d2012-02-05 00:39:18 +010014899 if (failed)
14900 {
14901 list_free(rettv->vval.v_list, TRUE);
14902 /* readfile doc says an empty list is returned on error */
14903 rettv->vval.v_list = list_alloc();
14904 }
14905
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000014906 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000014907 fclose(fd);
14908}
14909
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014910#if defined(FEAT_RELTIME)
14911static int list2proftime __ARGS((typval_T *arg, proftime_T *tm));
14912
14913/*
14914 * Convert a List to proftime_T.
14915 * Return FAIL when there is something wrong.
14916 */
14917 static int
14918list2proftime(arg, tm)
14919 typval_T *arg;
14920 proftime_T *tm;
14921{
14922 long n1, n2;
14923 int error = FALSE;
14924
14925 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
14926 || arg->vval.v_list->lv_len != 2)
14927 return FAIL;
14928 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
14929 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
14930# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014931 tm->HighPart = n1;
14932 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014933# else
14934 tm->tv_sec = n1;
14935 tm->tv_usec = n2;
14936# endif
14937 return error ? FAIL : OK;
14938}
14939#endif /* FEAT_RELTIME */
14940
14941/*
14942 * "reltime()" function
14943 */
14944 static void
14945f_reltime(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014946 typval_T *argvars UNUSED;
14947 typval_T *rettv UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014948{
14949#ifdef FEAT_RELTIME
14950 proftime_T res;
14951 proftime_T start;
14952
14953 if (argvars[0].v_type == VAR_UNKNOWN)
14954 {
14955 /* No arguments: get current time. */
14956 profile_start(&res);
14957 }
14958 else if (argvars[1].v_type == VAR_UNKNOWN)
14959 {
14960 if (list2proftime(&argvars[0], &res) == FAIL)
14961 return;
14962 profile_end(&res);
14963 }
14964 else
14965 {
14966 /* Two arguments: compute the difference. */
14967 if (list2proftime(&argvars[0], &start) == FAIL
14968 || list2proftime(&argvars[1], &res) == FAIL)
14969 return;
14970 profile_sub(&res, &start);
14971 }
14972
14973 if (rettv_list_alloc(rettv) == OK)
14974 {
14975 long n1, n2;
14976
14977# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000014978 n1 = res.HighPart;
14979 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014980# else
14981 n1 = res.tv_sec;
14982 n2 = res.tv_usec;
14983# endif
14984 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
14985 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
14986 }
14987#endif
14988}
14989
14990/*
14991 * "reltimestr()" function
14992 */
14993 static void
14994f_reltimestr(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010014995 typval_T *argvars UNUSED;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000014996 typval_T *rettv;
14997{
14998#ifdef FEAT_RELTIME
14999 proftime_T tm;
15000#endif
15001
15002 rettv->v_type = VAR_STRING;
15003 rettv->vval.v_string = NULL;
15004#ifdef FEAT_RELTIME
15005 if (list2proftime(&argvars[0], &tm) == OK)
15006 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
15007#endif
15008}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015009
Bram Moolenaar0d660222005-01-07 21:51:51 +000015010#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
15011static void make_connection __ARGS((void));
15012static int check_connection __ARGS((void));
15013
15014 static void
15015make_connection()
15016{
15017 if (X_DISPLAY == NULL
15018# ifdef FEAT_GUI
15019 && !gui.in_use
15020# endif
15021 )
15022 {
15023 x_force_connect = TRUE;
15024 setup_term_clip();
15025 x_force_connect = FALSE;
15026 }
15027}
15028
15029 static int
15030check_connection()
15031{
15032 make_connection();
15033 if (X_DISPLAY == NULL)
15034 {
15035 EMSG(_("E240: No connection to Vim server"));
15036 return FAIL;
15037 }
15038 return OK;
15039}
15040#endif
15041
15042#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015043static void remote_common __ARGS((typval_T *argvars, typval_T *rettv, int expr));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015044
15045 static void
15046remote_common(argvars, rettv, expr)
Bram Moolenaar33570922005-01-25 22:26:29 +000015047 typval_T *argvars;
15048 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015049 int expr;
15050{
15051 char_u *server_name;
15052 char_u *keys;
15053 char_u *r = NULL;
15054 char_u buf[NUMBUFLEN];
15055# ifdef WIN32
15056 HWND w;
15057# else
15058 Window w;
15059# endif
15060
15061 if (check_restricted() || check_secure())
15062 return;
15063
15064# ifdef FEAT_X11
15065 if (check_connection() == FAIL)
15066 return;
15067# endif
15068
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015069 server_name = get_tv_string_chk(&argvars[0]);
15070 if (server_name == NULL)
15071 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015072 keys = get_tv_string_buf(&argvars[1], buf);
15073# ifdef WIN32
15074 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
15075# else
15076 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
15077 < 0)
15078# endif
15079 {
15080 if (r != NULL)
15081 EMSG(r); /* sending worked but evaluation failed */
15082 else
15083 EMSG2(_("E241: Unable to send to %s"), server_name);
15084 return;
15085 }
15086
15087 rettv->vval.v_string = r;
15088
15089 if (argvars[2].v_type != VAR_UNKNOWN)
15090 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015091 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000015092 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015093 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015094
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015095 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000015096 v.di_tv.v_type = VAR_STRING;
15097 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015098 idvar = get_tv_string_chk(&argvars[2]);
15099 if (idvar != NULL)
15100 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015101 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015102 }
15103}
15104#endif
15105
15106/*
15107 * "remote_expr()" function
15108 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015109 static void
15110f_remote_expr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015111 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015112 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015113{
15114 rettv->v_type = VAR_STRING;
15115 rettv->vval.v_string = NULL;
15116#ifdef FEAT_CLIENTSERVER
15117 remote_common(argvars, rettv, TRUE);
15118#endif
15119}
15120
15121/*
15122 * "remote_foreground()" function
15123 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015124 static void
15125f_remote_foreground(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015126 typval_T *argvars UNUSED;
15127 typval_T *rettv UNUSED;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015128{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015129#ifdef FEAT_CLIENTSERVER
15130# ifdef WIN32
15131 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015132 {
15133 char_u *server_name = get_tv_string_chk(&argvars[0]);
15134
15135 if (server_name != NULL)
15136 serverForeground(server_name);
15137 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015138# else
15139 /* Send a foreground() expression to the server. */
15140 argvars[1].v_type = VAR_STRING;
15141 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
15142 argvars[2].v_type = VAR_UNKNOWN;
15143 remote_common(argvars, rettv, TRUE);
15144 vim_free(argvars[1].vval.v_string);
15145# endif
15146#endif
15147}
15148
Bram Moolenaar0d660222005-01-07 21:51:51 +000015149 static void
15150f_remote_peek(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015151 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015152 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015153{
15154#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000015155 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015156 char_u *s = NULL;
15157# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015158 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015159# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015160 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015161
15162 if (check_restricted() || check_secure())
15163 {
15164 rettv->vval.v_number = -1;
15165 return;
15166 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015167 serverid = get_tv_string_chk(&argvars[0]);
15168 if (serverid == NULL)
15169 {
15170 rettv->vval.v_number = -1;
15171 return; /* type error; errmsg already given */
15172 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000015173# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015174 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015175 if (n == 0)
15176 rettv->vval.v_number = -1;
15177 else
15178 {
15179 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
15180 rettv->vval.v_number = (s != NULL);
15181 }
15182# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000015183 if (check_connection() == FAIL)
15184 return;
15185
15186 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015187 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015188# endif
15189
15190 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
15191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015192 char_u *retvar;
15193
Bram Moolenaar33570922005-01-25 22:26:29 +000015194 v.di_tv.v_type = VAR_STRING;
15195 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015196 retvar = get_tv_string_chk(&argvars[1]);
15197 if (retvar != NULL)
15198 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000015199 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015200 }
15201#else
15202 rettv->vval.v_number = -1;
15203#endif
15204}
15205
Bram Moolenaar0d660222005-01-07 21:51:51 +000015206 static void
15207f_remote_read(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015208 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015209 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015210{
15211 char_u *r = NULL;
15212
15213#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015214 char_u *serverid = get_tv_string_chk(&argvars[0]);
15215
15216 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000015217 {
15218# ifdef WIN32
15219 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015220 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015221
Bram Moolenaareb3593b2006-04-22 22:33:57 +000015222 sscanf(serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000015223 if (n != 0)
15224 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
15225 if (r == NULL)
15226# else
15227 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015228 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015229# endif
15230 EMSG(_("E277: Unable to read a server reply"));
15231 }
15232#endif
15233 rettv->v_type = VAR_STRING;
15234 rettv->vval.v_string = r;
15235}
15236
15237/*
15238 * "remote_send()" function
15239 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015240 static void
15241f_remote_send(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000015242 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000015243 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015244{
15245 rettv->v_type = VAR_STRING;
15246 rettv->vval.v_string = NULL;
15247#ifdef FEAT_CLIENTSERVER
15248 remote_common(argvars, rettv, FALSE);
15249#endif
15250}
15251
15252/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015253 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015254 */
15255 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015256f_remove(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015257 typval_T *argvars;
15258 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015259{
Bram Moolenaar33570922005-01-25 22:26:29 +000015260 list_T *l;
15261 listitem_T *item, *item2;
15262 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015263 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015264 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000015265 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000015266 dict_T *d;
15267 dictitem_T *di;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015268 char *arg_errmsg = N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015269
Bram Moolenaar8c711452005-01-14 21:53:12 +000015270 if (argvars[0].v_type == VAR_DICT)
15271 {
15272 if (argvars[2].v_type != VAR_UNKNOWN)
15273 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015274 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015275 && !tv_check_lock(d->dv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar8c711452005-01-14 21:53:12 +000015276 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015277 key = get_tv_string_chk(&argvars[1]);
15278 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015280 di = dict_find(d, key, -1);
15281 if (di == NULL)
15282 EMSG2(_(e_dictkey), key);
15283 else
15284 {
15285 *rettv = di->di_tv;
15286 init_tv(&di->di_tv);
15287 dictitem_remove(d, di);
15288 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015289 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000015290 }
15291 }
15292 else if (argvars[0].v_type != VAR_LIST)
15293 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015294 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015295 && !tv_check_lock(l->lv_lock, (char_u *)_(arg_errmsg)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015296 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015297 int error = FALSE;
15298
15299 idx = get_tv_number_chk(&argvars[1], &error);
15300 if (error)
15301 ; /* type error: do nothing, errmsg already given */
15302 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015303 EMSGN(_(e_listidx), idx);
15304 else
15305 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015306 if (argvars[2].v_type == VAR_UNKNOWN)
15307 {
15308 /* Remove one item, return its value. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015309 list_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015310 *rettv = item->li_tv;
15311 vim_free(item);
15312 }
15313 else
15314 {
15315 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015316 end = get_tv_number_chk(&argvars[2], &error);
15317 if (error)
15318 ; /* type error: do nothing */
15319 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015320 EMSGN(_(e_listidx), end);
15321 else
15322 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015323 int cnt = 0;
15324
15325 for (li = item; li != NULL; li = li->li_next)
15326 {
15327 ++cnt;
15328 if (li == item2)
15329 break;
15330 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015331 if (li == NULL) /* didn't find "item2" after "item" */
15332 EMSG(_(e_invrange));
15333 else
15334 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000015335 list_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015336 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015337 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015338 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015339 l->lv_first = item;
15340 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015341 item->li_prev = NULL;
15342 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015343 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015344 }
15345 }
15346 }
15347 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015348 }
15349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350}
15351
15352/*
15353 * "rename({from}, {to})" function
15354 */
15355 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015356f_rename(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015357 typval_T *argvars;
15358 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015359{
15360 char_u buf[NUMBUFLEN];
15361
15362 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015364 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015365 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
15366 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000015367}
15368
15369/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015370 * "repeat()" function
15371 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015372 static void
15373f_repeat(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015374 typval_T *argvars;
15375 typval_T *rettv;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015376{
15377 char_u *p;
15378 int n;
15379 int slen;
15380 int len;
15381 char_u *r;
15382 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015383
15384 n = get_tv_number(&argvars[1]);
15385 if (argvars[0].v_type == VAR_LIST)
15386 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015387 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015388 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015389 if (list_extend(rettv->vval.v_list,
15390 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015391 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000015392 }
15393 else
15394 {
15395 p = get_tv_string(&argvars[0]);
15396 rettv->v_type = VAR_STRING;
15397 rettv->vval.v_string = NULL;
15398
15399 slen = (int)STRLEN(p);
15400 len = slen * n;
15401 if (len <= 0)
15402 return;
15403
15404 r = alloc(len + 1);
15405 if (r != NULL)
15406 {
15407 for (i = 0; i < n; i++)
15408 mch_memmove(r + i * slen, p, (size_t)slen);
15409 r[len] = NUL;
15410 }
15411
15412 rettv->vval.v_string = r;
15413 }
15414}
15415
15416/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015417 * "resolve()" function
15418 */
15419 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420f_resolve(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015421 typval_T *argvars;
15422 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015423{
15424 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020015425#ifdef HAVE_READLINK
15426 char_u *buf = NULL;
15427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015429 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015430#ifdef FEAT_SHORTCUT
15431 {
15432 char_u *v = NULL;
15433
15434 v = mch_resolve_shortcut(p);
15435 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015436 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015437 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015438 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 }
15440#else
15441# ifdef HAVE_READLINK
15442 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 char_u *cpy;
15444 int len;
15445 char_u *remain = NULL;
15446 char_u *q;
15447 int is_relative_to_current = FALSE;
15448 int has_trailing_pathsep = FALSE;
15449 int limit = 100;
15450
15451 p = vim_strsave(p);
15452
15453 if (p[0] == '.' && (vim_ispathsep(p[1])
15454 || (p[1] == '.' && (vim_ispathsep(p[2])))))
15455 is_relative_to_current = TRUE;
15456
15457 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015458 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015459 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020015461 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
15462 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463
15464 q = getnextcomp(p);
15465 if (*q != NUL)
15466 {
15467 /* Separate the first path component in "p", and keep the
15468 * remainder (beginning with the path separator). */
15469 remain = vim_strsave(q - 1);
15470 q[-1] = NUL;
15471 }
15472
Bram Moolenaard9462e32011-04-11 21:35:11 +020015473 buf = alloc(MAXPATHL + 1);
15474 if (buf == NULL)
15475 goto fail;
15476
Bram Moolenaar071d4272004-06-13 20:20:40 +000015477 for (;;)
15478 {
15479 for (;;)
15480 {
15481 len = readlink((char *)p, (char *)buf, MAXPATHL);
15482 if (len <= 0)
15483 break;
15484 buf[len] = NUL;
15485
15486 if (limit-- == 0)
15487 {
15488 vim_free(p);
15489 vim_free(remain);
15490 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015491 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492 goto fail;
15493 }
15494
15495 /* Ensure that the result will have a trailing path separator
15496 * if the argument has one. */
15497 if (remain == NULL && has_trailing_pathsep)
15498 add_pathsep(buf);
15499
15500 /* Separate the first path component in the link value and
15501 * concatenate the remainders. */
15502 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
15503 if (*q != NUL)
15504 {
15505 if (remain == NULL)
15506 remain = vim_strsave(q - 1);
15507 else
15508 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000015509 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015510 if (cpy != NULL)
15511 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512 vim_free(remain);
15513 remain = cpy;
15514 }
15515 }
15516 q[-1] = NUL;
15517 }
15518
15519 q = gettail(p);
15520 if (q > p && *q == NUL)
15521 {
15522 /* Ignore trailing path separator. */
15523 q[-1] = NUL;
15524 q = gettail(p);
15525 }
15526 if (q > p && !mch_isFullName(buf))
15527 {
15528 /* symlink is relative to directory of argument */
15529 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
15530 if (cpy != NULL)
15531 {
15532 STRCPY(cpy, p);
15533 STRCPY(gettail(cpy), buf);
15534 vim_free(p);
15535 p = cpy;
15536 }
15537 }
15538 else
15539 {
15540 vim_free(p);
15541 p = vim_strsave(buf);
15542 }
15543 }
15544
15545 if (remain == NULL)
15546 break;
15547
15548 /* Append the first path component of "remain" to "p". */
15549 q = getnextcomp(remain + 1);
15550 len = q - remain - (*q != NUL);
15551 cpy = vim_strnsave(p, STRLEN(p) + len);
15552 if (cpy != NULL)
15553 {
15554 STRNCAT(cpy, remain, len);
15555 vim_free(p);
15556 p = cpy;
15557 }
15558 /* Shorten "remain". */
15559 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015560 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561 else
15562 {
15563 vim_free(remain);
15564 remain = NULL;
15565 }
15566 }
15567
15568 /* If the result is a relative path name, make it explicitly relative to
15569 * the current directory if and only if the argument had this form. */
15570 if (!vim_ispathsep(*p))
15571 {
15572 if (is_relative_to_current
15573 && *p != NUL
15574 && !(p[0] == '.'
15575 && (p[1] == NUL
15576 || vim_ispathsep(p[1])
15577 || (p[1] == '.'
15578 && (p[2] == NUL
15579 || vim_ispathsep(p[2]))))))
15580 {
15581 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000015582 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583 if (cpy != NULL)
15584 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 vim_free(p);
15586 p = cpy;
15587 }
15588 }
15589 else if (!is_relative_to_current)
15590 {
15591 /* Strip leading "./". */
15592 q = p;
15593 while (q[0] == '.' && vim_ispathsep(q[1]))
15594 q += 2;
15595 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015596 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597 }
15598 }
15599
15600 /* Ensure that the result will have no trailing path separator
15601 * if the argument had none. But keep "/" or "//". */
15602 if (!has_trailing_pathsep)
15603 {
15604 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000015605 if (after_pathsep(p, q))
15606 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 }
15608
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015609 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610 }
15611# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015612 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015613# endif
15614#endif
15615
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015616 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015617
15618#ifdef HAVE_READLINK
15619fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020015620 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015621#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015622 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015623}
15624
15625/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015626 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 */
15628 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000015629f_reverse(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000015630 typval_T *argvars;
15631 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632{
Bram Moolenaar33570922005-01-25 22:26:29 +000015633 list_T *l;
15634 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635
Bram Moolenaar0d660222005-01-07 21:51:51 +000015636 if (argvars[0].v_type != VAR_LIST)
15637 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000015638 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar32f649e2011-04-11 13:46:13 +020015639 && !tv_check_lock(l->lv_lock, (char_u *)_("reverse() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000015640 {
15641 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015642 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015643 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015644 while (li != NULL)
15645 {
15646 ni = li->li_prev;
15647 list_append(l, li);
15648 li = ni;
15649 }
15650 rettv->vval.v_list = l;
15651 rettv->v_type = VAR_LIST;
15652 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000015653 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655}
15656
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015657#define SP_NOMOVE 0x01 /* don't move cursor */
15658#define SP_REPEAT 0x02 /* repeat to find outer pair */
15659#define SP_RETCOUNT 0x04 /* return matchcount */
15660#define SP_SETPCMARK 0x08 /* set previous context mark */
15661#define SP_START 0x10 /* accept match at start position */
15662#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
15663#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015664
Bram Moolenaar33570922005-01-25 22:26:29 +000015665static int get_search_arg __ARGS((typval_T *varp, int *flagsp));
Bram Moolenaar0d660222005-01-07 21:51:51 +000015666
15667/*
15668 * Get flags for a search function.
15669 * Possibly sets "p_ws".
15670 * Returns BACKWARD, FORWARD or zero (for an error).
15671 */
15672 static int
15673get_search_arg(varp, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015674 typval_T *varp;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015675 int *flagsp;
15676{
15677 int dir = FORWARD;
15678 char_u *flags;
15679 char_u nbuf[NUMBUFLEN];
15680 int mask;
15681
15682 if (varp->v_type != VAR_UNKNOWN)
15683 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015684 flags = get_tv_string_buf_chk(varp, nbuf);
15685 if (flags == NULL)
15686 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000015687 while (*flags != NUL)
15688 {
15689 switch (*flags)
15690 {
15691 case 'b': dir = BACKWARD; break;
15692 case 'w': p_ws = TRUE; break;
15693 case 'W': p_ws = FALSE; break;
15694 default: mask = 0;
15695 if (flagsp != NULL)
15696 switch (*flags)
15697 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015698 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015699 case 'e': mask = SP_END; break;
15700 case 'm': mask = SP_RETCOUNT; break;
15701 case 'n': mask = SP_NOMOVE; break;
15702 case 'p': mask = SP_SUBPAT; break;
15703 case 'r': mask = SP_REPEAT; break;
15704 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000015705 }
15706 if (mask == 0)
15707 {
15708 EMSG2(_(e_invarg2), flags);
15709 dir = 0;
15710 }
15711 else
15712 *flagsp |= mask;
15713 }
15714 if (dir == 0)
15715 break;
15716 ++flags;
15717 }
15718 }
15719 return dir;
15720}
15721
Bram Moolenaar071d4272004-06-13 20:20:40 +000015722/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015723 * Shared by search() and searchpos() functions
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015725 static int
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015726search_cmn(argvars, match_pos, flagsp)
Bram Moolenaar33570922005-01-25 22:26:29 +000015727 typval_T *argvars;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015728 pos_T *match_pos;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015729 int *flagsp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015731 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015732 char_u *pat;
15733 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015734 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 int save_p_ws = p_ws;
15736 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015737 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015738 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015739 proftime_T tm;
15740#ifdef FEAT_RELTIME
15741 long time_limit = 0;
15742#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015743 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015744 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015746 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015747 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015748 if (dir == 0)
15749 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015750 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015751 if (flags & SP_START)
15752 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015753 if (flags & SP_END)
15754 options |= SEARCH_END;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015755
Bram Moolenaar76929292008-01-06 19:07:36 +000015756 /* Optional arguments: line number to stop searching and timeout. */
15757 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015758 {
15759 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
15760 if (lnum_stop < 0)
15761 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000015762#ifdef FEAT_RELTIME
15763 if (argvars[3].v_type != VAR_UNKNOWN)
15764 {
15765 time_limit = get_tv_number_chk(&argvars[3], NULL);
15766 if (time_limit < 0)
15767 goto theend;
15768 }
15769#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015770 }
15771
Bram Moolenaar76929292008-01-06 19:07:36 +000015772#ifdef FEAT_RELTIME
15773 /* Set the time limit, if there is one. */
15774 profile_setlimit(time_limit, &tm);
15775#endif
15776
Bram Moolenaar231334e2005-07-25 20:46:57 +000015777 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015778 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000015779 * Check to make sure only those flags are set.
15780 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
15781 * flags cannot be set. Check for that condition also.
15782 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015783 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015784 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015785 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015786 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015787 goto theend;
15788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015789
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015790 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015791 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000015792 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015793 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015794 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000015795 if (flags & SP_SUBPAT)
15796 retval = subpatnum;
15797 else
15798 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000015799 if (flags & SP_SETPCMARK)
15800 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015801 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015802 if (match_pos != NULL)
15803 {
15804 /* Store the match cursor position */
15805 match_pos->lnum = pos.lnum;
15806 match_pos->col = pos.col + 1;
15807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015808 /* "/$" will put the cursor after the end of the line, may need to
15809 * correct that here */
15810 check_cursor();
15811 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015812
15813 /* If 'n' flag is used: restore cursor position. */
15814 if (flags & SP_NOMOVE)
15815 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000015816 else
15817 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000015818theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000015819 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015820
15821 return retval;
15822}
15823
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015824#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015825
15826/*
15827 * round() is not in C90, use ceil() or floor() instead.
15828 */
15829 float_T
15830vim_round(f)
15831 float_T f;
15832{
15833 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
15834}
15835
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015836/*
15837 * "round({float})" function
15838 */
15839 static void
15840f_round(argvars, rettv)
15841 typval_T *argvars;
15842 typval_T *rettv;
15843{
15844 float_T f;
15845
15846 rettv->v_type = VAR_FLOAT;
15847 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020015848 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015849 else
15850 rettv->vval.v_float = 0.0;
15851}
15852#endif
15853
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015854/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020015855 * "screenattr()" function
15856 */
15857 static void
15858f_screenattr(argvars, rettv)
15859 typval_T *argvars UNUSED;
15860 typval_T *rettv;
15861{
15862 int row;
15863 int col;
15864 int c;
15865
15866 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15867 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15868 if (row < 0 || row >= screen_Rows
15869 || col < 0 || col >= screen_Columns)
15870 c = -1;
15871 else
15872 c = ScreenAttrs[LineOffset[row] + col];
15873 rettv->vval.v_number = c;
15874}
15875
15876/*
15877 * "screenchar()" function
15878 */
15879 static void
15880f_screenchar(argvars, rettv)
15881 typval_T *argvars UNUSED;
15882 typval_T *rettv;
15883{
15884 int row;
15885 int col;
15886 int off;
15887 int c;
15888
15889 row = get_tv_number_chk(&argvars[0], NULL) - 1;
15890 col = get_tv_number_chk(&argvars[1], NULL) - 1;
15891 if (row < 0 || row >= screen_Rows
15892 || col < 0 || col >= screen_Columns)
15893 c = -1;
15894 else
15895 {
15896 off = LineOffset[row] + col;
15897#ifdef FEAT_MBYTE
15898 if (enc_utf8 && ScreenLinesUC[off] != 0)
15899 c = ScreenLinesUC[off];
15900 else
15901#endif
15902 c = ScreenLines[off];
15903 }
15904 rettv->vval.v_number = c;
15905}
15906
15907/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010015908 * "screencol()" function
15909 *
15910 * First column is 1 to be consistent with virtcol().
15911 */
15912 static void
15913f_screencol(argvars, rettv)
15914 typval_T *argvars UNUSED;
15915 typval_T *rettv;
15916{
15917 rettv->vval.v_number = screen_screencol() + 1;
15918}
15919
15920/*
15921 * "screenrow()" function
15922 */
15923 static void
15924f_screenrow(argvars, rettv)
15925 typval_T *argvars UNUSED;
15926 typval_T *rettv;
15927{
15928 rettv->vval.v_number = screen_screenrow() + 1;
15929}
15930
15931/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015932 * "search()" function
15933 */
15934 static void
15935f_search(argvars, rettv)
15936 typval_T *argvars;
15937 typval_T *rettv;
15938{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000015939 int flags = 0;
15940
15941 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015942}
15943
Bram Moolenaar071d4272004-06-13 20:20:40 +000015944/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015945 * "searchdecl()" function
15946 */
15947 static void
15948f_searchdecl(argvars, rettv)
15949 typval_T *argvars;
15950 typval_T *rettv;
15951{
15952 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015953 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015954 int error = FALSE;
15955 char_u *name;
15956
15957 rettv->vval.v_number = 1; /* default: FAIL */
15958
15959 name = get_tv_string_chk(&argvars[0]);
15960 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000015961 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015962 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000015963 if (!error && argvars[2].v_type != VAR_UNKNOWN)
15964 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
15965 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015966 if (!error && name != NULL)
15967 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000015968 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000015969}
15970
15971/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015972 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000015973 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015974 static int
15975searchpair_cmn(argvars, match_pos)
Bram Moolenaar33570922005-01-25 22:26:29 +000015976 typval_T *argvars;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015977 pos_T *match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015978{
15979 char_u *spat, *mpat, *epat;
15980 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015981 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015982 int dir;
15983 int flags = 0;
15984 char_u nbuf1[NUMBUFLEN];
15985 char_u nbuf2[NUMBUFLEN];
15986 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015987 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015988 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000015989 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015990
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015992 spat = get_tv_string_chk(&argvars[0]);
15993 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
15994 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
15995 if (spat == NULL || mpat == NULL || epat == NULL)
15996 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015997
Bram Moolenaar071d4272004-06-13 20:20:40 +000015998 /* Handle the optional fourth argument: flags */
15999 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000016000 if (dir == 0)
16001 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016002
16003 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000016004 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
16005 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016006 if ((flags & (SP_END | SP_SUBPAT)) != 0
16007 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000016008 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016009 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000016010 goto theend;
16011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016012
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016013 /* Using 'r' implies 'W', otherwise it doesn't work. */
16014 if (flags & SP_REPEAT)
16015 p_ws = FALSE;
16016
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016017 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016018 if (argvars[3].v_type == VAR_UNKNOWN
16019 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016020 skip = (char_u *)"";
16021 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016022 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016023 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016024 if (argvars[5].v_type != VAR_UNKNOWN)
16025 {
16026 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
16027 if (lnum_stop < 0)
16028 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000016029#ifdef FEAT_RELTIME
16030 if (argvars[6].v_type != VAR_UNKNOWN)
16031 {
16032 time_limit = get_tv_number_chk(&argvars[6], NULL);
16033 if (time_limit < 0)
16034 goto theend;
16035 }
16036#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016037 }
16038 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016039 if (skip == NULL)
16040 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016041
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016042 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000016043 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016044
16045theend:
16046 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016047
16048 return retval;
16049}
16050
16051/*
16052 * "searchpair()" function
16053 */
16054 static void
16055f_searchpair(argvars, rettv)
16056 typval_T *argvars;
16057 typval_T *rettv;
16058{
16059 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
16060}
16061
16062/*
16063 * "searchpairpos()" function
16064 */
16065 static void
16066f_searchpairpos(argvars, rettv)
16067 typval_T *argvars;
16068 typval_T *rettv;
16069{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016070 pos_T match_pos;
16071 int lnum = 0;
16072 int col = 0;
16073
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016074 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016075 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016076
16077 if (searchpair_cmn(argvars, &match_pos) > 0)
16078 {
16079 lnum = match_pos.lnum;
16080 col = match_pos.col;
16081 }
16082
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016083 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16084 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016085}
16086
16087/*
16088 * Search for a start/middle/end thing.
16089 * Used by searchpair(), see its documentation for the details.
16090 * Returns 0 or -1 for no match,
16091 */
16092 long
Bram Moolenaar76929292008-01-06 19:07:36 +000016093do_searchpair(spat, mpat, epat, dir, skip, flags, match_pos,
16094 lnum_stop, time_limit)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016095 char_u *spat; /* start pattern */
16096 char_u *mpat; /* middle pattern */
16097 char_u *epat; /* end pattern */
16098 int dir; /* BACKWARD or FORWARD */
16099 char_u *skip; /* skip expression */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000016100 int flags; /* SP_SETPCMARK and other SP_ values */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016101 pos_T *match_pos;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016102 linenr_T lnum_stop; /* stop at this line if not zero */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016103 long time_limit UNUSED; /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016104{
16105 char_u *save_cpo;
16106 char_u *pat, *pat2 = NULL, *pat3 = NULL;
16107 long retval = 0;
16108 pos_T pos;
16109 pos_T firstpos;
16110 pos_T foundpos;
16111 pos_T save_cursor;
16112 pos_T save_pos;
16113 int n;
16114 int r;
16115 int nest = 1;
16116 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016117 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000016118 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016119
16120 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
16121 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016122 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016123
Bram Moolenaar76929292008-01-06 19:07:36 +000016124#ifdef FEAT_RELTIME
16125 /* Set the time limit, if there is one. */
16126 profile_setlimit(time_limit, &tm);
16127#endif
16128
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016129 /* Make two search patterns: start/end (pat2, for in nested pairs) and
16130 * start/middle/end (pat3, for the top pair). */
16131 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
16132 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
16133 if (pat2 == NULL || pat3 == NULL)
16134 goto theend;
16135 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
16136 if (*mpat == NUL)
16137 STRCPY(pat3, pat2);
16138 else
16139 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
16140 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016141 if (flags & SP_START)
16142 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016143
Bram Moolenaar071d4272004-06-13 20:20:40 +000016144 save_cursor = curwin->w_cursor;
16145 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000016146 clearpos(&firstpos);
16147 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016148 pat = pat3;
16149 for (;;)
16150 {
16151 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000016152 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016153 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
16154 /* didn't find it or found the first match again: FAIL */
16155 break;
16156
16157 if (firstpos.lnum == 0)
16158 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000016159 if (equalpos(pos, foundpos))
16160 {
16161 /* Found the same position again. Can happen with a pattern that
16162 * has "\zs" at the end and searching backwards. Advance one
16163 * character and try again. */
16164 if (dir == BACKWARD)
16165 decl(&pos);
16166 else
16167 incl(&pos);
16168 }
16169 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016170
Bram Moolenaar92de73d2008-01-22 10:59:38 +000016171 /* clear the start flag to avoid getting stuck here */
16172 options &= ~SEARCH_START;
16173
Bram Moolenaar071d4272004-06-13 20:20:40 +000016174 /* If the skip pattern matches, ignore this match. */
16175 if (*skip != NUL)
16176 {
16177 save_pos = curwin->w_cursor;
16178 curwin->w_cursor = pos;
16179 r = eval_to_bool(skip, &err, NULL, FALSE);
16180 curwin->w_cursor = save_pos;
16181 if (err)
16182 {
16183 /* Evaluating {skip} caused an error, break here. */
16184 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016185 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016186 break;
16187 }
16188 if (r)
16189 continue;
16190 }
16191
16192 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
16193 {
16194 /* Found end when searching backwards or start when searching
16195 * forward: nested pair. */
16196 ++nest;
16197 pat = pat2; /* nested, don't search for middle */
16198 }
16199 else
16200 {
16201 /* Found end when searching forward or start when searching
16202 * backward: end of (nested) pair; or found middle in outer pair. */
16203 if (--nest == 1)
16204 pat = pat3; /* outer level, search for middle */
16205 }
16206
16207 if (nest == 0)
16208 {
16209 /* Found the match: return matchcount or line number. */
16210 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016211 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016212 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016213 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000016214 if (flags & SP_SETPCMARK)
16215 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000016216 curwin->w_cursor = pos;
16217 if (!(flags & SP_REPEAT))
16218 break;
16219 nest = 1; /* search for next unmatched */
16220 }
16221 }
16222
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016223 if (match_pos != NULL)
16224 {
16225 /* Store the match cursor position */
16226 match_pos->lnum = curwin->w_cursor.lnum;
16227 match_pos->col = curwin->w_cursor.col + 1;
16228 }
16229
Bram Moolenaar071d4272004-06-13 20:20:40 +000016230 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016231 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016232 curwin->w_cursor = save_cursor;
16233
16234theend:
16235 vim_free(pat2);
16236 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000016237 if (p_cpo == empty_option)
16238 p_cpo = save_cpo;
16239 else
16240 /* Darn, evaluating the {skip} expression changed the value. */
16241 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000016242
16243 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016244}
16245
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016246/*
16247 * "searchpos()" function
16248 */
16249 static void
16250f_searchpos(argvars, rettv)
16251 typval_T *argvars;
16252 typval_T *rettv;
16253{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016254 pos_T match_pos;
16255 int lnum = 0;
16256 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016257 int n;
16258 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016259
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016260 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016261 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016262
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016263 n = search_cmn(argvars, &match_pos, &flags);
16264 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016265 {
16266 lnum = match_pos.lnum;
16267 col = match_pos.col;
16268 }
16269
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016270 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
16271 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000016272 if (flags & SP_SUBPAT)
16273 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016274}
16275
16276
Bram Moolenaar0d660222005-01-07 21:51:51 +000016277 static void
16278f_server2client(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016279 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016280 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016281{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016282#ifdef FEAT_CLIENTSERVER
16283 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016284 char_u *server = get_tv_string_chk(&argvars[0]);
16285 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016286
Bram Moolenaar0d660222005-01-07 21:51:51 +000016287 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016288 if (server == NULL || reply == NULL)
16289 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016290 if (check_restricted() || check_secure())
16291 return;
16292# ifdef FEAT_X11
16293 if (check_connection() == FAIL)
16294 return;
16295# endif
16296
16297 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016298 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000016299 EMSG(_("E258: Unable to send to client"));
16300 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016301 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016302 rettv->vval.v_number = 0;
16303#else
16304 rettv->vval.v_number = -1;
16305#endif
16306}
16307
Bram Moolenaar0d660222005-01-07 21:51:51 +000016308 static void
16309f_serverlist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016310 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000016311 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016312{
16313 char_u *r = NULL;
16314
16315#ifdef FEAT_CLIENTSERVER
16316# ifdef WIN32
16317 r = serverGetVimNames();
16318# else
16319 make_connection();
16320 if (X_DISPLAY != NULL)
16321 r = serverGetVimNames(X_DISPLAY);
16322# endif
16323#endif
16324 rettv->v_type = VAR_STRING;
16325 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016326}
16327
16328/*
16329 * "setbufvar()" function
16330 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016331 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016332f_setbufvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016333 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016334 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016335{
16336 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016338 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016339 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 char_u nbuf[NUMBUFLEN];
16341
16342 if (check_restricted() || check_secure())
16343 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016344 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
16345 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010016346 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016347 varp = &argvars[2];
16348
16349 if (buf != NULL && varname != NULL && varp != NULL)
16350 {
16351 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016353
16354 if (*varname == '&')
16355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016356 long numval;
16357 char_u *strval;
16358 int error = FALSE;
16359
Bram Moolenaar071d4272004-06-13 20:20:40 +000016360 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016361 numval = get_tv_number_chk(varp, &error);
16362 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016363 if (!error && strval != NULL)
16364 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016365 }
16366 else
16367 {
16368 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
16369 if (bufvarname != NULL)
16370 {
16371 STRCPY(bufvarname, "b:");
16372 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016373 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374 vim_free(bufvarname);
16375 }
16376 }
16377
16378 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016379 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381}
16382
16383/*
16384 * "setcmdpos()" function
16385 */
16386 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016387f_setcmdpos(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016388 typval_T *argvars;
16389 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016390{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016391 int pos = (int)get_tv_number(&argvars[0]) - 1;
16392
16393 if (pos >= 0)
16394 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016395}
16396
16397/*
16398 * "setline()" function
16399 */
16400 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016401f_setline(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016402 typval_T *argvars;
16403 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016404{
16405 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000016406 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016407 list_T *l = NULL;
16408 listitem_T *li = NULL;
16409 long added = 0;
16410 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016411
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016412 lnum = get_tv_lnum(&argvars[0]);
16413 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016414 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016415 l = argvars[1].vval.v_list;
16416 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016417 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016418 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016419 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016420
Bram Moolenaar798b30b2009-04-22 10:56:16 +000016421 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016422 for (;;)
16423 {
16424 if (l != NULL)
16425 {
16426 /* list argument, get next string */
16427 if (li == NULL)
16428 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016429 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016430 li = li->li_next;
16431 }
16432
16433 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016434 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016435 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020016436
16437 /* When coming here from Insert mode, sync undo, so that this can be
16438 * undone separately from what was previously inserted. */
16439 if (u_sync_once == 2)
16440 {
16441 u_sync_once = 1; /* notify that u_sync() was called */
16442 u_sync(TRUE);
16443 }
16444
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016445 if (lnum <= curbuf->b_ml.ml_line_count)
16446 {
16447 /* existing line, replace it */
16448 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
16449 {
16450 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000016451 if (lnum == curwin->w_cursor.lnum)
16452 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000016453 rettv->vval.v_number = 0; /* OK */
16454 }
16455 }
16456 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
16457 {
16458 /* lnum is one past the last line, append the line */
16459 ++added;
16460 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
16461 rettv->vval.v_number = 0; /* OK */
16462 }
16463
16464 if (l == NULL) /* only one string argument */
16465 break;
16466 ++lnum;
16467 }
16468
16469 if (added > 0)
16470 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016471}
16472
Bram Moolenaard9ff7d52008-03-20 12:23:49 +000016473static void set_qf_ll_list __ARGS((win_T *wp, typval_T *list_arg, typval_T *action_arg, typval_T *rettv));
16474
Bram Moolenaar071d4272004-06-13 20:20:40 +000016475/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016476 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000016477 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000016478 static void
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016479set_qf_ll_list(wp, list_arg, action_arg, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016480 win_T *wp UNUSED;
16481 typval_T *list_arg UNUSED;
16482 typval_T *action_arg UNUSED;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016483 typval_T *rettv;
16484{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016485#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016486 char_u *act;
16487 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000016488#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016489
Bram Moolenaar2641f772005-03-25 21:58:17 +000016490 rettv->vval.v_number = -1;
16491
16492#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016493 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016494 EMSG(_(e_listreq));
16495 else
16496 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016497 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000016498
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016499 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016500 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016501 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016502 if (act == NULL)
16503 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000016504 if (*act == 'a' || *act == 'r')
16505 action = *act;
16506 }
16507
Bram Moolenaar81484f42012-12-05 15:16:47 +010016508 if (l != NULL && set_errorlist(wp, l, action,
16509 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000016510 rettv->vval.v_number = 0;
16511 }
16512#endif
16513}
16514
16515/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016516 * "setloclist()" function
16517 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016518 static void
16519f_setloclist(argvars, rettv)
16520 typval_T *argvars;
16521 typval_T *rettv;
16522{
16523 win_T *win;
16524
16525 rettv->vval.v_number = -1;
16526
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016527 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016528 if (win != NULL)
16529 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
16530}
16531
16532/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016533 * "setmatches()" function
16534 */
16535 static void
16536f_setmatches(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010016537 typval_T *argvars UNUSED;
16538 typval_T *rettv UNUSED;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016539{
16540#ifdef FEAT_SEARCH_EXTRA
16541 list_T *l;
16542 listitem_T *li;
16543 dict_T *d;
16544
16545 rettv->vval.v_number = -1;
16546 if (argvars[0].v_type != VAR_LIST)
16547 {
16548 EMSG(_(e_listreq));
16549 return;
16550 }
16551 if ((l = argvars[0].vval.v_list) != NULL)
16552 {
16553
16554 /* To some extent make sure that we are dealing with a list from
16555 * "getmatches()". */
16556 li = l->lv_first;
16557 while (li != NULL)
16558 {
16559 if (li->li_tv.v_type != VAR_DICT
16560 || (d = li->li_tv.vval.v_dict) == NULL)
16561 {
16562 EMSG(_(e_invarg));
16563 return;
16564 }
16565 if (!(dict_find(d, (char_u *)"group", -1) != NULL
16566 && dict_find(d, (char_u *)"pattern", -1) != NULL
16567 && dict_find(d, (char_u *)"priority", -1) != NULL
16568 && dict_find(d, (char_u *)"id", -1) != NULL))
16569 {
16570 EMSG(_(e_invarg));
16571 return;
16572 }
16573 li = li->li_next;
16574 }
16575
16576 clear_matches(curwin);
16577 li = l->lv_first;
16578 while (li != NULL)
16579 {
16580 d = li->li_tv.vval.v_dict;
16581 match_add(curwin, get_dict_string(d, (char_u *)"group", FALSE),
16582 get_dict_string(d, (char_u *)"pattern", FALSE),
16583 (int)get_dict_number(d, (char_u *)"priority"),
16584 (int)get_dict_number(d, (char_u *)"id"));
16585 li = li->li_next;
16586 }
16587 rettv->vval.v_number = 0;
16588 }
16589#endif
16590}
16591
16592/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016593 * "setpos()" function
16594 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016595 static void
16596f_setpos(argvars, rettv)
16597 typval_T *argvars;
16598 typval_T *rettv;
16599{
16600 pos_T pos;
16601 int fnum;
16602 char_u *name;
16603
Bram Moolenaar08250432008-02-13 11:42:46 +000016604 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016605 name = get_tv_string_chk(argvars);
16606 if (name != NULL)
16607 {
16608 if (list2fpos(&argvars[1], &pos, &fnum) == OK)
16609 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000016610 if (--pos.col < 0)
16611 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000016612 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016613 {
Bram Moolenaar08250432008-02-13 11:42:46 +000016614 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016615 if (fnum == curbuf->b_fnum)
16616 {
16617 curwin->w_cursor = pos;
16618 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000016619 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016620 }
16621 else
16622 EMSG(_(e_invarg));
16623 }
Bram Moolenaar08250432008-02-13 11:42:46 +000016624 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
16625 {
16626 /* set mark */
16627 if (setmark_pos(name[1], &pos, fnum) == OK)
16628 rettv->vval.v_number = 0;
16629 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000016630 else
16631 EMSG(_(e_invarg));
16632 }
16633 }
16634}
16635
16636/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016637 * "setqflist()" function
16638 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000016639 static void
16640f_setqflist(argvars, rettv)
16641 typval_T *argvars;
16642 typval_T *rettv;
16643{
16644 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
16645}
16646
16647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016648 * "setreg()" function
16649 */
16650 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016651f_setreg(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016652 typval_T *argvars;
16653 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016654{
16655 int regname;
16656 char_u *strregname;
16657 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016658 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016659 int append;
16660 char_u yank_type;
16661 long block_len;
16662
16663 block_len = -1;
16664 yank_type = MAUTO;
16665 append = FALSE;
16666
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016667 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016668 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016670 if (strregname == NULL)
16671 return; /* type error; errmsg already given */
16672 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016673 if (regname == 0 || regname == '@')
16674 regname = '"';
16675 else if (regname == '=')
16676 return;
16677
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016678 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016680 stropt = get_tv_string_chk(&argvars[2]);
16681 if (stropt == NULL)
16682 return; /* type error */
16683 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016684 switch (*stropt)
16685 {
16686 case 'a': case 'A': /* append */
16687 append = TRUE;
16688 break;
16689 case 'v': case 'c': /* character-wise selection */
16690 yank_type = MCHAR;
16691 break;
16692 case 'V': case 'l': /* line-wise selection */
16693 yank_type = MLINE;
16694 break;
16695#ifdef FEAT_VISUAL
16696 case 'b': case Ctrl_V: /* block-wise selection */
16697 yank_type = MBLOCK;
16698 if (VIM_ISDIGIT(stropt[1]))
16699 {
16700 ++stropt;
16701 block_len = getdigits(&stropt) - 1;
16702 --stropt;
16703 }
16704 break;
16705#endif
16706 }
16707 }
16708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016709 strval = get_tv_string_chk(&argvars[1]);
16710 if (strval != NULL)
16711 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000016712 append, yank_type, block_len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016713 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016714}
16715
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016716/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016717 * "settabvar()" function
16718 */
16719 static void
16720f_settabvar(argvars, rettv)
16721 typval_T *argvars;
16722 typval_T *rettv;
16723{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016724#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016725 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016726 tabpage_T *tp;
16727#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016728 char_u *varname, *tabvarname;
16729 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016730
16731 rettv->vval.v_number = 0;
16732
16733 if (check_restricted() || check_secure())
16734 return;
16735
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016736#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016737 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016738#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016739 varname = get_tv_string_chk(&argvars[1]);
16740 varp = &argvars[2];
16741
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016742 if (varname != NULL && varp != NULL
16743#ifdef FEAT_WINDOWS
16744 && tp != NULL
16745#endif
16746 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016747 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016748#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016749 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016750 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016751#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016752
16753 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
16754 if (tabvarname != NULL)
16755 {
16756 STRCPY(tabvarname, "t:");
16757 STRCPY(tabvarname + 2, varname);
16758 set_var(tabvarname, varp, TRUE);
16759 vim_free(tabvarname);
16760 }
16761
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016762#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016763 /* Restore current tabpage */
16764 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020016765 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016766#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020016767 }
16768}
16769
16770/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016771 * "settabwinvar()" function
16772 */
16773 static void
16774f_settabwinvar(argvars, rettv)
16775 typval_T *argvars;
16776 typval_T *rettv;
16777{
16778 setwinvar(argvars, rettv, 1);
16779}
Bram Moolenaar071d4272004-06-13 20:20:40 +000016780
16781/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016782 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016784 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016785f_setwinvar(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016786 typval_T *argvars;
16787 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016788{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016789 setwinvar(argvars, rettv, 0);
16790}
16791
16792/*
16793 * "setwinvar()" and "settabwinvar()" functions
16794 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020016795
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016796 static void
16797setwinvar(argvars, rettv, off)
16798 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000016799 typval_T *rettv UNUSED;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016800 int off;
16801{
Bram Moolenaar071d4272004-06-13 20:20:40 +000016802 win_T *win;
16803#ifdef FEAT_WINDOWS
16804 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016805 tabpage_T *save_curtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016806#endif
16807 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000016808 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016809 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020016810 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016811
16812 if (check_restricted() || check_secure())
16813 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016814
16815#ifdef FEAT_WINDOWS
16816 if (off == 1)
16817 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
16818 else
16819 tp = curtab;
16820#endif
16821 win = find_win_by_nr(&argvars[off], tp);
16822 varname = get_tv_string_chk(&argvars[off + 1]);
16823 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016824
16825 if (win != NULL && varname != NULL && varp != NULL)
16826 {
16827#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016828 if (switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == FAIL)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000016829 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830#endif
16831
16832 if (*varname == '&')
16833 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016834 long numval;
16835 char_u *strval;
16836 int error = FALSE;
16837
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016839 numval = get_tv_number_chk(varp, &error);
16840 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016841 if (!error && strval != NULL)
16842 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016843 }
16844 else
16845 {
16846 winvarname = alloc((unsigned)STRLEN(varname) + 3);
16847 if (winvarname != NULL)
16848 {
16849 STRCPY(winvarname, "w:");
16850 STRCPY(winvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000016851 set_var(winvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016852 vim_free(winvarname);
16853 }
16854 }
16855
16856#ifdef FEAT_WINDOWS
Bram Moolenaard6949742013-06-16 14:18:28 +020016857 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016858#endif
16859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016860}
16861
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010016862#ifdef FEAT_CRYPT
16863/*
16864 * "sha256({string})" function
16865 */
16866 static void
16867f_sha256(argvars, rettv)
16868 typval_T *argvars;
16869 typval_T *rettv;
16870{
16871 char_u *p;
16872
16873 p = get_tv_string(&argvars[0]);
16874 rettv->vval.v_string = vim_strsave(
16875 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
16876 rettv->v_type = VAR_STRING;
16877}
16878#endif /* FEAT_CRYPT */
16879
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016881 * "shellescape({string})" function
16882 */
16883 static void
16884f_shellescape(argvars, rettv)
16885 typval_T *argvars;
16886 typval_T *rettv;
16887{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016888 rettv->vval.v_string = vim_strsave_shellescape(
16889 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]));
Bram Moolenaar60a495f2006-10-03 12:44:42 +000016890 rettv->v_type = VAR_STRING;
16891}
16892
16893/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016894 * shiftwidth() function
16895 */
16896 static void
16897f_shiftwidth(argvars, rettv)
Bram Moolenaar95e09ea2012-10-21 23:56:05 +020016898 typval_T *argvars UNUSED;
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020016899 typval_T *rettv;
16900{
16901 rettv->vval.v_number = get_sw_value();
16902}
16903
16904/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016905 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016906 */
16907 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000016908f_simplify(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000016909 typval_T *argvars;
16910 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016911{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016912 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016913
Bram Moolenaar0d660222005-01-07 21:51:51 +000016914 p = get_tv_string(&argvars[0]);
16915 rettv->vval.v_string = vim_strsave(p);
16916 simplify_filename(rettv->vval.v_string); /* simplify in place */
16917 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016918}
16919
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016920#ifdef FEAT_FLOAT
16921/*
16922 * "sin()" function
16923 */
16924 static void
16925f_sin(argvars, rettv)
16926 typval_T *argvars;
16927 typval_T *rettv;
16928{
16929 float_T f;
16930
16931 rettv->v_type = VAR_FLOAT;
16932 if (get_float_arg(argvars, &f) == OK)
16933 rettv->vval.v_float = sin(f);
16934 else
16935 rettv->vval.v_float = 0.0;
16936}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020016937
16938/*
16939 * "sinh()" function
16940 */
16941 static void
16942f_sinh(argvars, rettv)
16943 typval_T *argvars;
16944 typval_T *rettv;
16945{
16946 float_T f;
16947
16948 rettv->v_type = VAR_FLOAT;
16949 if (get_float_arg(argvars, &f) == OK)
16950 rettv->vval.v_float = sinh(f);
16951 else
16952 rettv->vval.v_float = 0.0;
16953}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016954#endif
16955
Bram Moolenaar0d660222005-01-07 21:51:51 +000016956static int
16957#ifdef __BORLANDC__
16958 _RTLENTRYF
16959#endif
16960 item_compare __ARGS((const void *s1, const void *s2));
16961static int
16962#ifdef __BORLANDC__
16963 _RTLENTRYF
16964#endif
16965 item_compare2 __ARGS((const void *s1, const void *s2));
16966
16967static int item_compare_ic;
16968static char_u *item_compare_func;
Bram Moolenaar5f894962011-06-19 02:55:37 +020016969static dict_T *item_compare_selfdict;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016970static int item_compare_func_err;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016971#define ITEM_COMPARE_FAIL 999
16972
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016974 * Compare functions for f_sort() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000016975 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016976 static int
16977#ifdef __BORLANDC__
16978_RTLENTRYF
16979#endif
16980item_compare(s1, s2)
16981 const void *s1;
16982 const void *s2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016984 char_u *p1, *p2;
16985 char_u *tofree1, *tofree2;
16986 int res;
16987 char_u numbuf1[NUMBUFLEN];
16988 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000016989
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000016990 p1 = tv2string(&(*(listitem_T **)s1)->li_tv, &tofree1, numbuf1, 0);
16991 p2 = tv2string(&(*(listitem_T **)s2)->li_tv, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000016992 if (p1 == NULL)
16993 p1 = (char_u *)"";
16994 if (p2 == NULL)
16995 p2 = (char_u *)"";
Bram Moolenaar0d660222005-01-07 21:51:51 +000016996 if (item_compare_ic)
16997 res = STRICMP(p1, p2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016998 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016999 res = STRCMP(p1, p2);
17000 vim_free(tofree1);
17001 vim_free(tofree2);
17002 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017003}
17004
17005 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006#ifdef __BORLANDC__
17007_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000017008#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +000017009item_compare2(s1, s2)
17010 const void *s1;
17011 const void *s2;
17012{
17013 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000017014 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017015 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000017016 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017018 /* shortcut after failure in previous call; compare all items equal */
17019 if (item_compare_func_err)
17020 return 0;
17021
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017022 /* copy the values. This is needed to be able to set v_lock to VAR_FIXED
17023 * in the copy without changing the original list items. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017024 copy_tv(&(*(listitem_T **)s1)->li_tv, &argv[0]);
17025 copy_tv(&(*(listitem_T **)s2)->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017026
17027 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000017028 res = call_func(item_compare_func, (int)STRLEN(item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020017029 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
17030 item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017031 clear_tv(&argv[0]);
17032 clear_tv(&argv[1]);
17033
17034 if (res == FAIL)
17035 res = ITEM_COMPARE_FAIL;
17036 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017037 res = get_tv_number_chk(&rettv, &item_compare_func_err);
17038 if (item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000017039 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017040 clear_tv(&rettv);
17041 return res;
17042}
17043
17044/*
17045 * "sort({list})" function
17046 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017047 static void
Bram Moolenaar0d660222005-01-07 21:51:51 +000017048f_sort(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017049 typval_T *argvars;
17050 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017051{
Bram Moolenaar33570922005-01-25 22:26:29 +000017052 list_T *l;
17053 listitem_T *li;
17054 listitem_T **ptrs;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017055 long len;
17056 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017057
Bram Moolenaar0d660222005-01-07 21:51:51 +000017058 if (argvars[0].v_type != VAR_LIST)
17059 EMSG2(_(e_listarg), "sort()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000017060 else
17061 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017062 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020017063 if (l == NULL || tv_check_lock(l->lv_lock,
17064 (char_u *)_("sort() argument")))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017065 return;
17066 rettv->vval.v_list = l;
17067 rettv->v_type = VAR_LIST;
17068 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017069
Bram Moolenaar0d660222005-01-07 21:51:51 +000017070 len = list_len(l);
17071 if (len <= 1)
17072 return; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017073
Bram Moolenaar0d660222005-01-07 21:51:51 +000017074 item_compare_ic = FALSE;
17075 item_compare_func = NULL;
Bram Moolenaar5f894962011-06-19 02:55:37 +020017076 item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017077 if (argvars[1].v_type != VAR_UNKNOWN)
17078 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020017079 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017080 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017081 item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082 else
17083 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017084 int error = FALSE;
17085
17086 i = get_tv_number_chk(&argvars[1], &error);
17087 if (error)
17088 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017089 if (i == 1)
17090 item_compare_ic = TRUE;
17091 else
17092 item_compare_func = get_tv_string(&argvars[1]);
17093 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020017094
17095 if (argvars[2].v_type != VAR_UNKNOWN)
17096 {
17097 /* optional third argument: {dict} */
17098 if (argvars[2].v_type != VAR_DICT)
17099 {
17100 EMSG(_(e_dictreq));
17101 return;
17102 }
17103 item_compare_selfdict = argvars[2].vval.v_dict;
17104 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017106
Bram Moolenaar0d660222005-01-07 21:51:51 +000017107 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017108 ptrs = (listitem_T **)alloc((int)(len * sizeof(listitem_T *)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017109 if (ptrs == NULL)
17110 return;
17111 i = 0;
17112 for (li = l->lv_first; li != NULL; li = li->li_next)
17113 ptrs[i++] = li;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017114
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017115 item_compare_func_err = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017116 /* test the compare function */
17117 if (item_compare_func != NULL
17118 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
17119 == ITEM_COMPARE_FAIL)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000017120 EMSG(_("E702: Sort compare function failed"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017121 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017122 {
17123 /* Sort the array with item pointers. */
Bram Moolenaar33570922005-01-25 22:26:29 +000017124 qsort((void *)ptrs, (size_t)len, sizeof(listitem_T *),
Bram Moolenaar0d660222005-01-07 21:51:51 +000017125 item_compare_func == NULL ? item_compare : item_compare2);
17126
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017127 if (!item_compare_func_err)
17128 {
17129 /* Clear the List and append the items in the sorted order. */
Bram Moolenaar52514562008-04-01 11:12:09 +000017130 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017131 l->lv_len = 0;
17132 for (i = 0; i < len; ++i)
17133 list_append(l, ptrs[i]);
17134 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017135 }
17136
17137 vim_free(ptrs);
17138 }
17139}
17140
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017141/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017142 * "soundfold({word})" function
17143 */
17144 static void
17145f_soundfold(argvars, rettv)
17146 typval_T *argvars;
17147 typval_T *rettv;
17148{
17149 char_u *s;
17150
17151 rettv->v_type = VAR_STRING;
17152 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017153#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000017154 rettv->vval.v_string = eval_soundfold(s);
17155#else
17156 rettv->vval.v_string = vim_strsave(s);
17157#endif
17158}
17159
17160/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017161 * "spellbadword()" function
17162 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017163 static void
17164f_spellbadword(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017165 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017166 typval_T *rettv;
17167{
Bram Moolenaar4463f292005-09-25 22:20:24 +000017168 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017169 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017170 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017171
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017172 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017173 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017174
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017175#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000017176 if (argvars[0].v_type == VAR_UNKNOWN)
17177 {
17178 /* Find the start and length of the badly spelled word. */
17179 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
17180 if (len != 0)
17181 word = ml_get_cursor();
17182 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020017183 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017184 {
17185 char_u *str = get_tv_string_chk(&argvars[0]);
17186 int capcol = -1;
17187
17188 if (str != NULL)
17189 {
17190 /* Check the argument for spelling. */
17191 while (*str != NUL)
17192 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000017193 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017194 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000017195 {
17196 word = str;
17197 break;
17198 }
17199 str += len;
17200 }
17201 }
17202 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017203#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000017204
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017205 list_append_string(rettv->vval.v_list, word, len);
17206 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000017207 attr == HLF_SPB ? "bad" :
17208 attr == HLF_SPR ? "rare" :
17209 attr == HLF_SPL ? "local" :
17210 attr == HLF_SPC ? "caps" :
17211 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017212}
17213
17214/*
17215 * "spellsuggest()" function
17216 */
17217 static void
17218f_spellsuggest(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017219 typval_T *argvars UNUSED;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017220 typval_T *rettv;
17221{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017222#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017223 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017224 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017225 int maxcount;
17226 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017227 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017228 listitem_T *li;
17229 int need_capital = FALSE;
17230#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017231
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017232 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017233 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017234
Bram Moolenaar3c56a962006-03-12 22:19:04 +000017235#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020017236 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017237 {
17238 str = get_tv_string(&argvars[0]);
17239 if (argvars[1].v_type != VAR_UNKNOWN)
17240 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017241 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017242 if (maxcount <= 0)
17243 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000017244 if (argvars[2].v_type != VAR_UNKNOWN)
17245 {
17246 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
17247 if (typeerr)
17248 return;
17249 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017250 }
17251 else
17252 maxcount = 25;
17253
Bram Moolenaar4770d092006-01-12 23:22:24 +000017254 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017255
17256 for (i = 0; i < ga.ga_len; ++i)
17257 {
17258 str = ((char_u **)ga.ga_data)[i];
17259
17260 li = listitem_alloc();
17261 if (li == NULL)
17262 vim_free(str);
17263 else
17264 {
17265 li->li_tv.v_type = VAR_STRING;
17266 li->li_tv.v_lock = 0;
17267 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017268 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000017269 }
17270 }
17271 ga_clear(&ga);
17272 }
17273#endif
17274}
17275
Bram Moolenaar0d660222005-01-07 21:51:51 +000017276 static void
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000017277f_split(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017278 typval_T *argvars;
17279 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017280{
17281 char_u *str;
17282 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017283 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017284 regmatch_T regmatch;
17285 char_u patbuf[NUMBUFLEN];
17286 char_u *save_cpo;
17287 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017288 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017289 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017290 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017291
17292 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17293 save_cpo = p_cpo;
17294 p_cpo = (char_u *)"";
17295
17296 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017297 if (argvars[1].v_type != VAR_UNKNOWN)
17298 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017299 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17300 if (pat == NULL)
17301 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017302 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017303 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017304 }
17305 if (pat == NULL || *pat == NUL)
17306 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000017307
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017308 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017310 if (typeerr)
17311 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312
Bram Moolenaar0d660222005-01-07 21:51:51 +000017313 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
17314 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017316 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017317 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017318 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017319 if (*str == NUL)
17320 match = FALSE; /* empty item at the end */
17321 else
17322 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017323 if (match)
17324 end = regmatch.startp[0];
17325 else
17326 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017327 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
17328 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017329 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017330 if (list_append_string(rettv->vval.v_list, str,
17331 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017332 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017333 }
17334 if (!match)
17335 break;
17336 /* Advance to just after the match. */
17337 if (regmatch.endp[0] > str)
17338 col = 0;
17339 else
17340 {
17341 /* Don't get stuck at the same match. */
17342#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000017343 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017344#else
17345 col = 1;
17346#endif
17347 }
17348 str = regmatch.endp[0];
17349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017350
Bram Moolenaar473de612013-06-08 18:19:48 +020017351 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017353
Bram Moolenaar0d660222005-01-07 21:51:51 +000017354 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017355}
17356
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017357#ifdef FEAT_FLOAT
17358/*
17359 * "sqrt()" function
17360 */
17361 static void
17362f_sqrt(argvars, rettv)
17363 typval_T *argvars;
17364 typval_T *rettv;
17365{
17366 float_T f;
17367
17368 rettv->v_type = VAR_FLOAT;
17369 if (get_float_arg(argvars, &f) == OK)
17370 rettv->vval.v_float = sqrt(f);
17371 else
17372 rettv->vval.v_float = 0.0;
17373}
17374
17375/*
17376 * "str2float()" function
17377 */
17378 static void
17379f_str2float(argvars, rettv)
17380 typval_T *argvars;
17381 typval_T *rettv;
17382{
17383 char_u *p = skipwhite(get_tv_string(&argvars[0]));
17384
17385 if (*p == '+')
17386 p = skipwhite(p + 1);
17387 (void)string2float(p, &rettv->vval.v_float);
17388 rettv->v_type = VAR_FLOAT;
17389}
17390#endif
17391
Bram Moolenaar2c932302006-03-18 21:42:09 +000017392/*
17393 * "str2nr()" function
17394 */
17395 static void
17396f_str2nr(argvars, rettv)
17397 typval_T *argvars;
17398 typval_T *rettv;
17399{
17400 int base = 10;
17401 char_u *p;
17402 long n;
17403
17404 if (argvars[1].v_type != VAR_UNKNOWN)
17405 {
17406 base = get_tv_number(&argvars[1]);
17407 if (base != 8 && base != 10 && base != 16)
17408 {
17409 EMSG(_(e_invarg));
17410 return;
17411 }
17412 }
17413
17414 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017415 if (*p == '+')
17416 p = skipwhite(p + 1);
Bram Moolenaar2c932302006-03-18 21:42:09 +000017417 vim_str2nr(p, NULL, NULL, base == 8 ? 2 : 0, base == 16 ? 2 : 0, &n, NULL);
17418 rettv->vval.v_number = n;
17419}
17420
Bram Moolenaar071d4272004-06-13 20:20:40 +000017421#ifdef HAVE_STRFTIME
17422/*
17423 * "strftime({format}[, {time}])" function
17424 */
17425 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017426f_strftime(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017427 typval_T *argvars;
17428 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429{
17430 char_u result_buf[256];
17431 struct tm *curtime;
17432 time_t seconds;
17433 char_u *p;
17434
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017435 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017437 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017438 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439 seconds = time(NULL);
17440 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017441 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442 curtime = localtime(&seconds);
17443 /* MSVC returns NULL for an invalid value of seconds. */
17444 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017445 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017446 else
17447 {
17448# ifdef FEAT_MBYTE
17449 vimconv_T conv;
17450 char_u *enc;
17451
17452 conv.vc_type = CONV_NONE;
17453 enc = enc_locale();
17454 convert_setup(&conv, p_enc, enc);
17455 if (conv.vc_type != CONV_NONE)
17456 p = string_convert(&conv, p, NULL);
17457# endif
17458 if (p != NULL)
17459 (void)strftime((char *)result_buf, sizeof(result_buf),
17460 (char *)p, curtime);
17461 else
17462 result_buf[0] = NUL;
17463
17464# ifdef FEAT_MBYTE
17465 if (conv.vc_type != CONV_NONE)
17466 vim_free(p);
17467 convert_setup(&conv, enc, p_enc);
17468 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017469 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017470 else
17471# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017472 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473
17474# ifdef FEAT_MBYTE
17475 /* Release conversion descriptors */
17476 convert_setup(&conv, NULL, NULL);
17477 vim_free(enc);
17478# endif
17479 }
17480}
17481#endif
17482
17483/*
17484 * "stridx()" function
17485 */
17486 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017487f_stridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017488 typval_T *argvars;
17489 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490{
17491 char_u buf[NUMBUFLEN];
17492 char_u *needle;
17493 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000017494 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017495 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000017496 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017498 needle = get_tv_string_chk(&argvars[1]);
17499 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000017500 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017501 if (needle == NULL || haystack == NULL)
17502 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503
Bram Moolenaar33570922005-01-25 22:26:29 +000017504 if (argvars[2].v_type != VAR_UNKNOWN)
17505 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017506 int error = FALSE;
17507
17508 start_idx = get_tv_number_chk(&argvars[2], &error);
17509 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000017510 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017511 if (start_idx >= 0)
17512 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000017513 }
17514
17515 pos = (char_u *)strstr((char *)haystack, (char *)needle);
17516 if (pos != NULL)
17517 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017518}
17519
17520/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017521 * "string()" function
17522 */
17523 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017524f_string(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017525 typval_T *argvars;
17526 typval_T *rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017527{
17528 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017529 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017531 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000017532 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017533 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000017534 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017535 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536}
17537
17538/*
17539 * "strlen()" function
17540 */
17541 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017542f_strlen(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017543 typval_T *argvars;
17544 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017546 rettv->vval.v_number = (varnumber_T)(STRLEN(
17547 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017548}
17549
17550/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017551 * "strchars()" function
17552 */
17553 static void
17554f_strchars(argvars, rettv)
17555 typval_T *argvars;
17556 typval_T *rettv;
17557{
17558 char_u *s = get_tv_string(&argvars[0]);
17559#ifdef FEAT_MBYTE
17560 varnumber_T len = 0;
17561
17562 while (*s != NUL)
17563 {
17564 mb_cptr2char_adv(&s);
17565 ++len;
17566 }
17567 rettv->vval.v_number = len;
17568#else
17569 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
17570#endif
17571}
17572
17573/*
Bram Moolenaardc536092010-07-18 15:45:49 +020017574 * "strdisplaywidth()" function
17575 */
17576 static void
17577f_strdisplaywidth(argvars, rettv)
17578 typval_T *argvars;
17579 typval_T *rettv;
17580{
17581 char_u *s = get_tv_string(&argvars[0]);
17582 int col = 0;
17583
17584 if (argvars[1].v_type != VAR_UNKNOWN)
17585 col = get_tv_number(&argvars[1]);
17586
Bram Moolenaar8a09b982010-07-22 22:20:57 +020017587 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020017588}
17589
17590/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020017591 * "strwidth()" function
17592 */
17593 static void
17594f_strwidth(argvars, rettv)
17595 typval_T *argvars;
17596 typval_T *rettv;
17597{
17598 char_u *s = get_tv_string(&argvars[0]);
17599
17600 rettv->vval.v_number = (varnumber_T)(
17601#ifdef FEAT_MBYTE
17602 mb_string2cells(s, -1)
17603#else
17604 STRLEN(s)
17605#endif
17606 );
17607}
17608
17609/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610 * "strpart()" function
17611 */
17612 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017613f_strpart(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017614 typval_T *argvars;
17615 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616{
17617 char_u *p;
17618 int n;
17619 int len;
17620 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017621 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017623 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017624 slen = (int)STRLEN(p);
17625
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017626 n = get_tv_number_chk(&argvars[1], &error);
17627 if (error)
17628 len = 0;
17629 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017630 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 else
17632 len = slen - n; /* default len: all bytes that are available. */
17633
17634 /*
17635 * Only return the overlap between the specified part and the actual
17636 * string.
17637 */
17638 if (n < 0)
17639 {
17640 len += n;
17641 n = 0;
17642 }
17643 else if (n > slen)
17644 n = slen;
17645 if (len < 0)
17646 len = 0;
17647 else if (n + len > slen)
17648 len = slen - n;
17649
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017650 rettv->v_type = VAR_STRING;
17651 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652}
17653
17654/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017655 * "strridx()" function
17656 */
17657 static void
17658f_strridx(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017659 typval_T *argvars;
17660 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017661{
17662 char_u buf[NUMBUFLEN];
17663 char_u *needle;
17664 char_u *haystack;
17665 char_u *rest;
17666 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000017667 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017668
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017669 needle = get_tv_string_chk(&argvars[1]);
17670 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017671
17672 rettv->vval.v_number = -1;
17673 if (needle == NULL || haystack == NULL)
17674 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017675
17676 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017677 if (argvars[2].v_type != VAR_UNKNOWN)
17678 {
17679 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017680 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000017681 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017682 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017683 }
17684 else
17685 end_idx = haystack_len;
17686
Bram Moolenaar0d660222005-01-07 21:51:51 +000017687 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000017688 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017689 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000017690 lastmatch = haystack + end_idx;
17691 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017692 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000017693 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017694 for (rest = haystack; *rest != '\0'; ++rest)
17695 {
17696 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000017697 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017698 break;
17699 lastmatch = rest;
17700 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000017701 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017702
17703 if (lastmatch == NULL)
17704 rettv->vval.v_number = -1;
17705 else
17706 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
17707}
17708
17709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 * "strtrans()" function
17711 */
17712 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713f_strtrans(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017714 typval_T *argvars;
17715 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017717 rettv->v_type = VAR_STRING;
17718 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719}
17720
17721/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017722 * "submatch()" function
17723 */
17724 static void
17725f_submatch(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017726 typval_T *argvars;
17727 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017728{
17729 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017730 rettv->vval.v_string =
17731 reg_submatch((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar0d660222005-01-07 21:51:51 +000017732}
17733
17734/*
17735 * "substitute()" function
17736 */
17737 static void
17738f_substitute(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000017739 typval_T *argvars;
17740 typval_T *rettv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017741{
17742 char_u patbuf[NUMBUFLEN];
17743 char_u subbuf[NUMBUFLEN];
17744 char_u flagsbuf[NUMBUFLEN];
17745
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017746 char_u *str = get_tv_string_chk(&argvars[0]);
17747 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
17748 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
17749 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
17750
Bram Moolenaar0d660222005-01-07 21:51:51 +000017751 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017752 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
17753 rettv->vval.v_string = NULL;
17754 else
17755 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017756}
17757
17758/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017759 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017760 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017762f_synID(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017763 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017764 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765{
17766 int id = 0;
17767#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017768 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017769 long col;
17770 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000017771 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017773 lnum = get_tv_lnum(argvars); /* -1 on type error */
17774 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17775 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017777 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000017778 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017779 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017780#endif
17781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017782 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017783}
17784
17785/*
17786 * "synIDattr(id, what [, mode])" function
17787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017788 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017789f_synIDattr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017790 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017792{
17793 char_u *p = NULL;
17794#ifdef FEAT_SYN_HL
17795 int id;
17796 char_u *what;
17797 char_u *mode;
17798 char_u modebuf[NUMBUFLEN];
17799 int modec;
17800
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017801 id = get_tv_number(&argvars[0]);
17802 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017803 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017805 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020017807 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808 modec = 0; /* replace invalid with current */
17809 }
17810 else
17811 {
17812#ifdef FEAT_GUI
17813 if (gui.in_use)
17814 modec = 'g';
17815 else
17816#endif
17817 if (t_colors > 1)
17818 modec = 'c';
17819 else
17820 modec = 't';
17821 }
17822
17823
17824 switch (TOLOWER_ASC(what[0]))
17825 {
17826 case 'b':
17827 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
17828 p = highlight_color(id, what, modec);
17829 else /* bold */
17830 p = highlight_has_attr(id, HL_BOLD, modec);
17831 break;
17832
Bram Moolenaar12682fd2010-03-10 13:43:49 +010017833 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017834 p = highlight_color(id, what, modec);
17835 break;
17836
17837 case 'i':
17838 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
17839 p = highlight_has_attr(id, HL_INVERSE, modec);
17840 else /* italic */
17841 p = highlight_has_attr(id, HL_ITALIC, modec);
17842 break;
17843
17844 case 'n': /* name */
17845 p = get_highlight_name(NULL, id - 1);
17846 break;
17847
17848 case 'r': /* reverse */
17849 p = highlight_has_attr(id, HL_INVERSE, modec);
17850 break;
17851
Bram Moolenaar6f507d62008-11-28 10:16:05 +000017852 case 's':
17853 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
17854 p = highlight_color(id, what, modec);
17855 else /* standout */
17856 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857 break;
17858
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000017859 case 'u':
17860 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
17861 /* underline */
17862 p = highlight_has_attr(id, HL_UNDERLINE, modec);
17863 else
17864 /* undercurl */
17865 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866 break;
17867 }
17868
17869 if (p != NULL)
17870 p = vim_strsave(p);
17871#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017872 rettv->v_type = VAR_STRING;
17873 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017874}
17875
17876/*
17877 * "synIDtrans(id)" function
17878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017880f_synIDtrans(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017881 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000017882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017883{
17884 int id;
17885
17886#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017887 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017888
17889 if (id > 0)
17890 id = syn_get_final_id(id);
17891 else
17892#endif
17893 id = 0;
17894
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017895 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017896}
17897
17898/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020017899 * "synconcealed(lnum, col)" function
17900 */
17901 static void
17902f_synconcealed(argvars, rettv)
17903 typval_T *argvars UNUSED;
17904 typval_T *rettv;
17905{
17906#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17907 long lnum;
17908 long col;
17909 int syntax_flags = 0;
17910 int cchar;
17911 int matchid = 0;
17912 char_u str[NUMBUFLEN];
17913#endif
17914
17915 rettv->v_type = VAR_LIST;
17916 rettv->vval.v_list = NULL;
17917
17918#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
17919 lnum = get_tv_lnum(argvars); /* -1 on type error */
17920 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17921
17922 vim_memset(str, NUL, sizeof(str));
17923
17924 if (rettv_list_alloc(rettv) != FAIL)
17925 {
17926 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
17927 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
17928 && curwin->w_p_cole > 0)
17929 {
17930 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
17931 syntax_flags = get_syntax_info(&matchid);
17932
17933 /* get the conceal character */
17934 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
17935 {
17936 cchar = syn_get_sub_char();
17937 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
17938 cchar = lcs_conceal;
17939 if (cchar != NUL)
17940 {
17941# ifdef FEAT_MBYTE
17942 if (has_mbyte)
17943 (*mb_char2bytes)(cchar, str);
17944 else
17945# endif
17946 str[0] = cchar;
17947 }
17948 }
17949 }
17950
17951 list_append_number(rettv->vval.v_list,
17952 (syntax_flags & HL_CONCEAL) != 0);
17953 /* -1 to auto-determine strlen */
17954 list_append_string(rettv->vval.v_list, str, -1);
17955 list_append_number(rettv->vval.v_list, matchid);
17956 }
17957#endif
17958}
17959
17960/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017961 * "synstack(lnum, col)" function
17962 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017963 static void
17964f_synstack(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000017965 typval_T *argvars UNUSED;
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017966 typval_T *rettv;
17967{
17968#ifdef FEAT_SYN_HL
17969 long lnum;
17970 long col;
17971 int i;
17972 int id;
17973#endif
17974
17975 rettv->v_type = VAR_LIST;
17976 rettv->vval.v_list = NULL;
17977
17978#ifdef FEAT_SYN_HL
17979 lnum = get_tv_lnum(argvars); /* -1 on type error */
17980 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
17981
17982 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020017983 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017984 && rettv_list_alloc(rettv) != FAIL)
17985 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000017986 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000017987 for (i = 0; ; ++i)
17988 {
17989 id = syn_get_stack_item(i);
17990 if (id < 0)
17991 break;
17992 if (list_append_number(rettv->vval.v_list, id) == FAIL)
17993 break;
17994 }
17995 }
17996#endif
17997}
17998
17999/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018000 * "system()" function
18001 */
18002 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018003f_system(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018004 typval_T *argvars;
18005 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018007 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018008 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018009 char_u *infile = NULL;
18010 char_u buf[NUMBUFLEN];
18011 int err = FALSE;
18012 FILE *fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018013
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018014 if (check_restricted() || check_secure())
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018015 goto done;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018016
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018017 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018018 {
18019 /*
18020 * Write the string to a temp file, to be used for input of the shell
18021 * command.
18022 */
18023 if ((infile = vim_tempname('i')) == NULL)
18024 {
18025 EMSG(_(e_notmp));
Bram Moolenaare6f565a2007-12-07 16:09:32 +000018026 goto done;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018027 }
18028
18029 fd = mch_fopen((char *)infile, WRITEBIN);
18030 if (fd == NULL)
18031 {
18032 EMSG2(_(e_notopen), infile);
18033 goto done;
18034 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018035 p = get_tv_string_buf_chk(&argvars[1], buf);
18036 if (p == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018037 {
18038 fclose(fd);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018039 goto done; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018040 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018041 if (fwrite(p, STRLEN(p), 1, fd) != 1)
18042 err = TRUE;
18043 if (fclose(fd) != 0)
18044 err = TRUE;
18045 if (err)
18046 {
18047 EMSG(_("E677: Error writing temp file"));
18048 goto done;
18049 }
18050 }
18051
Bram Moolenaare580b0c2006-03-21 21:33:03 +000018052 res = get_cmd_output(get_tv_string(&argvars[0]), infile,
18053 SHELL_SILENT | SHELL_COOKED);
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018054
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055#ifdef USE_CR
18056 /* translate <CR> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018057 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058 {
18059 char_u *s;
18060
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018061 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 {
18063 if (*s == CAR)
18064 *s = NL;
18065 }
18066 }
18067#else
18068# ifdef USE_CRNL
18069 /* translate <CR><NL> into <NL> */
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018070 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018071 {
18072 char_u *s, *d;
18073
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018074 d = res;
18075 for (s = res; *s; ++s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018076 {
18077 if (s[0] == CAR && s[1] == NL)
18078 ++s;
18079 *d++ = *s;
18080 }
18081 *d = NUL;
18082 }
18083# endif
18084#endif
Bram Moolenaarc0197e22004-09-13 20:26:32 +000018085
18086done:
18087 if (infile != NULL)
18088 {
18089 mch_remove(infile);
18090 vim_free(infile);
18091 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018092 rettv->v_type = VAR_STRING;
18093 rettv->vval.v_string = res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018094}
18095
18096/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018097 * "tabpagebuflist()" function
18098 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018099 static void
18100f_tabpagebuflist(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018101 typval_T *argvars UNUSED;
18102 typval_T *rettv UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018103{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018104#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018105 tabpage_T *tp;
18106 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018107
18108 if (argvars[0].v_type == VAR_UNKNOWN)
18109 wp = firstwin;
18110 else
18111 {
18112 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18113 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000018114 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018115 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018116 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018117 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018118 for (; wp != NULL; wp = wp->w_next)
18119 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018120 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018121 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018122 }
18123#endif
18124}
18125
18126
18127/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018128 * "tabpagenr()" function
18129 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018130 static void
18131f_tabpagenr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018132 typval_T *argvars UNUSED;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018133 typval_T *rettv;
18134{
18135 int nr = 1;
18136#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018137 char_u *arg;
18138
18139 if (argvars[0].v_type != VAR_UNKNOWN)
18140 {
18141 arg = get_tv_string_chk(&argvars[0]);
18142 nr = 0;
18143 if (arg != NULL)
18144 {
18145 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000018146 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018147 else
18148 EMSG2(_(e_invexpr2), arg);
18149 }
18150 }
18151 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000018152 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018153#endif
18154 rettv->vval.v_number = nr;
18155}
18156
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018157
18158#ifdef FEAT_WINDOWS
18159static int get_winnr __ARGS((tabpage_T *tp, typval_T *argvar));
18160
18161/*
18162 * Common code for tabpagewinnr() and winnr().
18163 */
18164 static int
18165get_winnr(tp, argvar)
18166 tabpage_T *tp;
18167 typval_T *argvar;
18168{
18169 win_T *twin;
18170 int nr = 1;
18171 win_T *wp;
18172 char_u *arg;
18173
18174 twin = (tp == curtab) ? curwin : tp->tp_curwin;
18175 if (argvar->v_type != VAR_UNKNOWN)
18176 {
18177 arg = get_tv_string_chk(argvar);
18178 if (arg == NULL)
18179 nr = 0; /* type error; errmsg already given */
18180 else if (STRCMP(arg, "$") == 0)
18181 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
18182 else if (STRCMP(arg, "#") == 0)
18183 {
18184 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
18185 if (twin == NULL)
18186 nr = 0;
18187 }
18188 else
18189 {
18190 EMSG2(_(e_invexpr2), arg);
18191 nr = 0;
18192 }
18193 }
18194
18195 if (nr > 0)
18196 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
18197 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018198 {
18199 if (wp == NULL)
18200 {
18201 /* didn't find it in this tabpage */
18202 nr = 0;
18203 break;
18204 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018205 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000018206 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018207 return nr;
18208}
18209#endif
18210
18211/*
18212 * "tabpagewinnr()" function
18213 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018214 static void
18215f_tabpagewinnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018216 typval_T *argvars UNUSED;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018217 typval_T *rettv;
18218{
18219 int nr = 1;
18220#ifdef FEAT_WINDOWS
18221 tabpage_T *tp;
18222
18223 tp = find_tabpage((int)get_tv_number(&argvars[0]));
18224 if (tp == NULL)
18225 nr = 0;
18226 else
18227 nr = get_winnr(tp, &argvars[1]);
18228#endif
18229 rettv->vval.v_number = nr;
18230}
18231
18232
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000018233/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018234 * "tagfiles()" function
18235 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018236 static void
18237f_tagfiles(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018238 typval_T *argvars UNUSED;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018239 typval_T *rettv;
18240{
Bram Moolenaard9462e32011-04-11 21:35:11 +020018241 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018242 tagname_T tn;
18243 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018244
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018245 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018246 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020018247 fname = alloc(MAXPATHL);
18248 if (fname == NULL)
18249 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018250
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018251 for (first = TRUE; ; first = FALSE)
18252 if (get_tagfname(&tn, first, fname) == FAIL
18253 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018254 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018255 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020018256 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000018257}
18258
18259/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000018260 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018261 */
18262 static void
18263f_taglist(argvars, rettv)
18264 typval_T *argvars;
18265 typval_T *rettv;
18266{
18267 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018268
18269 tag_pattern = get_tv_string(&argvars[0]);
18270
18271 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018272 if (*tag_pattern == NUL)
18273 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018274
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018275 if (rettv_list_alloc(rettv) == OK)
18276 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000018277}
18278
18279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280 * "tempname()" function
18281 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018282 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018283f_tempname(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018284 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018285 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018286{
18287 static int x = 'A';
18288
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018289 rettv->v_type = VAR_STRING;
18290 rettv->vval.v_string = vim_tempname(x);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018291
18292 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
18293 * names. Skip 'I' and 'O', they are used for shell redirection. */
18294 do
18295 {
18296 if (x == 'Z')
18297 x = '0';
18298 else if (x == '9')
18299 x = 'A';
18300 else
18301 {
18302#ifdef EBCDIC
18303 if (x == 'I')
18304 x = 'J';
18305 else if (x == 'R')
18306 x = 'S';
18307 else
18308#endif
18309 ++x;
18310 }
18311 } while (x == 'I' || x == 'O');
18312}
18313
18314/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000018315 * "test(list)" function: Just checking the walls...
18316 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000018317 static void
18318f_test(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018319 typval_T *argvars UNUSED;
18320 typval_T *rettv UNUSED;
Bram Moolenaard52d9742005-08-21 22:20:28 +000018321{
18322 /* Used for unit testing. Change the code below to your liking. */
18323#if 0
18324 listitem_T *li;
18325 list_T *l;
18326 char_u *bad, *good;
18327
18328 if (argvars[0].v_type != VAR_LIST)
18329 return;
18330 l = argvars[0].vval.v_list;
18331 if (l == NULL)
18332 return;
18333 li = l->lv_first;
18334 if (li == NULL)
18335 return;
18336 bad = get_tv_string(&li->li_tv);
18337 li = li->li_next;
18338 if (li == NULL)
18339 return;
18340 good = get_tv_string(&li->li_tv);
18341 rettv->vval.v_number = test_edit_score(bad, good);
18342#endif
18343}
18344
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018345#ifdef FEAT_FLOAT
18346/*
18347 * "tan()" function
18348 */
18349 static void
18350f_tan(argvars, rettv)
18351 typval_T *argvars;
18352 typval_T *rettv;
18353{
18354 float_T f;
18355
18356 rettv->v_type = VAR_FLOAT;
18357 if (get_float_arg(argvars, &f) == OK)
18358 rettv->vval.v_float = tan(f);
18359 else
18360 rettv->vval.v_float = 0.0;
18361}
18362
18363/*
18364 * "tanh()" function
18365 */
18366 static void
18367f_tanh(argvars, rettv)
18368 typval_T *argvars;
18369 typval_T *rettv;
18370{
18371 float_T f;
18372
18373 rettv->v_type = VAR_FLOAT;
18374 if (get_float_arg(argvars, &f) == OK)
18375 rettv->vval.v_float = tanh(f);
18376 else
18377 rettv->vval.v_float = 0.0;
18378}
18379#endif
18380
Bram Moolenaard52d9742005-08-21 22:20:28 +000018381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018382 * "tolower(string)" function
18383 */
18384 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018385f_tolower(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018386 typval_T *argvars;
18387 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018388{
18389 char_u *p;
18390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018391 p = vim_strsave(get_tv_string(&argvars[0]));
18392 rettv->v_type = VAR_STRING;
18393 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394
18395 if (p != NULL)
18396 while (*p != NUL)
18397 {
18398#ifdef FEAT_MBYTE
18399 int l;
18400
18401 if (enc_utf8)
18402 {
18403 int c, lc;
18404
18405 c = utf_ptr2char(p);
18406 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018407 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018408 /* TODO: reallocate string when byte count changes. */
18409 if (utf_char2len(lc) == l)
18410 utf_char2bytes(lc, p);
18411 p += l;
18412 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018413 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414 p += l; /* skip multi-byte character */
18415 else
18416#endif
18417 {
18418 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
18419 ++p;
18420 }
18421 }
18422}
18423
18424/*
18425 * "toupper(string)" function
18426 */
18427 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018428f_toupper(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018429 typval_T *argvars;
18430 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018431{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018432 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018433 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000018434}
18435
18436/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000018437 * "tr(string, fromstr, tostr)" function
18438 */
18439 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018440f_tr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018441 typval_T *argvars;
18442 typval_T *rettv;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018443{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018444 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018445 char_u *fromstr;
18446 char_u *tostr;
18447 char_u *p;
18448#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000018449 int inlen;
18450 int fromlen;
18451 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018452 int idx;
18453 char_u *cpstr;
18454 int cplen;
18455 int first = TRUE;
18456#endif
18457 char_u buf[NUMBUFLEN];
18458 char_u buf2[NUMBUFLEN];
18459 garray_T ga;
18460
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018461 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018462 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
18463 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018464
18465 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018466 rettv->v_type = VAR_STRING;
18467 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018468 if (fromstr == NULL || tostr == NULL)
18469 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018470 ga_init2(&ga, (int)sizeof(char), 80);
18471
18472#ifdef FEAT_MBYTE
18473 if (!has_mbyte)
18474#endif
18475 /* not multi-byte: fromstr and tostr must be the same length */
18476 if (STRLEN(fromstr) != STRLEN(tostr))
18477 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018478#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000018479error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000018480#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000018481 EMSG2(_(e_invarg2), fromstr);
18482 ga_clear(&ga);
18483 return;
18484 }
18485
18486 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018487 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018488 {
18489#ifdef FEAT_MBYTE
18490 if (has_mbyte)
18491 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018492 inlen = (*mb_ptr2len)(in_str);
18493 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018494 cplen = inlen;
18495 idx = 0;
18496 for (p = fromstr; *p != NUL; p += fromlen)
18497 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018498 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018499 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018500 {
18501 for (p = tostr; *p != NUL; p += tolen)
18502 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018503 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018504 if (idx-- == 0)
18505 {
18506 cplen = tolen;
18507 cpstr = p;
18508 break;
18509 }
18510 }
18511 if (*p == NUL) /* tostr is shorter than fromstr */
18512 goto error;
18513 break;
18514 }
18515 ++idx;
18516 }
18517
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018518 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000018519 {
18520 /* Check that fromstr and tostr have the same number of
18521 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018522 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000018523 first = FALSE;
18524 for (p = tostr; *p != NUL; p += tolen)
18525 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000018526 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018527 --idx;
18528 }
18529 if (idx != 0)
18530 goto error;
18531 }
18532
18533 ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000018534 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018535 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018536
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018537 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018538 }
18539 else
18540#endif
18541 {
18542 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018543 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000018544 if (p != NULL)
18545 ga_append(&ga, tostr[p - fromstr]);
18546 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010018547 ga_append(&ga, *in_str);
18548 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018549 }
18550 }
18551
Bram Moolenaar61b974b2006-12-05 09:32:29 +000018552 /* add a terminating NUL */
18553 ga_grow(&ga, 1);
18554 ga_append(&ga, NUL);
18555
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018556 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000018557}
18558
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018559#ifdef FEAT_FLOAT
18560/*
18561 * "trunc({float})" function
18562 */
18563 static void
18564f_trunc(argvars, rettv)
18565 typval_T *argvars;
18566 typval_T *rettv;
18567{
18568 float_T f;
18569
18570 rettv->v_type = VAR_FLOAT;
18571 if (get_float_arg(argvars, &f) == OK)
18572 /* trunc() is not in C90, use floor() or ceil() instead. */
18573 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
18574 else
18575 rettv->vval.v_float = 0.0;
18576}
18577#endif
18578
Bram Moolenaar8299df92004-07-10 09:47:34 +000018579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018580 * "type(expr)" function
18581 */
18582 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018583f_type(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018584 typval_T *argvars;
18585 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018586{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018587 int n;
18588
18589 switch (argvars[0].v_type)
18590 {
18591 case VAR_NUMBER: n = 0; break;
18592 case VAR_STRING: n = 1; break;
18593 case VAR_FUNC: n = 2; break;
18594 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000018595 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018596#ifdef FEAT_FLOAT
18597 case VAR_FLOAT: n = 5; break;
18598#endif
Bram Moolenaar6cc16192005-01-08 21:49:45 +000018599 default: EMSG2(_(e_intern2), "f_type()"); n = 0; break;
18600 }
18601 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602}
18603
18604/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018605 * "undofile(name)" function
18606 */
18607 static void
18608f_undofile(argvars, rettv)
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +010018609 typval_T *argvars UNUSED;
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018610 typval_T *rettv;
18611{
18612 rettv->v_type = VAR_STRING;
18613#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018614 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018615 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018616
Bram Moolenaarb41d9682012-04-30 17:35:48 +020018617 if (*fname == NUL)
18618 {
18619 /* If there is no file name there will be no undo file. */
18620 rettv->vval.v_string = NULL;
18621 }
18622 else
18623 {
18624 char_u *ffname = FullName_save(fname, FALSE);
18625
18626 if (ffname != NULL)
18627 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
18628 vim_free(ffname);
18629 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020018630 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020018631#else
18632 rettv->vval.v_string = NULL;
18633#endif
18634}
18635
18636/*
Bram Moolenaara800b422010-06-27 01:15:55 +020018637 * "undotree()" function
18638 */
18639 static void
18640f_undotree(argvars, rettv)
18641 typval_T *argvars UNUSED;
18642 typval_T *rettv;
18643{
18644 if (rettv_dict_alloc(rettv) == OK)
18645 {
18646 dict_T *dict = rettv->vval.v_dict;
18647 list_T *list;
18648
Bram Moolenaar730cde92010-06-27 05:18:54 +020018649 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018650 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018651 dict_add_nr_str(dict, "save_last",
18652 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018653 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
18654 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020018655 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020018656
18657 list = list_alloc();
18658 if (list != NULL)
18659 {
18660 u_eval_tree(curbuf->b_u_oldhead, list);
18661 dict_add_list(dict, "entries", list);
18662 }
18663 }
18664}
18665
18666/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000018667 * "values(dict)" function
18668 */
18669 static void
18670f_values(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018671 typval_T *argvars;
18672 typval_T *rettv;
Bram Moolenaar8c711452005-01-14 21:53:12 +000018673{
18674 dict_list(argvars, rettv, 1);
18675}
18676
18677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018678 * "virtcol(string)" function
18679 */
18680 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018681f_virtcol(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018682 typval_T *argvars;
18683 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684{
18685 colnr_T vcol = 0;
18686 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018687 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018688
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018689 fp = var2fpos(&argvars[0], FALSE, &fnum);
18690 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
18691 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018692 {
18693 getvvcol(curwin, fp, NULL, NULL, &vcol);
18694 ++vcol;
18695 }
18696
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018697 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698}
18699
18700/*
18701 * "visualmode()" function
18702 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018703 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018704f_visualmode(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018705 typval_T *argvars UNUSED;
18706 typval_T *rettv UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018707{
18708#ifdef FEAT_VISUAL
18709 char_u str[2];
18710
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018711 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712 str[0] = curbuf->b_visual_mode_eval;
18713 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018714 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715
18716 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018717 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018719#endif
18720}
18721
18722/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010018723 * "wildmenumode()" function
18724 */
18725 static void
18726f_wildmenumode(argvars, rettv)
18727 typval_T *argvars UNUSED;
18728 typval_T *rettv UNUSED;
18729{
18730#ifdef FEAT_WILDMENU
18731 if (wild_menu_showing)
18732 rettv->vval.v_number = 1;
18733#endif
18734}
18735
18736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018737 * "winbufnr(nr)" function
18738 */
18739 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018740f_winbufnr(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018741 typval_T *argvars;
18742 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743{
18744 win_T *wp;
18745
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018746 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018748 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018749 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018750 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018751}
18752
18753/*
18754 * "wincol()" function
18755 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018756 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018757f_wincol(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018758 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018759 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760{
18761 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018762 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018763}
18764
18765/*
18766 * "winheight(nr)" function
18767 */
18768 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018769f_winheight(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018770 typval_T *argvars;
18771 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018772{
18773 win_T *wp;
18774
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018775 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018776 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018777 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018779 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780}
18781
18782/*
18783 * "winline()" function
18784 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018785 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018786f_winline(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018787 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018788 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789{
18790 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018791 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792}
18793
18794/*
18795 * "winnr()" function
18796 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018798f_winnr(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018799 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018800 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801{
18802 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018803
Bram Moolenaar071d4272004-06-13 20:20:40 +000018804#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000018805 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018807 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018808}
18809
18810/*
18811 * "winrestcmd()" function
18812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018813 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018814f_winrestcmd(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018815 typval_T *argvars UNUSED;
Bram Moolenaar33570922005-01-25 22:26:29 +000018816 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817{
18818#ifdef FEAT_WINDOWS
18819 win_T *wp;
18820 int winnr = 1;
18821 garray_T ga;
18822 char_u buf[50];
18823
18824 ga_init2(&ga, (int)sizeof(char), 70);
18825 for (wp = firstwin; wp != NULL; wp = wp->w_next)
18826 {
18827 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
18828 ga_concat(&ga, buf);
18829# ifdef FEAT_VERTSPLIT
18830 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
18831 ga_concat(&ga, buf);
18832# endif
18833 ++winnr;
18834 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000018835 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018837 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018838#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018839 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018840#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018841 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842}
18843
18844/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018845 * "winrestview()" function
18846 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018847 static void
18848f_winrestview(argvars, rettv)
18849 typval_T *argvars;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018850 typval_T *rettv UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018851{
18852 dict_T *dict;
18853
18854 if (argvars[0].v_type != VAR_DICT
18855 || (dict = argvars[0].vval.v_dict) == NULL)
18856 EMSG(_(e_invarg));
18857 else
18858 {
18859 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
18860 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
18861#ifdef FEAT_VIRTUALEDIT
18862 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
18863#endif
18864 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018865 curwin->w_set_curswant = FALSE;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018866
Bram Moolenaar6f11a412006-09-06 20:16:42 +000018867 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018868#ifdef FEAT_DIFF
18869 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
18870#endif
18871 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
18872 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
18873
18874 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020018875 win_new_height(curwin, curwin->w_height);
18876# ifdef FEAT_VERTSPLIT
18877 win_new_width(curwin, W_WIDTH(curwin));
18878# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020018879 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018880
18881 if (curwin->w_topline == 0)
18882 curwin->w_topline = 1;
18883 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
18884 curwin->w_topline = curbuf->b_ml.ml_line_count;
18885#ifdef FEAT_DIFF
18886 check_topfill(curwin, TRUE);
18887#endif
18888 }
18889}
18890
18891/*
18892 * "winsaveview()" function
18893 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018894 static void
18895f_winsaveview(argvars, rettv)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000018896 typval_T *argvars UNUSED;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018897 typval_T *rettv;
18898{
18899 dict_T *dict;
18900
Bram Moolenaara800b422010-06-27 01:15:55 +020018901 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018902 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020018903 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018904
18905 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
18906 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
18907#ifdef FEAT_VIRTUALEDIT
18908 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
18909#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000018910 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000018911 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
18912
18913 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
18914#ifdef FEAT_DIFF
18915 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
18916#endif
18917 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
18918 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
18919}
18920
18921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018922 * "winwidth(nr)" function
18923 */
18924 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018925f_winwidth(argvars, rettv)
Bram Moolenaar33570922005-01-25 22:26:29 +000018926 typval_T *argvars;
18927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018928{
18929 win_T *wp;
18930
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018931 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018933 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018934 else
18935#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018936 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018937#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018938 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018939#endif
18940}
18941
Bram Moolenaar071d4272004-06-13 20:20:40 +000018942/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018943 * "writefile()" function
18944 */
18945 static void
18946f_writefile(argvars, rettv)
18947 typval_T *argvars;
18948 typval_T *rettv;
18949{
18950 int binary = FALSE;
18951 char_u *fname;
18952 FILE *fd;
18953 listitem_T *li;
18954 char_u *s;
18955 int ret = 0;
18956 int c;
18957
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000018958 if (check_restricted() || check_secure())
18959 return;
18960
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000018961 if (argvars[0].v_type != VAR_LIST)
18962 {
18963 EMSG2(_(e_listarg), "writefile()");
18964 return;
18965 }
18966 if (argvars[0].vval.v_list == NULL)
18967 return;
18968
18969 if (argvars[2].v_type != VAR_UNKNOWN
18970 && STRCMP(get_tv_string(&argvars[2]), "b") == 0)
18971 binary = TRUE;
18972
18973 /* Always open the file in binary mode, library functions have a mind of
18974 * their own about CR-LF conversion. */
18975 fname = get_tv_string(&argvars[1]);
18976 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
18977 {
18978 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
18979 ret = -1;
18980 }
18981 else
18982 {
18983 for (li = argvars[0].vval.v_list->lv_first; li != NULL;
18984 li = li->li_next)
18985 {
18986 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
18987 {
18988 if (*s == '\n')
18989 c = putc(NUL, fd);
18990 else
18991 c = putc(*s, fd);
18992 if (c == EOF)
18993 {
18994 ret = -1;
18995 break;
18996 }
18997 }
18998 if (!binary || li->li_next != NULL)
18999 if (putc('\n', fd) == EOF)
19000 {
19001 ret = -1;
19002 break;
19003 }
19004 if (ret < 0)
19005 {
19006 EMSG(_(e_write));
19007 break;
19008 }
19009 }
19010 fclose(fd);
19011 }
19012
19013 rettv->vval.v_number = ret;
19014}
19015
19016/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010019017 * "xor(expr, expr)" function
19018 */
19019 static void
19020f_xor(argvars, rettv)
19021 typval_T *argvars;
19022 typval_T *rettv;
19023{
19024 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
19025 ^ get_tv_number_chk(&argvars[1], NULL);
19026}
19027
19028
19029/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019030 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019031 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019032 */
19033 static pos_T *
Bram Moolenaar477933c2007-07-17 14:32:23 +000019034var2fpos(varp, dollar_lnum, fnum)
Bram Moolenaar33570922005-01-25 22:26:29 +000019035 typval_T *varp;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019036 int dollar_lnum; /* TRUE when $ is last line */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019037 int *fnum; /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019038{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019039 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019040 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000019041 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042
Bram Moolenaara5525202006-03-02 22:52:09 +000019043 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019044 if (varp->v_type == VAR_LIST)
19045 {
19046 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019047 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000019048 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000019049 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019050
19051 l = varp->vval.v_list;
19052 if (l == NULL)
19053 return NULL;
19054
19055 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019056 pos.lnum = list_find_nr(l, 0L, &error);
19057 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019058 return NULL; /* invalid line number */
19059
19060 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019061 pos.col = list_find_nr(l, 1L, &error);
19062 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019063 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019064 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000019065
19066 /* We accept "$" for the column number: last column. */
19067 li = list_find(l, 1L);
19068 if (li != NULL && li->li_tv.v_type == VAR_STRING
19069 && li->li_tv.vval.v_string != NULL
19070 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
19071 pos.col = len + 1;
19072
Bram Moolenaara5525202006-03-02 22:52:09 +000019073 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000019074 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019075 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000019076 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019077
Bram Moolenaara5525202006-03-02 22:52:09 +000019078#ifdef FEAT_VIRTUALEDIT
19079 /* Get the virtual offset. Defaults to zero. */
19080 pos.coladd = list_find_nr(l, 2L, &error);
19081 if (error)
19082 pos.coladd = 0;
19083#endif
19084
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019085 return &pos;
19086 }
19087
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019088 name = get_tv_string_chk(varp);
19089 if (name == NULL)
19090 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019091 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000019093#ifdef FEAT_VISUAL
19094 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
19095 {
19096 if (VIsual_active)
19097 return &VIsual;
19098 return &curwin->w_cursor;
19099 }
19100#endif
19101 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010019103 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019104 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
19105 return NULL;
19106 return pp;
19107 }
Bram Moolenaara5525202006-03-02 22:52:09 +000019108
19109#ifdef FEAT_VIRTUALEDIT
19110 pos.coladd = 0;
19111#endif
19112
Bram Moolenaar477933c2007-07-17 14:32:23 +000019113 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019114 {
19115 pos.col = 0;
19116 if (name[1] == '0') /* "w0": first visible line */
19117 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019118 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019119 pos.lnum = curwin->w_topline;
19120 return &pos;
19121 }
19122 else if (name[1] == '$') /* "w$": last visible line */
19123 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000019124 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000019125 pos.lnum = curwin->w_botline - 1;
19126 return &pos;
19127 }
19128 }
19129 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000019131 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 {
19133 pos.lnum = curbuf->b_ml.ml_line_count;
19134 pos.col = 0;
19135 }
19136 else
19137 {
19138 pos.lnum = curwin->w_cursor.lnum;
19139 pos.col = (colnr_T)STRLEN(ml_get_curline());
19140 }
19141 return &pos;
19142 }
19143 return NULL;
19144}
19145
19146/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019147 * Convert list in "arg" into a position and optional file number.
19148 * When "fnump" is NULL there is no file number, only 3 items.
19149 * Note that the column is passed on as-is, the caller may want to decrement
19150 * it to use 1 for the first column.
19151 * Return FAIL when conversion is not possible, doesn't check the position for
19152 * validity.
19153 */
19154 static int
19155list2fpos(arg, posp, fnump)
19156 typval_T *arg;
19157 pos_T *posp;
19158 int *fnump;
19159{
19160 list_T *l = arg->vval.v_list;
19161 long i = 0;
19162 long n;
19163
Bram Moolenaarbde35262006-07-23 20:12:24 +000019164 /* List must be: [fnum, lnum, col, coladd], where "fnum" is only there
19165 * when "fnump" isn't NULL and "coladd" is optional. */
19166 if (arg->v_type != VAR_LIST
19167 || l == NULL
19168 || l->lv_len < (fnump == NULL ? 2 : 3)
19169 || l->lv_len > (fnump == NULL ? 3 : 4))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019170 return FAIL;
19171
19172 if (fnump != NULL)
19173 {
19174 n = list_find_nr(l, i++, NULL); /* fnum */
19175 if (n < 0)
19176 return FAIL;
19177 if (n == 0)
19178 n = curbuf->b_fnum; /* current buffer */
19179 *fnump = n;
19180 }
19181
19182 n = list_find_nr(l, i++, NULL); /* lnum */
19183 if (n < 0)
19184 return FAIL;
19185 posp->lnum = n;
19186
19187 n = list_find_nr(l, i++, NULL); /* col */
19188 if (n < 0)
19189 return FAIL;
19190 posp->col = n;
19191
19192#ifdef FEAT_VIRTUALEDIT
19193 n = list_find_nr(l, i, NULL);
19194 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000019195 posp->coladd = 0;
19196 else
19197 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000019198#endif
19199
19200 return OK;
19201}
19202
19203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019204 * Get the length of an environment variable name.
19205 * Advance "arg" to the first character after the name.
19206 * Return 0 for error.
19207 */
19208 static int
19209get_env_len(arg)
19210 char_u **arg;
19211{
19212 char_u *p;
19213 int len;
19214
19215 for (p = *arg; vim_isIDc(*p); ++p)
19216 ;
19217 if (p == *arg) /* no name found */
19218 return 0;
19219
19220 len = (int)(p - *arg);
19221 *arg = p;
19222 return len;
19223}
19224
19225/*
19226 * Get the length of the name of a function or internal variable.
19227 * "arg" is advanced to the first non-white character after the name.
19228 * Return 0 if something is wrong.
19229 */
19230 static int
19231get_id_len(arg)
19232 char_u **arg;
19233{
19234 char_u *p;
19235 int len;
19236
19237 /* Find the end of the name. */
19238 for (p = *arg; eval_isnamec(*p); ++p)
19239 ;
19240 if (p == *arg) /* no name found */
19241 return 0;
19242
19243 len = (int)(p - *arg);
19244 *arg = skipwhite(p);
19245
19246 return len;
19247}
19248
19249/*
Bram Moolenaara7043832005-01-21 11:56:39 +000019250 * Get the length of the name of a variable or function.
19251 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000019252 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019253 * Return -1 if curly braces expansion failed.
19254 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255 * If the name contains 'magic' {}'s, expand them and return the
19256 * expanded name in an allocated string via 'alias' - caller must free.
19257 */
19258 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019259get_name_len(arg, alias, evaluate, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019260 char_u **arg;
19261 char_u **alias;
19262 int evaluate;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019263 int verbose;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019264{
19265 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019266 char_u *p;
19267 char_u *expr_start;
19268 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019269
19270 *alias = NULL; /* default to no alias */
19271
19272 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
19273 && (*arg)[2] == (int)KE_SNR)
19274 {
19275 /* hard coded <SNR>, already translated */
19276 *arg += 3;
19277 return get_id_len(arg) + 3;
19278 }
19279 len = eval_fname_script(*arg);
19280 if (len > 0)
19281 {
19282 /* literal "<SID>", "s:" or "<SNR>" */
19283 *arg += len;
19284 }
19285
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019287 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019288 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019289 p = find_name_end(*arg, &expr_start, &expr_end,
19290 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019291 if (expr_start != NULL)
19292 {
19293 char_u *temp_string;
19294
19295 if (!evaluate)
19296 {
19297 len += (int)(p - *arg);
19298 *arg = skipwhite(p);
19299 return len;
19300 }
19301
19302 /*
19303 * Include any <SID> etc in the expanded string:
19304 * Thus the -len here.
19305 */
19306 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
19307 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019308 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309 *alias = temp_string;
19310 *arg = skipwhite(p);
19311 return (int)STRLEN(temp_string);
19312 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313
19314 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019315 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019316 EMSG2(_(e_invexpr2), *arg);
19317
19318 return len;
19319}
19320
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019321/*
19322 * Find the end of a variable or function name, taking care of magic braces.
19323 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
19324 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019325 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019326 * Return a pointer to just after the name. Equal to "arg" if there is no
19327 * valid name.
19328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 static char_u *
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019330find_name_end(arg, expr_start, expr_end, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331 char_u *arg;
19332 char_u **expr_start;
19333 char_u **expr_end;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019334 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019335{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019336 int mb_nest = 0;
19337 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019338 char_u *p;
19339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019340 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019341 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019342 *expr_start = NULL;
19343 *expr_end = NULL;
19344 }
19345
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019346 /* Quick check for valid starting character. */
19347 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
19348 return arg;
19349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019350 for (p = arg; *p != NUL
19351 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019352 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019353 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019354 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000019355 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019356 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000019357 if (*p == '\'')
19358 {
19359 /* skip over 'string' to avoid counting [ and ] inside it. */
19360 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
19361 ;
19362 if (*p == NUL)
19363 break;
19364 }
19365 else if (*p == '"')
19366 {
19367 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
19368 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
19369 if (*p == '\\' && p[1] != NUL)
19370 ++p;
19371 if (*p == NUL)
19372 break;
19373 }
19374
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019375 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019377 if (*p == '[')
19378 ++br_nest;
19379 else if (*p == ']')
19380 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019381 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000019382
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019383 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019384 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019385 if (*p == '{')
19386 {
19387 mb_nest++;
19388 if (expr_start != NULL && *expr_start == NULL)
19389 *expr_start = p;
19390 }
19391 else if (*p == '}')
19392 {
19393 mb_nest--;
19394 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
19395 *expr_end = p;
19396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019398 }
19399
19400 return p;
19401}
19402
19403/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019404 * Expands out the 'magic' {}'s in a variable/function name.
19405 * Note that this can call itself recursively, to deal with
19406 * constructs like foo{bar}{baz}{bam}
19407 * The four pointer arguments point to "foo{expre}ss{ion}bar"
19408 * "in_start" ^
19409 * "expr_start" ^
19410 * "expr_end" ^
19411 * "in_end" ^
19412 *
19413 * Returns a new allocated string, which the caller must free.
19414 * Returns NULL for failure.
19415 */
19416 static char_u *
19417make_expanded_name(in_start, expr_start, expr_end, in_end)
19418 char_u *in_start;
19419 char_u *expr_start;
19420 char_u *expr_end;
19421 char_u *in_end;
19422{
19423 char_u c1;
19424 char_u *retval = NULL;
19425 char_u *temp_result;
19426 char_u *nextcmd = NULL;
19427
19428 if (expr_end == NULL || in_end == NULL)
19429 return NULL;
19430 *expr_start = NUL;
19431 *expr_end = NUL;
19432 c1 = *in_end;
19433 *in_end = NUL;
19434
Bram Moolenaar362e1a32006-03-06 23:29:24 +000019435 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019436 if (temp_result != NULL && nextcmd == NULL)
19437 {
19438 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
19439 + (in_end - expr_end) + 1));
19440 if (retval != NULL)
19441 {
19442 STRCPY(retval, in_start);
19443 STRCAT(retval, temp_result);
19444 STRCAT(retval, expr_end + 1);
19445 }
19446 }
19447 vim_free(temp_result);
19448
19449 *in_end = c1; /* put char back for error messages */
19450 *expr_start = '{';
19451 *expr_end = '}';
19452
19453 if (retval != NULL)
19454 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019455 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019456 if (expr_start != NULL)
19457 {
19458 /* Further expansion! */
19459 temp_result = make_expanded_name(retval, expr_start,
19460 expr_end, temp_result);
19461 vim_free(retval);
19462 retval = temp_result;
19463 }
19464 }
19465
19466 return retval;
19467}
19468
19469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019470 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000019471 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 */
19473 static int
19474eval_isnamec(c)
19475 int c;
19476{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000019477 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
19478}
19479
19480/*
19481 * Return TRUE if character "c" can be used as the first character in a
19482 * variable or function name (excluding '{' and '}').
19483 */
19484 static int
19485eval_isnamec1(c)
19486 int c;
19487{
19488 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489}
19490
19491/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019492 * Set number v: variable to "val".
19493 */
19494 void
19495set_vim_var_nr(idx, val)
19496 int idx;
19497 long val;
19498{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019499 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019500}
19501
19502/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019503 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504 */
19505 long
19506get_vim_var_nr(idx)
19507 int idx;
19508{
Bram Moolenaare9a41262005-01-15 22:18:47 +000019509 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510}
19511
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019512/*
19513 * Get string v: variable value. Uses a static buffer, can only be used once.
19514 */
19515 char_u *
19516get_vim_var_str(idx)
19517 int idx;
19518{
19519 return get_tv_string(&vimvars[idx].vv_tv);
19520}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000019521
Bram Moolenaar071d4272004-06-13 20:20:40 +000019522/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019523 * Get List v: variable value. Caller must take care of reference count when
19524 * needed.
19525 */
19526 list_T *
19527get_vim_var_list(idx)
19528 int idx;
19529{
19530 return vimvars[idx].vv_list;
19531}
19532
19533/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019534 * Set v:char to character "c".
19535 */
19536 void
19537set_vim_var_char(c)
19538 int c;
19539{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020019540 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000019541
19542#ifdef FEAT_MBYTE
19543 if (has_mbyte)
19544 buf[(*mb_char2bytes)(c, buf)] = NUL;
19545 else
19546#endif
19547 {
19548 buf[0] = c;
19549 buf[1] = NUL;
19550 }
19551 set_vim_var_string(VV_CHAR, buf, -1);
19552}
19553
19554/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019555 * Set v:count to "count" and v:count1 to "count1".
19556 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557 */
19558 void
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019559set_vcount(count, count1, set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 long count;
19561 long count1;
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019562 int set_prevcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000019564 if (set_prevcount)
19565 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000019566 vimvars[VV_COUNT].vv_nr = count;
19567 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568}
19569
19570/*
19571 * Set string v: variable to a copy of "val".
19572 */
19573 void
19574set_vim_var_string(idx, val, len)
19575 int idx;
19576 char_u *val;
19577 int len; /* length of "val" to use or -1 (whole string) */
19578{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019579 /* Need to do this (at least) once, since we can't initialize a union.
19580 * Will always be invoked when "v:progname" is set. */
19581 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
19582
Bram Moolenaare9a41262005-01-15 22:18:47 +000019583 vim_free(vimvars[idx].vv_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019585 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019586 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019587 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019588 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000019589 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019590}
19591
19592/*
Bram Moolenaard812df62008-11-09 12:46:09 +000019593 * Set List v: variable to "val".
19594 */
19595 void
19596set_vim_var_list(idx, val)
19597 int idx;
19598 list_T *val;
19599{
19600 list_unref(vimvars[idx].vv_list);
19601 vimvars[idx].vv_list = val;
19602 if (val != NULL)
19603 ++val->lv_refcount;
19604}
19605
19606/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 * Set v:register if needed.
19608 */
19609 void
19610set_reg_var(c)
19611 int c;
19612{
19613 char_u regname;
19614
19615 if (c == 0 || c == ' ')
19616 regname = '"';
19617 else
19618 regname = c;
19619 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000019620 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 set_vim_var_string(VV_REG, &regname, 1);
19622}
19623
19624/*
19625 * Get or set v:exception. If "oldval" == NULL, return the current value.
19626 * Otherwise, restore the value to "oldval" and return NULL.
19627 * Must always be called in pairs to save and restore v:exception! Does not
19628 * take care of memory allocations.
19629 */
19630 char_u *
19631v_exception(oldval)
19632 char_u *oldval;
19633{
19634 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019635 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019636
Bram Moolenaare9a41262005-01-15 22:18:47 +000019637 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019638 return NULL;
19639}
19640
19641/*
19642 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
19643 * Otherwise, restore the value to "oldval" and return NULL.
19644 * Must always be called in pairs to save and restore v:throwpoint! Does not
19645 * take care of memory allocations.
19646 */
19647 char_u *
19648v_throwpoint(oldval)
19649 char_u *oldval;
19650{
19651 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019652 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653
Bram Moolenaare9a41262005-01-15 22:18:47 +000019654 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655 return NULL;
19656}
19657
19658#if defined(FEAT_AUTOCMD) || defined(PROTO)
19659/*
19660 * Set v:cmdarg.
19661 * If "eap" != NULL, use "eap" to generate the value and return the old value.
19662 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
19663 * Must always be called in pairs!
19664 */
19665 char_u *
19666set_cmdarg(eap, oldarg)
19667 exarg_T *eap;
19668 char_u *oldarg;
19669{
19670 char_u *oldval;
19671 char_u *newval;
19672 unsigned len;
19673
Bram Moolenaare9a41262005-01-15 22:18:47 +000019674 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019675 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019676 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019677 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000019678 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019679 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680 }
19681
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019682 if (eap->force_bin == FORCE_BIN)
19683 len = 6;
19684 else if (eap->force_bin == FORCE_NOBIN)
19685 len = 8;
19686 else
19687 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019688
19689 if (eap->read_edit)
19690 len += 7;
19691
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019692 if (eap->force_ff != 0)
19693 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
19694# ifdef FEAT_MBYTE
19695 if (eap->force_enc != 0)
19696 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019697 if (eap->bad_char != 0)
19698 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019699# endif
19700
19701 newval = alloc(len + 1);
19702 if (newval == NULL)
19703 return NULL;
19704
19705 if (eap->force_bin == FORCE_BIN)
19706 sprintf((char *)newval, " ++bin");
19707 else if (eap->force_bin == FORCE_NOBIN)
19708 sprintf((char *)newval, " ++nobin");
19709 else
19710 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000019711
19712 if (eap->read_edit)
19713 STRCAT(newval, " ++edit");
19714
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019715 if (eap->force_ff != 0)
19716 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
19717 eap->cmd + eap->force_ff);
19718# ifdef FEAT_MBYTE
19719 if (eap->force_enc != 0)
19720 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
19721 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020019722 if (eap->bad_char == BAD_KEEP)
19723 STRCPY(newval + STRLEN(newval), " ++bad=keep");
19724 else if (eap->bad_char == BAD_DROP)
19725 STRCPY(newval + STRLEN(newval), " ++bad=drop");
19726 else if (eap->bad_char != 0)
19727 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019728# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000019729 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000019730 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731}
19732#endif
19733
19734/*
19735 * Get the value of internal variable "name".
19736 * Return OK or FAIL.
19737 */
19738 static int
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019739get_var_tv(name, len, rettv, verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740 char_u *name;
19741 int len; /* length of "name" */
Bram Moolenaar33570922005-01-25 22:26:29 +000019742 typval_T *rettv; /* NULL when only checking existence */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019743 int verbose; /* may give error message */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019744{
19745 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000019746 typval_T *tv = NULL;
19747 typval_T atv;
19748 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019749 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019750
19751 /* truncate the name, so that we can use strcmp() */
19752 cc = name[len];
19753 name[len] = NUL;
19754
19755 /*
19756 * Check for "b:changedtick".
19757 */
19758 if (STRCMP(name, "b:changedtick") == 0)
19759 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000019760 atv.v_type = VAR_NUMBER;
19761 atv.vval.v_number = curbuf->b_changedtick;
19762 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019763 }
19764
19765 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019766 * Check for user-defined variables.
19767 */
19768 else
19769 {
Bram Moolenaara7043832005-01-21 11:56:39 +000019770 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019771 if (v != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019772 tv = &v->di_tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019773 }
19774
Bram Moolenaare9a41262005-01-15 22:18:47 +000019775 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019776 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019777 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019778 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019779 ret = FAIL;
19780 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019781 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000019782 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019783
19784 name[len] = cc;
19785
19786 return ret;
19787}
19788
19789/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019790 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
19791 * Also handle function call with Funcref variable: func(expr)
19792 * Can all be combined: dict.func(expr)[idx]['func'](expr)
19793 */
19794 static int
19795handle_subscript(arg, rettv, evaluate, verbose)
19796 char_u **arg;
19797 typval_T *rettv;
19798 int evaluate; /* do more than finding the end */
19799 int verbose; /* give error messages */
19800{
19801 int ret = OK;
19802 dict_T *selfdict = NULL;
19803 char_u *s;
19804 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000019805 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019806
19807 while (ret == OK
19808 && (**arg == '['
19809 || (**arg == '.' && rettv->v_type == VAR_DICT)
19810 || (**arg == '(' && rettv->v_type == VAR_FUNC))
19811 && !vim_iswhite(*(*arg - 1)))
19812 {
19813 if (**arg == '(')
19814 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000019815 /* need to copy the funcref so that we can clear rettv */
19816 functv = *rettv;
19817 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019818
19819 /* Invoke the function. Recursive! */
Bram Moolenaard9fba312005-06-26 22:34:35 +000019820 s = functv.vval.v_string;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000019821 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000019822 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
19823 &len, evaluate, selfdict);
19824
19825 /* Clear the funcref afterwards, so that deleting it while
19826 * evaluating the arguments is possible (see test55). */
19827 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000019828
19829 /* Stop the expression evaluation when immediately aborting on
19830 * error, or when an interrupt occurred or an exception was thrown
19831 * but not caught. */
19832 if (aborting())
19833 {
19834 if (ret == OK)
19835 clear_tv(rettv);
19836 ret = FAIL;
19837 }
19838 dict_unref(selfdict);
19839 selfdict = NULL;
19840 }
19841 else /* **arg == '[' || **arg == '.' */
19842 {
19843 dict_unref(selfdict);
19844 if (rettv->v_type == VAR_DICT)
19845 {
19846 selfdict = rettv->vval.v_dict;
19847 if (selfdict != NULL)
19848 ++selfdict->dv_refcount;
19849 }
19850 else
19851 selfdict = NULL;
19852 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
19853 {
19854 clear_tv(rettv);
19855 ret = FAIL;
19856 }
19857 }
19858 }
19859 dict_unref(selfdict);
19860 return ret;
19861}
19862
19863/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019864 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019865 * value).
19866 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019867 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019868alloc_tv()
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019869{
Bram Moolenaar33570922005-01-25 22:26:29 +000019870 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019871}
19872
19873/*
19874 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019875 * The string "s" must have been allocated, it is consumed.
19876 * Return NULL for out of memory, the variable otherwise.
19877 */
Bram Moolenaar33570922005-01-25 22:26:29 +000019878 static typval_T *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019879alloc_string_tv(s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019880 char_u *s;
19881{
Bram Moolenaar33570922005-01-25 22:26:29 +000019882 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019884 rettv = alloc_tv();
19885 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019886 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019887 rettv->v_type = VAR_STRING;
19888 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019889 }
19890 else
19891 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019892 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893}
19894
19895/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019896 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019897 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000019898 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019899free_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019900 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901{
19902 if (varp != NULL)
19903 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019904 switch (varp->v_type)
19905 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019906 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019907 func_unref(varp->vval.v_string);
19908 /*FALLTHROUGH*/
19909 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019910 vim_free(varp->vval.v_string);
19911 break;
19912 case VAR_LIST:
19913 list_unref(varp->vval.v_list);
19914 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019915 case VAR_DICT:
19916 dict_unref(varp->vval.v_dict);
19917 break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000019918 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019919#ifdef FEAT_FLOAT
19920 case VAR_FLOAT:
19921#endif
Bram Moolenaar758711c2005-02-02 23:11:38 +000019922 case VAR_UNKNOWN:
19923 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019924 default:
Bram Moolenaar758711c2005-02-02 23:11:38 +000019925 EMSG2(_(e_intern2), "free_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019926 break;
19927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019928 vim_free(varp);
19929 }
19930}
19931
19932/*
19933 * Free the memory for a variable value and set the value to NULL or 0.
19934 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000019935 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019936clear_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019937 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019938{
19939 if (varp != NULL)
19940 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019941 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019942 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019943 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000019944 func_unref(varp->vval.v_string);
19945 /*FALLTHROUGH*/
19946 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019947 vim_free(varp->vval.v_string);
19948 varp->vval.v_string = NULL;
19949 break;
19950 case VAR_LIST:
19951 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019952 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019953 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019954 case VAR_DICT:
19955 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000019956 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000019957 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019958 case VAR_NUMBER:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019959 varp->vval.v_number = 0;
19960 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019961#ifdef FEAT_FLOAT
19962 case VAR_FLOAT:
19963 varp->vval.v_float = 0.0;
19964 break;
19965#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019966 case VAR_UNKNOWN:
19967 break;
19968 default:
19969 EMSG2(_(e_intern2), "clear_tv()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019971 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019972 }
19973}
19974
19975/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019976 * Set the value of a variable to NULL without freeing items.
19977 */
19978 static void
19979init_tv(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019980 typval_T *varp;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019981{
19982 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000019983 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019984}
19985
19986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987 * Get the number value of a variable.
19988 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019989 * For incompatible types, return 0.
19990 * get_tv_number_chk() is similar to get_tv_number(), but informs the
19991 * caller of incompatible types: it sets *denote to TRUE if "denote"
19992 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 */
19994 static long
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019995get_tv_number(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000019996 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019997{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019998 int error = FALSE;
19999
20000 return get_tv_number_chk(varp, &error); /* return 0L on error */
20001}
20002
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020003 long
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020004get_tv_number_chk(varp, denote)
20005 typval_T *varp;
20006 int *denote;
20007{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020008 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020009
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020010 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020012 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020013 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020014#ifdef FEAT_FLOAT
20015 case VAR_FLOAT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020016 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020017 break;
20018#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020019 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020020 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020021 break;
20022 case VAR_STRING:
20023 if (varp->vval.v_string != NULL)
20024 vim_str2nr(varp->vval.v_string, NULL, NULL,
20025 TRUE, TRUE, &n, NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020026 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020027 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020028 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000020029 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020030 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000020031 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020032 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020033 default:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020034 EMSG2(_(e_intern2), "get_tv_number()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020035 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020037 if (denote == NULL) /* useful for values that must be unsigned */
20038 n = -1;
20039 else
20040 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020041 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020042}
20043
20044/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020045 * Get the lnum from the first argument.
20046 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020047 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020048 */
20049 static linenr_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020050get_tv_lnum(argvars)
Bram Moolenaar33570922005-01-25 22:26:29 +000020051 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020052{
Bram Moolenaar33570922005-01-25 22:26:29 +000020053 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020054 linenr_T lnum;
20055
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020056 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020057 if (lnum == 0) /* no valid number, try using line() */
20058 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020059 rettv.v_type = VAR_NUMBER;
20060 f_line(argvars, &rettv);
20061 lnum = rettv.vval.v_number;
20062 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020063 }
20064 return lnum;
20065}
20066
20067/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000020068 * Get the lnum from the first argument.
20069 * Also accepts "$", then "buf" is used.
20070 * Returns 0 on error.
20071 */
20072 static linenr_T
20073get_tv_lnum_buf(argvars, buf)
20074 typval_T *argvars;
20075 buf_T *buf;
20076{
20077 if (argvars[0].v_type == VAR_STRING
20078 && argvars[0].vval.v_string != NULL
20079 && argvars[0].vval.v_string[0] == '$'
20080 && buf != NULL)
20081 return buf->b_ml.ml_line_count;
20082 return get_tv_number_chk(&argvars[0], NULL);
20083}
20084
20085/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020086 * Get the string value of a variable.
20087 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000020088 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
20089 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020090 * If the String variable has never been set, return an empty string.
20091 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020092 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
20093 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020094 */
20095 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020096get_tv_string(varp)
Bram Moolenaar33570922005-01-25 22:26:29 +000020097 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020098{
20099 static char_u mybuf[NUMBUFLEN];
20100
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020101 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020102}
20103
20104 static char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020105get_tv_string_buf(varp, buf)
Bram Moolenaar33570922005-01-25 22:26:29 +000020106 typval_T *varp;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020107 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020108{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020109 char_u *res = get_tv_string_buf_chk(varp, buf);
20110
20111 return res != NULL ? res : (char_u *)"";
20112}
20113
Bram Moolenaar4be06f92005-07-29 22:36:03 +000020114 char_u *
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020115get_tv_string_chk(varp)
20116 typval_T *varp;
20117{
20118 static char_u mybuf[NUMBUFLEN];
20119
20120 return get_tv_string_buf_chk(varp, mybuf);
20121}
20122
20123 static char_u *
20124get_tv_string_buf_chk(varp, buf)
20125 typval_T *varp;
20126 char_u *buf;
20127{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020128 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020129 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020130 case VAR_NUMBER:
20131 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
20132 return buf;
20133 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020134 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020135 break;
20136 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020137 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000020138 break;
20139 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020140 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020141 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020142#ifdef FEAT_FLOAT
20143 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +020020144 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020145 break;
20146#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020147 case VAR_STRING:
20148 if (varp->vval.v_string != NULL)
20149 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020150 return (char_u *)"";
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020151 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020152 EMSG2(_(e_intern2), "get_tv_string_buf()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020153 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020154 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020155 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020156}
20157
20158/*
20159 * Find variable "name" in the list of variables.
20160 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020161 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020162 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000020163 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020164 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020165 static dictitem_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020166find_var(name, htp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020167 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020168 hashtab_T **htp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020169{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020171 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020172
Bram Moolenaara7043832005-01-21 11:56:39 +000020173 ht = find_var_ht(name, &varname);
20174 if (htp != NULL)
20175 *htp = ht;
20176 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177 return NULL;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020178 return find_var_in_ht(ht, *name, varname, htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179}
20180
20181/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020020182 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000020183 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020184 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020185 static dictitem_T *
Bram Moolenaar332ac062013-04-15 13:06:21 +020020186find_var_in_ht(ht, htname, varname, writing)
Bram Moolenaar33570922005-01-25 22:26:29 +000020187 hashtab_T *ht;
Bram Moolenaar332ac062013-04-15 13:06:21 +020020188 int htname;
Bram Moolenaara7043832005-01-21 11:56:39 +000020189 char_u *varname;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020190 int writing;
Bram Moolenaara7043832005-01-21 11:56:39 +000020191{
Bram Moolenaar33570922005-01-25 22:26:29 +000020192 hashitem_T *hi;
20193
20194 if (*varname == NUL)
20195 {
20196 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020020197 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000020198 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020199 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020200 case 'g': return &globvars_var;
20201 case 'v': return &vimvars_var;
20202 case 'b': return &curbuf->b_bufvar;
20203 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020204#ifdef FEAT_WINDOWS
20205 case 't': return &curtab->tp_winvar;
20206#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020207 case 'l': return current_funccal == NULL
20208 ? NULL : &current_funccal->l_vars_var;
20209 case 'a': return current_funccal == NULL
20210 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000020211 }
20212 return NULL;
20213 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020214
20215 hi = hash_find(ht, varname);
20216 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020217 {
20218 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000020219 * worked find the variable again. Don't auto-load a script if it was
20220 * loaded already, otherwise it would be loaded every time when
20221 * checking if a function name is a Funcref variable. */
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020222 if (ht == &globvarht && !writing)
20223 {
20224 /* Note: script_autoload() may make "hi" invalid. It must either
20225 * be obtained again or not used. */
20226 if (!script_autoload(varname, FALSE) || aborting())
20227 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020228 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010020229 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020230 if (HASHITEM_EMPTY(hi))
20231 return NULL;
20232 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020233 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020234}
20235
20236/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020237 * Find the hashtab used for a variable name.
Bram Moolenaara7043832005-01-21 11:56:39 +000020238 * Set "varname" to the start of name without ':'.
20239 */
Bram Moolenaar33570922005-01-25 22:26:29 +000020240 static hashtab_T *
Bram Moolenaara7043832005-01-21 11:56:39 +000020241find_var_ht(name, varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242 char_u *name;
20243 char_u **varname;
20244{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020245 hashitem_T *hi;
20246
Bram Moolenaar071d4272004-06-13 20:20:40 +000020247 if (name[1] != ':')
20248 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020249 /* The name must not start with a colon or #. */
20250 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020251 return NULL;
20252 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000020253
20254 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000020255 hi = hash_find(&compat_hashtab, name);
20256 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000020257 return &compat_hashtab;
20258
Bram Moolenaar071d4272004-06-13 20:20:40 +000020259 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000020260 return &globvarht; /* global variable */
20261 return &current_funccal->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020262 }
20263 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020264 if (*name == 'g') /* global variable */
20265 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000020266 /* There must be no ':' or '#' in the rest of the name, unless g: is used
20267 */
20268 if (vim_strchr(name + 2, ':') != NULL
20269 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000020270 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020272 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020273 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020274 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020275#ifdef FEAT_WINDOWS
20276 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020020277 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000020278#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000020279 if (*name == 'v') /* v: variable */
20280 return &vimvarht;
20281 if (*name == 'a' && current_funccal != NULL) /* function argument */
20282 return &current_funccal->l_avars.dv_hashtab;
20283 if (*name == 'l' && current_funccal != NULL) /* local function variable */
20284 return &current_funccal->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 if (*name == 's' /* script variable */
20286 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
20287 return &SCRIPT_VARS(current_SID);
20288 return NULL;
20289}
20290
20291/*
20292 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020020293 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020294 * Returns NULL when it doesn't exist.
20295 */
20296 char_u *
20297get_var_value(name)
20298 char_u *name;
20299{
Bram Moolenaar33570922005-01-25 22:26:29 +000020300 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020301
Bram Moolenaara7043832005-01-21 11:56:39 +000020302 v = find_var(name, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020303 if (v == NULL)
20304 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000020305 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306}
20307
20308/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020309 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 * sourcing this script and when executing functions defined in the script.
20311 */
20312 void
20313new_script_vars(id)
20314 scid_T id;
20315{
Bram Moolenaara7043832005-01-21 11:56:39 +000020316 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000020317 hashtab_T *ht;
20318 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000020319
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
20321 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020322 /* Re-allocating ga_data means that an ht_array pointing to
20323 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000020324 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000020325 for (i = 1; i <= ga_scripts.ga_len; ++i)
20326 {
20327 ht = &SCRIPT_VARS(i);
20328 if (ht->ht_mask == HT_INIT_SIZE - 1)
20329 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020330 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000020331 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000020332 }
20333
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334 while (ga_scripts.ga_len < id)
20335 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020020336 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020020337 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020338 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020339 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 }
20341 }
20342}
20343
20344/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020345 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
20346 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020347 */
20348 void
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020349init_var_dict(dict, dict_var, scope)
Bram Moolenaar33570922005-01-25 22:26:29 +000020350 dict_T *dict;
20351 dictitem_T *dict_var;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020352 int scope;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020353{
Bram Moolenaar33570922005-01-25 22:26:29 +000020354 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020020355 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020020356 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020357 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000020358 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000020359 dict_var->di_tv.vval.v_dict = dict;
20360 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020361 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000020362 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
20363 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020364}
20365
20366/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020020367 * Unreference a dictionary initialized by init_var_dict().
20368 */
20369 void
20370unref_var_dict(dict)
20371 dict_T *dict;
20372{
20373 /* Now the dict needs to be freed if no one else is using it, go back to
20374 * normal reference counting. */
20375 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
20376 dict_unref(dict);
20377}
20378
20379/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020380 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000020381 * Frees all allocated variables and the value they contain.
20382 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020383 */
20384 void
Bram Moolenaara7043832005-01-21 11:56:39 +000020385vars_clear(ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000020386 hashtab_T *ht;
20387{
20388 vars_clear_ext(ht, TRUE);
20389}
20390
20391/*
20392 * Like vars_clear(), but only free the value if "free_val" is TRUE.
20393 */
20394 static void
20395vars_clear_ext(ht, free_val)
20396 hashtab_T *ht;
20397 int free_val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398{
Bram Moolenaara7043832005-01-21 11:56:39 +000020399 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000020400 hashitem_T *hi;
20401 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020402
Bram Moolenaar33570922005-01-25 22:26:29 +000020403 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000020404 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000020405 for (hi = ht->ht_array; todo > 0; ++hi)
20406 {
20407 if (!HASHITEM_EMPTY(hi))
20408 {
20409 --todo;
20410
Bram Moolenaar33570922005-01-25 22:26:29 +000020411 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000020412 * ht_array might change then. hash_clear() takes care of it
20413 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000020414 v = HI2DI(hi);
20415 if (free_val)
20416 clear_tv(&v->di_tv);
20417 if ((v->di_flags & DI_FLAGS_FIX) == 0)
20418 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000020419 }
20420 }
20421 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000020422 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020423}
20424
Bram Moolenaara7043832005-01-21 11:56:39 +000020425/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020426 * Delete a variable from hashtab "ht" at item "hi".
20427 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000020428 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020429 static void
Bram Moolenaara7043832005-01-21 11:56:39 +000020430delete_var(ht, hi)
Bram Moolenaar33570922005-01-25 22:26:29 +000020431 hashtab_T *ht;
20432 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020433{
Bram Moolenaar33570922005-01-25 22:26:29 +000020434 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000020435
20436 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000020437 clear_tv(&di->di_tv);
20438 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020439}
20440
20441/*
20442 * List the value of one internal variable.
20443 */
20444 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020445list_one_var(v, prefix, first)
Bram Moolenaar33570922005-01-25 22:26:29 +000020446 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020447 char_u *prefix;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020448 int *first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020449{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020450 char_u *tofree;
20451 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020452 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020453
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020454 current_copyID += COPYID_INC;
20455 s = echo_string(&v->di_tv, &tofree, numbuf, current_copyID);
Bram Moolenaar33570922005-01-25 22:26:29 +000020456 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020457 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020458 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020459}
20460
Bram Moolenaar071d4272004-06-13 20:20:40 +000020461 static void
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020462list_one_var_a(prefix, name, type, string, first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020463 char_u *prefix;
20464 char_u *name;
20465 int type;
20466 char_u *string;
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020467 int *first; /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468{
Bram Moolenaar31859182007-08-14 20:41:13 +000020469 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
20470 msg_start();
20471 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020472 if (name != NULL) /* "a:" vars don't have a name stored */
20473 msg_puts(name);
20474 msg_putchar(' ');
20475 msg_advance(22);
20476 if (type == VAR_NUMBER)
20477 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020478 else if (type == VAR_FUNC)
20479 msg_putchar('*');
20480 else if (type == VAR_LIST)
20481 {
20482 msg_putchar('[');
20483 if (*string == '[')
20484 ++string;
20485 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000020486 else if (type == VAR_DICT)
20487 {
20488 msg_putchar('{');
20489 if (*string == '{')
20490 ++string;
20491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020492 else
20493 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020494
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020496
20497 if (type == VAR_FUNC)
20498 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000020499 if (*first)
20500 {
20501 msg_clr_eos();
20502 *first = FALSE;
20503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020504}
20505
20506/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020507 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000020508 * If the variable already exists, the value is updated.
20509 * Otherwise the variable is created.
20510 */
20511 static void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020512set_var(name, tv, copy)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000020514 typval_T *tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020515 int copy; /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020516{
Bram Moolenaar33570922005-01-25 22:26:29 +000020517 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000020519 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020521 ht = find_var_ht(name, &varname);
20522 if (ht == NULL || *varname == NUL)
20523 {
20524 EMSG2(_(e_illvar), name);
20525 return;
20526 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020020527 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010020528
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020529 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
20530 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020531
Bram Moolenaar33570922005-01-25 22:26:29 +000020532 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020534 /* existing variable, need to clear the value */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020535 if (var_check_ro(v->di_flags, name)
20536 || tv_check_lock(v->di_tv.v_lock, name))
Bram Moolenaar33570922005-01-25 22:26:29 +000020537 return;
20538 if (v->di_tv.v_type != tv->v_type
20539 && !((v->di_tv.v_type == VAR_STRING
20540 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020541 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020542 || tv->v_type == VAR_NUMBER))
20543#ifdef FEAT_FLOAT
20544 && !((v->di_tv.v_type == VAR_NUMBER
20545 || v->di_tv.v_type == VAR_FLOAT)
20546 && (tv->v_type == VAR_NUMBER
20547 || tv->v_type == VAR_FLOAT))
20548#endif
20549 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020550 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000020551 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020552 return;
20553 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020554
20555 /*
Bram Moolenaar758711c2005-02-02 23:11:38 +000020556 * Handle setting internal v: variables separately: we don't change
20557 * the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000020558 */
20559 if (ht == &vimvarht)
20560 {
20561 if (v->di_tv.v_type == VAR_STRING)
20562 {
20563 vim_free(v->di_tv.vval.v_string);
20564 if (copy || tv->v_type != VAR_STRING)
20565 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
20566 else
20567 {
20568 /* Take over the string to avoid an extra alloc/free. */
20569 v->di_tv.vval.v_string = tv->vval.v_string;
20570 tv->vval.v_string = NULL;
20571 }
20572 }
20573 else if (v->di_tv.v_type != VAR_NUMBER)
20574 EMSG2(_(e_intern2), "set_var()");
20575 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020576 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020577 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020578 if (STRCMP(varname, "searchforward") == 0)
20579 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
20580 }
Bram Moolenaar33570922005-01-25 22:26:29 +000020581 return;
20582 }
20583
20584 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020585 }
20586 else /* add a new variable */
20587 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000020588 /* Can't add "v:" variable. */
20589 if (ht == &vimvarht)
20590 {
20591 EMSG2(_(e_illvar), name);
20592 return;
20593 }
20594
Bram Moolenaar92124a32005-06-17 22:03:40 +000020595 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020596 if (!valid_varname(varname))
20597 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000020598
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020599 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
20600 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000020601 if (v == NULL)
20602 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000020603 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000020604 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605 {
Bram Moolenaara7043832005-01-21 11:56:39 +000020606 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607 return;
20608 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020609 v->di_flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020610 }
Bram Moolenaara7043832005-01-21 11:56:39 +000020611
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020612 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000020613 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020614 else
20615 {
Bram Moolenaar33570922005-01-25 22:26:29 +000020616 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020617 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020618 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000020619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620}
20621
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020622/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020623 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000020624 * Also give an error message.
20625 */
20626 static int
20627var_check_ro(flags, name)
20628 int flags;
20629 char_u *name;
20630{
20631 if (flags & DI_FLAGS_RO)
20632 {
20633 EMSG2(_(e_readonlyvar), name);
20634 return TRUE;
20635 }
20636 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
20637 {
20638 EMSG2(_(e_readonlysbx), name);
20639 return TRUE;
20640 }
20641 return FALSE;
20642}
20643
20644/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000020645 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
20646 * Also give an error message.
20647 */
20648 static int
20649var_check_fixed(flags, name)
20650 int flags;
20651 char_u *name;
20652{
20653 if (flags & DI_FLAGS_FIX)
20654 {
20655 EMSG2(_("E795: Cannot delete variable %s"), name);
20656 return TRUE;
20657 }
20658 return FALSE;
20659}
20660
20661/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020020662 * Check if a funcref is assigned to a valid variable name.
20663 * Return TRUE and give an error if not.
20664 */
20665 static int
20666var_check_func_name(name, new_var)
20667 char_u *name; /* points to start of variable name */
20668 int new_var; /* TRUE when creating the variable */
20669{
20670 if (!(vim_strchr((char_u *)"wbs", name[0]) != NULL && name[1] == ':')
20671 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
20672 ? name[2] : name[0]))
20673 {
20674 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
20675 name);
20676 return TRUE;
20677 }
20678 /* Don't allow hiding a function. When "v" is not NULL we might be
20679 * assigning another function to the same var, the type is checked
20680 * below. */
20681 if (new_var && function_exists(name))
20682 {
20683 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
20684 name);
20685 return TRUE;
20686 }
20687 return FALSE;
20688}
20689
20690/*
20691 * Check if a variable name is valid.
20692 * Return FALSE and give an error if not.
20693 */
20694 static int
20695valid_varname(varname)
20696 char_u *varname;
20697{
20698 char_u *p;
20699
20700 for (p = varname; *p != NUL; ++p)
20701 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
20702 && *p != AUTOLOAD_CHAR)
20703 {
20704 EMSG2(_(e_illvar), varname);
20705 return FALSE;
20706 }
20707 return TRUE;
20708}
20709
20710/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020711 * Return TRUE if typeval "tv" is set to be locked (immutable).
20712 * Also give an error message, using "name".
20713 */
20714 static int
20715tv_check_lock(lock, name)
20716 int lock;
20717 char_u *name;
20718{
20719 if (lock & VAR_LOCKED)
20720 {
20721 EMSG2(_("E741: Value is locked: %s"),
20722 name == NULL ? (char_u *)_("Unknown") : name);
20723 return TRUE;
20724 }
20725 if (lock & VAR_FIXED)
20726 {
20727 EMSG2(_("E742: Cannot change value of %s"),
20728 name == NULL ? (char_u *)_("Unknown") : name);
20729 return TRUE;
20730 }
20731 return FALSE;
20732}
20733
20734/*
Bram Moolenaar33570922005-01-25 22:26:29 +000020735 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020736 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020737 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000020738 * It is OK for "from" and "to" to point to the same item. This is used to
20739 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020740 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010020741 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020742copy_tv(from, to)
Bram Moolenaar33570922005-01-25 22:26:29 +000020743 typval_T *from;
20744 typval_T *to;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020746 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020747 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020748 switch (from->v_type)
20749 {
20750 case VAR_NUMBER:
20751 to->vval.v_number = from->vval.v_number;
20752 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020753#ifdef FEAT_FLOAT
20754 case VAR_FLOAT:
20755 to->vval.v_float = from->vval.v_float;
20756 break;
20757#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020758 case VAR_STRING:
20759 case VAR_FUNC:
20760 if (from->vval.v_string == NULL)
20761 to->vval.v_string = NULL;
20762 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020763 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020764 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000020765 if (from->v_type == VAR_FUNC)
20766 func_ref(to->vval.v_string);
20767 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020768 break;
20769 case VAR_LIST:
20770 if (from->vval.v_list == NULL)
20771 to->vval.v_list = NULL;
20772 else
20773 {
20774 to->vval.v_list = from->vval.v_list;
20775 ++to->vval.v_list->lv_refcount;
20776 }
20777 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000020778 case VAR_DICT:
20779 if (from->vval.v_dict == NULL)
20780 to->vval.v_dict = NULL;
20781 else
20782 {
20783 to->vval.v_dict = from->vval.v_dict;
20784 ++to->vval.v_dict->dv_refcount;
20785 }
20786 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020787 default:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020788 EMSG2(_(e_intern2), "copy_tv()");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020789 break;
20790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020791}
20792
20793/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000020794 * Make a copy of an item.
20795 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020796 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
20797 * reference to an already copied list/dict can be used.
20798 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000020799 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020800 static int
20801item_copy(from, to, deep, copyID)
Bram Moolenaar33570922005-01-25 22:26:29 +000020802 typval_T *from;
20803 typval_T *to;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020804 int deep;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020805 int copyID;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020806{
20807 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020808 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020809
Bram Moolenaar33570922005-01-25 22:26:29 +000020810 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000020811 {
20812 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020813 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020814 }
20815 ++recurse;
20816
20817 switch (from->v_type)
20818 {
20819 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020820#ifdef FEAT_FLOAT
20821 case VAR_FLOAT:
20822#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000020823 case VAR_STRING:
20824 case VAR_FUNC:
20825 copy_tv(from, to);
20826 break;
20827 case VAR_LIST:
20828 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020829 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020830 if (from->vval.v_list == NULL)
20831 to->vval.v_list = NULL;
20832 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
20833 {
20834 /* use the copy made earlier */
20835 to->vval.v_list = from->vval.v_list->lv_copylist;
20836 ++to->vval.v_list->lv_refcount;
20837 }
20838 else
20839 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
20840 if (to->vval.v_list == NULL)
20841 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020842 break;
20843 case VAR_DICT:
20844 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000020845 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020846 if (from->vval.v_dict == NULL)
20847 to->vval.v_dict = NULL;
20848 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
20849 {
20850 /* use the copy made earlier */
20851 to->vval.v_dict = from->vval.v_dict->dv_copydict;
20852 ++to->vval.v_dict->dv_refcount;
20853 }
20854 else
20855 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
20856 if (to->vval.v_dict == NULL)
20857 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020858 break;
20859 default:
20860 EMSG2(_(e_intern2), "item_copy()");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020861 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020862 }
20863 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020864 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000020865}
20866
20867/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868 * ":echo expr1 ..." print each argument separated with a space, add a
20869 * newline at the end.
20870 * ":echon expr1 ..." print each argument plain.
20871 */
20872 void
20873ex_echo(eap)
20874 exarg_T *eap;
20875{
20876 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000020877 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020878 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879 char_u *p;
20880 int needclr = TRUE;
20881 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000020882 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000020883
20884 if (eap->skip)
20885 ++emsg_skip;
20886 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
20887 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020888 /* If eval1() causes an error message the text from the command may
20889 * still need to be cleared. E.g., "echo 22,44". */
20890 need_clr_eos = needclr;
20891
Bram Moolenaar071d4272004-06-13 20:20:40 +000020892 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020893 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020894 {
20895 /*
20896 * Report the invalid expression unless the expression evaluation
20897 * has been cancelled due to an aborting error, an interrupt, or an
20898 * exception.
20899 */
20900 if (!aborting())
20901 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020902 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903 break;
20904 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020905 need_clr_eos = FALSE;
20906
Bram Moolenaar071d4272004-06-13 20:20:40 +000020907 if (!eap->skip)
20908 {
20909 if (atstart)
20910 {
20911 atstart = FALSE;
20912 /* Call msg_start() after eval1(), evaluating the expression
20913 * may cause a message to appear. */
20914 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010020915 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020020916 /* Mark the saved text as finishing the line, so that what
20917 * follows is displayed on a new line when scrolling back
20918 * at the more prompt. */
20919 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010020921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020922 }
20923 else if (eap->cmdidx == CMD_echo)
20924 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000020925 current_copyID += COPYID_INC;
20926 p = echo_string(&rettv, &tofree, numbuf, current_copyID);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020927 if (p != NULL)
20928 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020930 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020932 if (*p != TAB && needclr)
20933 {
20934 /* remove any text still there from the command */
20935 msg_clr_eos();
20936 needclr = FALSE;
20937 }
20938 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 }
20940 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020941 {
20942#ifdef FEAT_MBYTE
20943 if (has_mbyte)
20944 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020945 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020946
20947 (void)msg_outtrans_len_attr(p, i, echo_attr);
20948 p += i - 1;
20949 }
20950 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000020952 (void)msg_outtrans_len_attr(p, 1, echo_attr);
20953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020955 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020957 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 arg = skipwhite(arg);
20959 }
20960 eap->nextcmd = check_nextcmd(arg);
20961
20962 if (eap->skip)
20963 --emsg_skip;
20964 else
20965 {
20966 /* remove text that may still be there from the command */
20967 if (needclr)
20968 msg_clr_eos();
20969 if (eap->cmdidx == CMD_echo)
20970 msg_end();
20971 }
20972}
20973
20974/*
20975 * ":echohl {name}".
20976 */
20977 void
20978ex_echohl(eap)
20979 exarg_T *eap;
20980{
20981 int id;
20982
20983 id = syn_name2id(eap->arg);
20984 if (id == 0)
20985 echo_attr = 0;
20986 else
20987 echo_attr = syn_id2attr(id);
20988}
20989
20990/*
20991 * ":execute expr1 ..." execute the result of an expression.
20992 * ":echomsg expr1 ..." Print a message
20993 * ":echoerr expr1 ..." Print an error
20994 * Each gets spaces around each argument and a newline at the end for
20995 * echo commands
20996 */
20997 void
20998ex_execute(eap)
20999 exarg_T *eap;
21000{
21001 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000021002 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003 int ret = OK;
21004 char_u *p;
21005 garray_T ga;
21006 int len;
21007 int save_did_emsg;
21008
21009 ga_init2(&ga, 1, 80);
21010
21011 if (eap->skip)
21012 ++emsg_skip;
21013 while (*arg != NUL && *arg != '|' && *arg != '\n')
21014 {
21015 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021016 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021017 {
21018 /*
21019 * Report the invalid expression unless the expression evaluation
21020 * has been cancelled due to an aborting error, an interrupt, or an
21021 * exception.
21022 */
21023 if (!aborting())
21024 EMSG2(_(e_invexpr2), p);
21025 ret = FAIL;
21026 break;
21027 }
21028
21029 if (!eap->skip)
21030 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021031 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032 len = (int)STRLEN(p);
21033 if (ga_grow(&ga, len + 2) == FAIL)
21034 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021035 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021036 ret = FAIL;
21037 break;
21038 }
21039 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000021041 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021042 ga.ga_len += len;
21043 }
21044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021045 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046 arg = skipwhite(arg);
21047 }
21048
21049 if (ret != FAIL && ga.ga_data != NULL)
21050 {
21051 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000021052 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000021053 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000021054 out_flush();
21055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021056 else if (eap->cmdidx == CMD_echoerr)
21057 {
21058 /* We don't want to abort following commands, restore did_emsg. */
21059 save_did_emsg = did_emsg;
21060 EMSG((char_u *)ga.ga_data);
21061 if (!force_abort)
21062 did_emsg = save_did_emsg;
21063 }
21064 else if (eap->cmdidx == CMD_execute)
21065 do_cmdline((char_u *)ga.ga_data,
21066 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
21067 }
21068
21069 ga_clear(&ga);
21070
21071 if (eap->skip)
21072 --emsg_skip;
21073
21074 eap->nextcmd = check_nextcmd(arg);
21075}
21076
21077/*
21078 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
21079 * "arg" points to the "&" or '+' when called, to "option" when returning.
21080 * Returns NULL when no option name found. Otherwise pointer to the char
21081 * after the option name.
21082 */
21083 static char_u *
21084find_option_end(arg, opt_flags)
21085 char_u **arg;
21086 int *opt_flags;
21087{
21088 char_u *p = *arg;
21089
21090 ++p;
21091 if (*p == 'g' && p[1] == ':')
21092 {
21093 *opt_flags = OPT_GLOBAL;
21094 p += 2;
21095 }
21096 else if (*p == 'l' && p[1] == ':')
21097 {
21098 *opt_flags = OPT_LOCAL;
21099 p += 2;
21100 }
21101 else
21102 *opt_flags = 0;
21103
21104 if (!ASCII_ISALPHA(*p))
21105 return NULL;
21106 *arg = p;
21107
21108 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
21109 p += 4; /* termcap option */
21110 else
21111 while (ASCII_ISALPHA(*p))
21112 ++p;
21113 return p;
21114}
21115
21116/*
21117 * ":function"
21118 */
21119 void
21120ex_function(eap)
21121 exarg_T *eap;
21122{
21123 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021124 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021125 int j;
21126 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021128 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021129 char_u *name = NULL;
21130 char_u *p;
21131 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021132 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021133 garray_T newargs;
21134 garray_T newlines;
21135 int varargs = FALSE;
21136 int mustend = FALSE;
21137 int flags = 0;
21138 ufunc_T *fp;
21139 int indent;
21140 int nesting;
21141 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000021142 dictitem_T *v;
21143 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021144 static int func_nr = 0; /* number for nameless function */
21145 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021146 hashtab_T *ht;
21147 int todo;
21148 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021149 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021150
21151 /*
21152 * ":function" without argument: list functions.
21153 */
21154 if (ends_excmd(*eap->arg))
21155 {
21156 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021157 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021158 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000021159 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021160 {
21161 if (!HASHITEM_EMPTY(hi))
21162 {
21163 --todo;
21164 fp = HI2UF(hi);
21165 if (!isdigit(*fp->uf_name))
21166 list_func_head(fp, FALSE);
21167 }
21168 }
21169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021170 eap->nextcmd = check_nextcmd(eap->arg);
21171 return;
21172 }
21173
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021174 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021175 * ":function /pat": list functions matching pattern.
21176 */
21177 if (*eap->arg == '/')
21178 {
21179 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
21180 if (!eap->skip)
21181 {
21182 regmatch_T regmatch;
21183
21184 c = *p;
21185 *p = NUL;
21186 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
21187 *p = c;
21188 if (regmatch.regprog != NULL)
21189 {
21190 regmatch.rm_ic = p_ic;
21191
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021192 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021193 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
21194 {
21195 if (!HASHITEM_EMPTY(hi))
21196 {
21197 --todo;
21198 fp = HI2UF(hi);
21199 if (!isdigit(*fp->uf_name)
21200 && vim_regexec(&regmatch, fp->uf_name, 0))
21201 list_func_head(fp, FALSE);
21202 }
21203 }
Bram Moolenaar473de612013-06-08 18:19:48 +020021204 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000021205 }
21206 }
21207 if (*p == '/')
21208 ++p;
21209 eap->nextcmd = check_nextcmd(p);
21210 return;
21211 }
21212
21213 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021214 * Get the function name. There are these situations:
21215 * func normal function name
21216 * "name" == func, "fudi.fd_dict" == NULL
21217 * dict.func new dictionary entry
21218 * "name" == NULL, "fudi.fd_dict" set,
21219 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
21220 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021221 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021222 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21223 * dict.func existing dict entry that's not a Funcref
21224 * "name" == NULL, "fudi.fd_dict" set,
21225 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
21226 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021228 name = trans_function_name(&p, eap->skip, 0, &fudi);
21229 paren = (vim_strchr(p, '(') != NULL);
21230 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231 {
21232 /*
21233 * Return on an invalid expression in braces, unless the expression
21234 * evaluation has been cancelled due to an aborting error, an
21235 * interrupt, or an exception.
21236 */
21237 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021238 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021239 if (!eap->skip && fudi.fd_newkey != NULL)
21240 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021241 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021242 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 else
21245 eap->skip = TRUE;
21246 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000021247
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248 /* An error in a function call during evaluation of an expression in magic
21249 * braces should not cause the function not to be defined. */
21250 saved_did_emsg = did_emsg;
21251 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021252
21253 /*
21254 * ":function func" with only function name: list function.
21255 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021256 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021257 {
21258 if (!ends_excmd(*skipwhite(p)))
21259 {
21260 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021261 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262 }
21263 eap->nextcmd = check_nextcmd(p);
21264 if (eap->nextcmd != NULL)
21265 *p = NUL;
21266 if (!eap->skip && !got_int)
21267 {
21268 fp = find_func(name);
21269 if (fp != NULL)
21270 {
21271 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021272 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021274 if (FUNCLINE(fp, j) == NULL)
21275 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 msg_putchar('\n');
21277 msg_outnum((long)(j + 1));
21278 if (j < 9)
21279 msg_putchar(' ');
21280 if (j < 99)
21281 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021282 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 out_flush(); /* show a line at a time */
21284 ui_breakcheck();
21285 }
21286 if (!got_int)
21287 {
21288 msg_putchar('\n');
21289 msg_puts((char_u *)" endfunction");
21290 }
21291 }
21292 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021293 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021294 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021295 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 }
21297
21298 /*
21299 * ":function name(arg1, arg2)" Define function.
21300 */
21301 p = skipwhite(p);
21302 if (*p != '(')
21303 {
21304 if (!eap->skip)
21305 {
21306 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021307 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308 }
21309 /* attempt to continue by skipping some text */
21310 if (vim_strchr(p, '(') != NULL)
21311 p = vim_strchr(p, '(');
21312 }
21313 p = skipwhite(p + 1);
21314
21315 ga_init2(&newargs, (int)sizeof(char_u *), 3);
21316 ga_init2(&newlines, (int)sizeof(char_u *), 3);
21317
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021318 if (!eap->skip)
21319 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021320 /* Check the name of the function. Unless it's a dictionary function
21321 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021322 if (name != NULL)
21323 arg = name;
21324 else
21325 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000021326 if (arg != NULL && (fudi.fd_di == NULL
21327 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021328 {
21329 if (*arg == K_SPECIAL)
21330 j = 3;
21331 else
21332 j = 0;
21333 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
21334 : eval_isnamec(arg[j])))
21335 ++j;
21336 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000021337 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021338 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010021339 /* Disallow using the g: dict. */
21340 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
21341 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021342 }
21343
Bram Moolenaar071d4272004-06-13 20:20:40 +000021344 /*
21345 * Isolate the arguments: "arg1, arg2, ...)"
21346 */
21347 while (*p != ')')
21348 {
21349 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
21350 {
21351 varargs = TRUE;
21352 p += 3;
21353 mustend = TRUE;
21354 }
21355 else
21356 {
21357 arg = p;
21358 while (ASCII_ISALNUM(*p) || *p == '_')
21359 ++p;
21360 if (arg == p || isdigit(*arg)
21361 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
21362 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
21363 {
21364 if (!eap->skip)
21365 EMSG2(_("E125: Illegal argument: %s"), arg);
21366 break;
21367 }
21368 if (ga_grow(&newargs, 1) == FAIL)
21369 goto erret;
21370 c = *p;
21371 *p = NUL;
21372 arg = vim_strsave(arg);
21373 if (arg == NULL)
21374 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020021375
21376 /* Check for duplicate argument name. */
21377 for (i = 0; i < newargs.ga_len; ++i)
21378 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
21379 {
21380 EMSG2(_("E853: Duplicate argument name: %s"), arg);
21381 goto erret;
21382 }
21383
Bram Moolenaar071d4272004-06-13 20:20:40 +000021384 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
21385 *p = c;
21386 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 if (*p == ',')
21388 ++p;
21389 else
21390 mustend = TRUE;
21391 }
21392 p = skipwhite(p);
21393 if (mustend && *p != ')')
21394 {
21395 if (!eap->skip)
21396 EMSG2(_(e_invarg2), eap->arg);
21397 break;
21398 }
21399 }
21400 ++p; /* skip the ')' */
21401
Bram Moolenaare9a41262005-01-15 22:18:47 +000021402 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 for (;;)
21404 {
21405 p = skipwhite(p);
21406 if (STRNCMP(p, "range", 5) == 0)
21407 {
21408 flags |= FC_RANGE;
21409 p += 5;
21410 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000021411 else if (STRNCMP(p, "dict", 4) == 0)
21412 {
21413 flags |= FC_DICT;
21414 p += 4;
21415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416 else if (STRNCMP(p, "abort", 5) == 0)
21417 {
21418 flags |= FC_ABORT;
21419 p += 5;
21420 }
21421 else
21422 break;
21423 }
21424
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021425 /* When there is a line break use what follows for the function body.
21426 * Makes 'exe "func Test()\n...\nendfunc"' work. */
21427 if (*p == '\n')
21428 line_arg = p + 1;
21429 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021430 EMSG(_(e_trailing));
21431
21432 /*
21433 * Read the body of the function, until ":endfunction" is found.
21434 */
21435 if (KeyTyped)
21436 {
21437 /* Check if the function already exists, don't let the user type the
21438 * whole function before telling him it doesn't work! For a script we
21439 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021440 if (!eap->skip && !eap->forceit)
21441 {
21442 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
21443 EMSG(_(e_funcdict));
21444 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021445 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021447
Bram Moolenaard857f0e2005-06-21 22:37:39 +000021448 if (!eap->skip && did_emsg)
21449 goto erret;
21450
Bram Moolenaar071d4272004-06-13 20:20:40 +000021451 msg_putchar('\n'); /* don't overwrite the function name */
21452 cmdline_row = msg_row;
21453 }
21454
21455 indent = 2;
21456 nesting = 0;
21457 for (;;)
21458 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021459 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021460 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020021461 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021462 saved_wait_return = FALSE;
21463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021464 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021465 sourcing_lnum_off = sourcing_lnum;
21466
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021467 if (line_arg != NULL)
21468 {
21469 /* Use eap->arg, split up in parts by line breaks. */
21470 theline = line_arg;
21471 p = vim_strchr(theline, '\n');
21472 if (p == NULL)
21473 line_arg += STRLEN(line_arg);
21474 else
21475 {
21476 *p = NUL;
21477 line_arg = p + 1;
21478 }
21479 }
21480 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481 theline = getcmdline(':', 0L, indent);
21482 else
21483 theline = eap->getline(':', eap->cookie, indent);
21484 if (KeyTyped)
21485 lines_left = Rows - 1;
21486 if (theline == NULL)
21487 {
21488 EMSG(_("E126: Missing :endfunction"));
21489 goto erret;
21490 }
21491
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021492 /* Detect line continuation: sourcing_lnum increased more than one. */
21493 if (sourcing_lnum > sourcing_lnum_off + 1)
21494 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
21495 else
21496 sourcing_lnum_off = 0;
21497
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 if (skip_until != NULL)
21499 {
21500 /* between ":append" and "." and between ":python <<EOF" and "EOF"
21501 * don't check for ":endfunc". */
21502 if (STRCMP(theline, skip_until) == 0)
21503 {
21504 vim_free(skip_until);
21505 skip_until = NULL;
21506 }
21507 }
21508 else
21509 {
21510 /* skip ':' and blanks*/
21511 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
21512 ;
21513
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021514 /* Check for "endfunction". */
21515 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021517 if (line_arg == NULL)
21518 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021519 break;
21520 }
21521
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021522 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000021523 * at "end". */
21524 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
21525 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021526 else if (STRNCMP(p, "if", 2) == 0
21527 || STRNCMP(p, "wh", 2) == 0
21528 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 || STRNCMP(p, "try", 3) == 0)
21530 indent += 2;
21531
21532 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021533 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021535 if (*p == '!')
21536 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 p += eval_fname_script(p);
21538 if (ASCII_ISALPHA(*p))
21539 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021540 vim_free(trans_function_name(&p, TRUE, 0, NULL));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021541 if (*skipwhite(p) == '(')
21542 {
21543 ++nesting;
21544 indent += 2;
21545 }
21546 }
21547 }
21548
21549 /* Check for ":append" or ":insert". */
21550 p = skip_range(p, NULL);
21551 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
21552 || (p[0] == 'i'
21553 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
21554 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
21555 skip_until = vim_strsave((char_u *)".");
21556
21557 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
21558 arg = skipwhite(skiptowhite(p));
21559 if (arg[0] == '<' && arg[1] =='<'
21560 && ((p[0] == 'p' && p[1] == 'y'
21561 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
21562 || (p[0] == 'p' && p[1] == 'e'
21563 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
21564 || (p[0] == 't' && p[1] == 'c'
21565 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020021566 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
21567 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021568 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
21569 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000021570 || (p[0] == 'm' && p[1] == 'z'
21571 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000021572 ))
21573 {
21574 /* ":python <<" continues until a dot, like ":append" */
21575 p = skipwhite(arg + 2);
21576 if (*p == NUL)
21577 skip_until = vim_strsave((char_u *)".");
21578 else
21579 skip_until = vim_strsave(p);
21580 }
21581 }
21582
21583 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021584 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021585 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021586 if (line_arg == NULL)
21587 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021589 }
21590
21591 /* Copy the line to newly allocated memory. get_one_sourceline()
21592 * allocates 250 bytes per line, this saves 80% on average. The cost
21593 * is an extra alloc/free. */
21594 p = vim_strsave(theline);
21595 if (p != NULL)
21596 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021597 if (line_arg == NULL)
21598 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000021599 theline = p;
21600 }
21601
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000021602 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
21603
21604 /* Add NULL lines for continuation lines, so that the line count is
21605 * equal to the index in the growarray. */
21606 while (sourcing_lnum_off-- > 0)
21607 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000021608
21609 /* Check for end of eap->arg. */
21610 if (line_arg != NULL && *line_arg == NUL)
21611 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021612 }
21613
21614 /* Don't define the function when skipping commands or when an error was
21615 * detected. */
21616 if (eap->skip || did_emsg)
21617 goto erret;
21618
21619 /*
21620 * If there are no errors, add the function
21621 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021622 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021623 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021624 v = find_var(name, &ht);
Bram Moolenaar33570922005-01-25 22:26:29 +000021625 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021626 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021627 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021628 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021629 goto erret;
21630 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021631
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021632 fp = find_func(name);
21633 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021634 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021635 if (!eap->forceit)
21636 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000021637 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021638 goto erret;
21639 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021640 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021641 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000021642 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021643 name);
21644 goto erret;
21645 }
21646 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021647 ga_clear_strings(&(fp->uf_args));
21648 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021649 vim_free(name);
21650 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021652 }
21653 else
21654 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021655 char numbuf[20];
21656
21657 fp = NULL;
21658 if (fudi.fd_newkey == NULL && !eap->forceit)
21659 {
21660 EMSG(_(e_funcdict));
21661 goto erret;
21662 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000021663 if (fudi.fd_di == NULL)
21664 {
21665 /* Can't add a function to a locked dictionary */
21666 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg))
21667 goto erret;
21668 }
21669 /* Can't change an existing function if it is locked */
21670 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg))
21671 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021672
21673 /* Give the function a sequential number. Can only be used with a
21674 * Funcref! */
21675 vim_free(name);
21676 sprintf(numbuf, "%d", ++func_nr);
21677 name = vim_strsave((char_u *)numbuf);
21678 if (name == NULL)
21679 goto erret;
21680 }
21681
21682 if (fp == NULL)
21683 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021684 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021685 {
21686 int slen, plen;
21687 char_u *scriptname;
21688
21689 /* Check that the autoload name matches the script name. */
21690 j = FAIL;
21691 if (sourcing_name != NULL)
21692 {
21693 scriptname = autoload_name(name);
21694 if (scriptname != NULL)
21695 {
21696 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021697 plen = (int)STRLEN(p);
21698 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021699 if (slen > plen && fnamecmp(p,
21700 sourcing_name + slen - plen) == 0)
21701 j = OK;
21702 vim_free(scriptname);
21703 }
21704 }
21705 if (j == FAIL)
21706 {
21707 EMSG2(_("E746: Function name does not match script file name: %s"), name);
21708 goto erret;
21709 }
21710 }
21711
21712 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713 if (fp == NULL)
21714 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021715
21716 if (fudi.fd_dict != NULL)
21717 {
21718 if (fudi.fd_di == NULL)
21719 {
21720 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021721 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021722 if (fudi.fd_di == NULL)
21723 {
21724 vim_free(fp);
21725 goto erret;
21726 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021727 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
21728 {
21729 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000021730 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021731 goto erret;
21732 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021733 }
21734 else
21735 /* overwrite existing dict entry */
21736 clear_tv(&fudi.fd_di->di_tv);
21737 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021738 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021739 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021740 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021741
21742 /* behave like "dict" was used */
21743 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021744 }
21745
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021747 STRCPY(fp->uf_name, name);
21748 hash_add(&func_hashtab, UF2HIKEY(fp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021749 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021750 fp->uf_args = newargs;
21751 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000021752#ifdef FEAT_PROFILE
21753 fp->uf_tml_count = NULL;
21754 fp->uf_tml_total = NULL;
21755 fp->uf_tml_self = NULL;
21756 fp->uf_profiling = FALSE;
21757 if (prof_def_func())
21758 func_do_profile(fp);
21759#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000021760 fp->uf_varargs = varargs;
21761 fp->uf_flags = flags;
21762 fp->uf_calls = 0;
21763 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021764 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021765
21766erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000021767 ga_clear_strings(&newargs);
21768 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021769ret_free:
21770 vim_free(skip_until);
21771 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021772 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020021774 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021775}
21776
21777/*
21778 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000021779 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021781 * flags:
21782 * TFN_INT: internal function name OK
21783 * TFN_QUIET: be quiet
Bram Moolenaar071d4272004-06-13 20:20:40 +000021784 * Advances "pp" to just after the function name (if no error).
21785 */
21786 static char_u *
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021787trans_function_name(pp, skip, flags, fdp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021788 char_u **pp;
21789 int skip; /* only find the end, don't evaluate */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021790 int flags;
Bram Moolenaar33570922005-01-25 22:26:29 +000021791 funcdict_T *fdp; /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021793 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021794 char_u *start;
21795 char_u *end;
21796 int lead;
21797 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000021799 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021800
21801 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021802 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000021803 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000021804
21805 /* Check for hard coded <SNR>: already translated function ID (from a user
21806 * command). */
21807 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
21808 && (*pp)[2] == (int)KE_SNR)
21809 {
21810 *pp += 3;
21811 len = get_id_len(pp) + 3;
21812 return vim_strnsave(start, len);
21813 }
21814
21815 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
21816 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000021818 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021819 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021820
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021821 end = get_lval(start, NULL, &lv, FALSE, skip, flags & TFN_QUIET,
21822 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021823 if (end == start)
21824 {
21825 if (!skip)
21826 EMSG(_("E129: Function name required"));
21827 goto theend;
21828 }
Bram Moolenaara7043832005-01-21 11:56:39 +000021829 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021830 {
21831 /*
21832 * Report an invalid expression in braces, unless the expression
21833 * evaluation has been cancelled due to an aborting error, an
21834 * interrupt, or an exception.
21835 */
21836 if (!aborting())
21837 {
21838 if (end != NULL)
21839 EMSG2(_(e_invarg2), start);
21840 }
21841 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021842 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021843 goto theend;
21844 }
21845
21846 if (lv.ll_tv != NULL)
21847 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021848 if (fdp != NULL)
21849 {
21850 fdp->fd_dict = lv.ll_dict;
21851 fdp->fd_newkey = lv.ll_newkey;
21852 lv.ll_newkey = NULL;
21853 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021854 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021855 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
21856 {
21857 name = vim_strsave(lv.ll_tv->vval.v_string);
21858 *pp = end;
21859 }
21860 else
21861 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021862 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
21863 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021864 EMSG(_(e_funcref));
21865 else
21866 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021867 name = NULL;
21868 }
21869 goto theend;
21870 }
21871
21872 if (lv.ll_name == NULL)
21873 {
21874 /* Error found, but continue after the function name. */
21875 *pp = end;
21876 goto theend;
21877 }
21878
Bram Moolenaar33e1a802007-09-06 12:26:44 +000021879 /* Check if the name is a Funcref. If so, use the value. */
21880 if (lv.ll_exp_name != NULL)
21881 {
21882 len = (int)STRLEN(lv.ll_exp_name);
21883 name = deref_func_name(lv.ll_exp_name, &len);
21884 if (name == lv.ll_exp_name)
21885 name = NULL;
21886 }
21887 else
21888 {
21889 len = (int)(end - *pp);
21890 name = deref_func_name(*pp, &len);
21891 if (name == *pp)
21892 name = NULL;
21893 }
21894 if (name != NULL)
21895 {
21896 name = vim_strsave(name);
21897 *pp = end;
21898 goto theend;
21899 }
21900
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021901 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021902 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021903 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000021904 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
21905 && STRNCMP(lv.ll_name, "s:", 2) == 0)
21906 {
21907 /* When there was "s:" already or the name expanded to get a
21908 * leading "s:" then remove it. */
21909 lv.ll_name += 2;
21910 len -= 2;
21911 lead = 2;
21912 }
21913 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021914 else
Bram Moolenaara7043832005-01-21 11:56:39 +000021915 {
21916 if (lead == 2) /* skip over "s:" */
21917 lv.ll_name += 2;
21918 len = (int)(end - lv.ll_name);
21919 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021920
21921 /*
21922 * Copy the function name to allocated memory.
21923 * Accept <SID>name() inside a script, translate into <SNR>123_name().
21924 * Accept <SNR>123_name() outside a script.
21925 */
21926 if (skip)
21927 lead = 0; /* do nothing */
21928 else if (lead > 0)
21929 {
21930 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000021931 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
21932 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021933 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000021934 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021935 if (current_SID <= 0)
21936 {
21937 EMSG(_(e_usingsid));
21938 goto theend;
21939 }
21940 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
21941 lead += (int)STRLEN(sid_buf);
21942 }
21943 }
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021944 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021945 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000021946 EMSG2(_("E128: Function name must start with a capital or contain a colon: %s"), lv.ll_name);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021947 goto theend;
21948 }
21949 name = alloc((unsigned)(len + lead + 1));
21950 if (name != NULL)
21951 {
21952 if (lead > 0)
21953 {
21954 name[0] = K_SPECIAL;
21955 name[1] = KS_EXTRA;
21956 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000021957 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000021958 STRCPY(name + 3, sid_buf);
21959 }
21960 mch_memmove(name + lead, lv.ll_name, (size_t)len);
21961 name[len + lead] = NUL;
21962 }
21963 *pp = end;
21964
21965theend:
21966 clear_lval(&lv);
21967 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021968}
21969
21970/*
21971 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
21972 * Return 2 if "p" starts with "s:".
21973 * Return 0 otherwise.
21974 */
21975 static int
21976eval_fname_script(p)
21977 char_u *p;
21978{
21979 if (p[0] == '<' && (STRNICMP(p + 1, "SID>", 4) == 0
21980 || STRNICMP(p + 1, "SNR>", 4) == 0))
21981 return 5;
21982 if (p[0] == 's' && p[1] == ':')
21983 return 2;
21984 return 0;
21985}
21986
21987/*
21988 * Return TRUE if "p" starts with "<SID>" or "s:".
21989 * Only works if eval_fname_script() returned non-zero for "p"!
21990 */
21991 static int
21992eval_fname_sid(p)
21993 char_u *p;
21994{
21995 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
21996}
21997
21998/*
21999 * List the head of the function: "name(arg1, arg2)".
22000 */
22001 static void
22002list_func_head(fp, indent)
22003 ufunc_T *fp;
22004 int indent;
22005{
22006 int j;
22007
22008 msg_start();
22009 if (indent)
22010 MSG_PUTS(" ");
22011 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022012 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022013 {
22014 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022015 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022016 }
22017 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022018 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022020 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022021 {
22022 if (j)
22023 MSG_PUTS(", ");
22024 msg_puts(FUNCARG(fp, j));
22025 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022026 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022027 {
22028 if (j)
22029 MSG_PUTS(", ");
22030 MSG_PUTS("...");
22031 }
22032 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020022033 if (fp->uf_flags & FC_ABORT)
22034 MSG_PUTS(" abort");
22035 if (fp->uf_flags & FC_RANGE)
22036 MSG_PUTS(" range");
22037 if (fp->uf_flags & FC_DICT)
22038 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000022039 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000022040 if (p_verbose > 0)
22041 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022042}
22043
22044/*
22045 * Find a function by name, return pointer to it in ufuncs.
22046 * Return NULL for unknown function.
22047 */
22048 static ufunc_T *
22049find_func(name)
22050 char_u *name;
22051{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022052 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022054 hi = hash_find(&func_hashtab, name);
22055 if (!HASHITEM_EMPTY(hi))
22056 return HI2UF(hi);
22057 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058}
22059
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000022060#if defined(EXITFREE) || defined(PROTO)
22061 void
22062free_all_functions()
22063{
22064 hashitem_T *hi;
22065
22066 /* Need to start all over every time, because func_free() may change the
22067 * hash table. */
22068 while (func_hashtab.ht_used > 0)
22069 for (hi = func_hashtab.ht_array; ; ++hi)
22070 if (!HASHITEM_EMPTY(hi))
22071 {
22072 func_free(HI2UF(hi));
22073 break;
22074 }
22075}
22076#endif
22077
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022078 int
22079translated_function_exists(name)
22080 char_u *name;
22081{
22082 if (builtin_function(name))
22083 return find_internal_func(name) >= 0;
22084 return find_func(name) != NULL;
22085}
22086
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022087/*
22088 * Return TRUE if a function "name" exists.
22089 */
22090 static int
22091function_exists(name)
22092 char_u *name;
22093{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022094 char_u *nm = name;
22095 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022096 int n = FALSE;
22097
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000022098 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000022099 nm = skipwhite(nm);
22100
22101 /* Only accept "funcname", "funcname ", "funcname (..." and
22102 * "funcname(...", not "funcname!...". */
22103 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022104 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000022105 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022106 return n;
22107}
22108
Bram Moolenaara1544c02013-05-30 12:35:52 +020022109 char_u *
22110get_expanded_name(name, check)
22111 char_u *name;
22112 int check;
22113{
22114 char_u *nm = name;
22115 char_u *p;
22116
22117 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
22118
22119 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022120 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020022121 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020022122
Bram Moolenaara1544c02013-05-30 12:35:52 +020022123 vim_free(p);
22124 return NULL;
22125}
22126
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022127/*
22128 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022129 * lower case letter and doesn't contain a ':' or AUTOLOAD_CHAR.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022130 */
22131 static int
22132builtin_function(name)
22133 char_u *name;
22134{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022135 return ASCII_ISLOWER(name[0]) && vim_strchr(name, ':') == NULL
22136 && vim_strchr(name, AUTOLOAD_CHAR) == NULL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022137}
22138
Bram Moolenaar05159a02005-02-26 23:04:13 +000022139#if defined(FEAT_PROFILE) || defined(PROTO)
22140/*
22141 * Start profiling function "fp".
22142 */
22143 static void
22144func_do_profile(fp)
22145 ufunc_T *fp;
22146{
Bram Moolenaar904c6222010-07-24 16:57:39 +020022147 int len = fp->uf_lines.ga_len;
22148
22149 if (len == 0)
22150 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000022151 fp->uf_tm_count = 0;
22152 profile_zero(&fp->uf_tm_self);
22153 profile_zero(&fp->uf_tm_total);
22154 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022155 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022156 if (fp->uf_tml_total == NULL)
22157 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022158 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022159 if (fp->uf_tml_self == NULL)
22160 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020022161 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022162 fp->uf_tml_idx = -1;
22163 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
22164 || fp->uf_tml_self == NULL)
22165 return; /* out of memory */
22166
22167 fp->uf_profiling = TRUE;
22168}
22169
22170/*
22171 * Dump the profiling results for all functions in file "fd".
22172 */
22173 void
22174func_dump_profile(fd)
22175 FILE *fd;
22176{
22177 hashitem_T *hi;
22178 int todo;
22179 ufunc_T *fp;
22180 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000022181 ufunc_T **sorttab;
22182 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022183
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022184 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022185 if (todo == 0)
22186 return; /* nothing to dump */
22187
Bram Moolenaar73830342005-02-28 22:48:19 +000022188 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T) * todo));
22189
Bram Moolenaar05159a02005-02-26 23:04:13 +000022190 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
22191 {
22192 if (!HASHITEM_EMPTY(hi))
22193 {
22194 --todo;
22195 fp = HI2UF(hi);
22196 if (fp->uf_profiling)
22197 {
Bram Moolenaar73830342005-02-28 22:48:19 +000022198 if (sorttab != NULL)
22199 sorttab[st_len++] = fp;
22200
Bram Moolenaar05159a02005-02-26 23:04:13 +000022201 if (fp->uf_name[0] == K_SPECIAL)
22202 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
22203 else
22204 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
22205 if (fp->uf_tm_count == 1)
22206 fprintf(fd, "Called 1 time\n");
22207 else
22208 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
22209 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
22210 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
22211 fprintf(fd, "\n");
22212 fprintf(fd, "count total (s) self (s)\n");
22213
22214 for (i = 0; i < fp->uf_lines.ga_len; ++i)
22215 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000022216 if (FUNCLINE(fp, i) == NULL)
22217 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000022218 prof_func_line(fd, fp->uf_tml_count[i],
22219 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022220 fprintf(fd, "%s\n", FUNCLINE(fp, i));
22221 }
22222 fprintf(fd, "\n");
22223 }
22224 }
22225 }
Bram Moolenaar73830342005-02-28 22:48:19 +000022226
22227 if (sorttab != NULL && st_len > 0)
22228 {
22229 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22230 prof_total_cmp);
22231 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
22232 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
22233 prof_self_cmp);
22234 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
22235 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000022236
22237 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022238}
Bram Moolenaar73830342005-02-28 22:48:19 +000022239
22240 static void
22241prof_sort_list(fd, sorttab, st_len, title, prefer_self)
22242 FILE *fd;
22243 ufunc_T **sorttab;
22244 int st_len;
22245 char *title;
22246 int prefer_self; /* when equal print only self time */
22247{
22248 int i;
22249 ufunc_T *fp;
22250
22251 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
22252 fprintf(fd, "count total (s) self (s) function\n");
22253 for (i = 0; i < 20 && i < st_len; ++i)
22254 {
22255 fp = sorttab[i];
22256 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
22257 prefer_self);
22258 if (fp->uf_name[0] == K_SPECIAL)
22259 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
22260 else
22261 fprintf(fd, " %s()\n", fp->uf_name);
22262 }
22263 fprintf(fd, "\n");
22264}
22265
22266/*
22267 * Print the count and times for one function or function line.
22268 */
22269 static void
22270prof_func_line(fd, count, total, self, prefer_self)
22271 FILE *fd;
22272 int count;
22273 proftime_T *total;
22274 proftime_T *self;
22275 int prefer_self; /* when equal print only self time */
22276{
22277 if (count > 0)
22278 {
22279 fprintf(fd, "%5d ", count);
22280 if (prefer_self && profile_equal(total, self))
22281 fprintf(fd, " ");
22282 else
22283 fprintf(fd, "%s ", profile_msg(total));
22284 if (!prefer_self && profile_equal(total, self))
22285 fprintf(fd, " ");
22286 else
22287 fprintf(fd, "%s ", profile_msg(self));
22288 }
22289 else
22290 fprintf(fd, " ");
22291}
22292
22293/*
22294 * Compare function for total time sorting.
22295 */
22296 static int
22297#ifdef __BORLANDC__
22298_RTLENTRYF
22299#endif
22300prof_total_cmp(s1, s2)
22301 const void *s1;
22302 const void *s2;
22303{
22304 ufunc_T *p1, *p2;
22305
22306 p1 = *(ufunc_T **)s1;
22307 p2 = *(ufunc_T **)s2;
22308 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
22309}
22310
22311/*
22312 * Compare function for self time sorting.
22313 */
22314 static int
22315#ifdef __BORLANDC__
22316_RTLENTRYF
22317#endif
22318prof_self_cmp(s1, s2)
22319 const void *s1;
22320 const void *s2;
22321{
22322 ufunc_T *p1, *p2;
22323
22324 p1 = *(ufunc_T **)s1;
22325 p2 = *(ufunc_T **)s2;
22326 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
22327}
22328
Bram Moolenaar05159a02005-02-26 23:04:13 +000022329#endif
22330
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022331/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022332 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022333 * Return TRUE if a package was loaded.
22334 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020022335 static int
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022336script_autoload(name, reload)
22337 char_u *name;
22338 int reload; /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022339{
22340 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022341 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022342 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022343 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022344
Bram Moolenaar2cefbed2010-07-11 23:12:29 +020022345 /* Return quickly when autoload disabled. */
22346 if (no_autoload)
22347 return FALSE;
22348
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022349 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022350 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022351 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022352 return FALSE;
22353
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022354 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022355
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022356 /* Find the name in the list of previously loaded package names. Skip
22357 * "autoload/", it's always the same. */
22358 for (i = 0; i < ga_loaded.ga_len; ++i)
22359 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
22360 break;
22361 if (!reload && i < ga_loaded.ga_len)
22362 ret = FALSE; /* was loaded already */
22363 else
22364 {
22365 /* Remember the name if it wasn't loaded already. */
22366 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
22367 {
22368 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
22369 tofree = NULL;
22370 }
22371
22372 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000022373 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022374 ret = TRUE;
22375 }
22376
22377 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022378 return ret;
22379}
22380
22381/*
22382 * Return the autoload script name for a function or variable name.
22383 * Returns NULL when out of memory.
22384 */
22385 static char_u *
22386autoload_name(name)
22387 char_u *name;
22388{
22389 char_u *p;
22390 char_u *scriptname;
22391
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022392 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022393 scriptname = alloc((unsigned)(STRLEN(name) + 14));
22394 if (scriptname == NULL)
22395 return FALSE;
22396 STRCPY(scriptname, "autoload/");
22397 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022398 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022399 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022400 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022401 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022402 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000022403}
22404
Bram Moolenaar071d4272004-06-13 20:20:40 +000022405#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
22406
22407/*
22408 * Function given to ExpandGeneric() to obtain the list of user defined
22409 * function names.
22410 */
22411 char_u *
22412get_user_func_name(xp, idx)
22413 expand_T *xp;
22414 int idx;
22415{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022416 static long_u done;
22417 static hashitem_T *hi;
22418 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022419
22420 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022422 done = 0;
22423 hi = func_hashtab.ht_array;
22424 }
22425 if (done < func_hashtab.ht_used)
22426 {
22427 if (done++ > 0)
22428 ++hi;
22429 while (HASHITEM_EMPTY(hi))
22430 ++hi;
22431 fp = HI2UF(hi);
22432
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022433 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010022434 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010022435
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022436 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
22437 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438
22439 cat_func_name(IObuff, fp);
22440 if (xp->xp_context != EXPAND_USER_FUNC)
22441 {
22442 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022443 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444 STRCAT(IObuff, ")");
22445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446 return IObuff;
22447 }
22448 return NULL;
22449}
22450
22451#endif /* FEAT_CMDL_COMPL */
22452
22453/*
22454 * Copy the function name of "fp" to buffer "buf".
22455 * "buf" must be able to hold the function name plus three bytes.
22456 * Takes care of script-local function names.
22457 */
22458 static void
22459cat_func_name(buf, fp)
22460 char_u *buf;
22461 ufunc_T *fp;
22462{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022463 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022464 {
22465 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022466 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022467 }
22468 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022469 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022470}
22471
22472/*
22473 * ":delfunction {name}"
22474 */
22475 void
22476ex_delfunction(eap)
22477 exarg_T *eap;
22478{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022479 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022480 char_u *p;
22481 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000022482 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483
22484 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022485 name = trans_function_name(&p, eap->skip, 0, &fudi);
22486 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022487 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022488 {
22489 if (fudi.fd_dict != NULL && !eap->skip)
22490 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022491 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493 if (!ends_excmd(*skipwhite(p)))
22494 {
22495 vim_free(name);
22496 EMSG(_(e_trailing));
22497 return;
22498 }
22499 eap->nextcmd = check_nextcmd(p);
22500 if (eap->nextcmd != NULL)
22501 *p = NUL;
22502
22503 if (!eap->skip)
22504 fp = find_func(name);
22505 vim_free(name);
22506
22507 if (!eap->skip)
22508 {
22509 if (fp == NULL)
22510 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000022511 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022512 return;
22513 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022514 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022515 {
22516 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
22517 return;
22518 }
22519
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022520 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022521 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022522 /* Delete the dict item that refers to the function, it will
22523 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022524 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022526 else
22527 func_free(fp);
22528 }
22529}
22530
22531/*
22532 * Free a function and remove it from the list of functions.
22533 */
22534 static void
22535func_free(fp)
22536 ufunc_T *fp;
22537{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022538 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022539
22540 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022541 ga_clear_strings(&(fp->uf_args));
22542 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000022543#ifdef FEAT_PROFILE
22544 vim_free(fp->uf_tml_count);
22545 vim_free(fp->uf_tml_total);
22546 vim_free(fp->uf_tml_self);
22547#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022548
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022549 /* remove the function from the function hashtable */
22550 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
22551 if (HASHITEM_EMPTY(hi))
22552 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022553 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022554 hash_remove(&func_hashtab, hi);
22555
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022556 vim_free(fp);
22557}
22558
22559/*
22560 * Unreference a Function: decrement the reference count and free it when it
22561 * becomes zero. Only for numbered functions.
22562 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022563 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022564func_unref(name)
22565 char_u *name;
22566{
22567 ufunc_T *fp;
22568
22569 if (name != NULL && isdigit(*name))
22570 {
22571 fp = find_func(name);
22572 if (fp == NULL)
22573 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022574 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022575 {
22576 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022577 * when "uf_calls" becomes zero. */
22578 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022579 func_free(fp);
22580 }
22581 }
22582}
22583
22584/*
22585 * Count a reference to a Function.
22586 */
Bram Moolenaardb913952012-06-29 12:54:53 +020022587 void
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022588func_ref(name)
22589 char_u *name;
22590{
22591 ufunc_T *fp;
22592
22593 if (name != NULL && isdigit(*name))
22594 {
22595 fp = find_func(name);
22596 if (fp == NULL)
22597 EMSG2(_(e_intern2), "func_ref()");
22598 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022599 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600 }
22601}
22602
22603/*
22604 * Call a user function.
22605 */
22606 static void
Bram Moolenaare9a41262005-01-15 22:18:47 +000022607call_user_func(fp, argcount, argvars, rettv, firstline, lastline, selfdict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022608 ufunc_T *fp; /* pointer to function */
22609 int argcount; /* nr of args */
Bram Moolenaar33570922005-01-25 22:26:29 +000022610 typval_T *argvars; /* arguments */
22611 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 linenr_T firstline; /* first line of range */
22613 linenr_T lastline; /* last line of range */
Bram Moolenaar33570922005-01-25 22:26:29 +000022614 dict_T *selfdict; /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615{
Bram Moolenaar33570922005-01-25 22:26:29 +000022616 char_u *save_sourcing_name;
22617 linenr_T save_sourcing_lnum;
22618 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022619 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000022620 int save_did_emsg;
22621 static int depth = 0;
22622 dictitem_T *v;
22623 int fixvar_idx = 0; /* index in fixvar[] */
22624 int i;
22625 int ai;
22626 char_u numbuf[NUMBUFLEN];
22627 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022628#ifdef FEAT_PROFILE
22629 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022630 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022631#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632
22633 /* If depth of calling is getting too high, don't execute the function */
22634 if (depth >= p_mfd)
22635 {
22636 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022637 rettv->v_type = VAR_NUMBER;
22638 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639 return;
22640 }
22641 ++depth;
22642
22643 line_breakcheck(); /* check for CTRL-C hit */
22644
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022645 fc = (funccall_T *)alloc(sizeof(funccall_T));
22646 fc->caller = current_funccal;
22647 current_funccal = fc;
22648 fc->func = fp;
22649 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022650 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022651 fc->linenr = 0;
22652 fc->returned = FALSE;
22653 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022655 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
22656 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022657
Bram Moolenaar33570922005-01-25 22:26:29 +000022658 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022659 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000022660 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
22661 * each argument variable and saves a lot of time.
22662 */
22663 /*
22664 * Init l: variables.
22665 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022666 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000022667 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022668 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022669 /* Set l:self to "selfdict". Use "name" to avoid a warning from
22670 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022671 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000022672 name = v->di_key;
22673 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000022674 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022675 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022676 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022677 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022678 v->di_tv.vval.v_dict = selfdict;
22679 ++selfdict->dv_refcount;
22680 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000022681
Bram Moolenaar33570922005-01-25 22:26:29 +000022682 /*
22683 * Init a: variables.
22684 * Set a:0 to "argcount".
22685 * Set a:000 to a list with room for the "..." arguments.
22686 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022687 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022688 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022689 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022690 /* Use "name" to avoid a warning from some compiler that checks the
22691 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022692 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000022693 name = v->di_key;
22694 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000022695 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022696 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022697 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022698 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022699 v->di_tv.vval.v_list = &fc->l_varlist;
22700 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
22701 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
22702 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022703
22704 /*
22705 * Set a:firstline to "firstline" and a:lastline to "lastline".
22706 * Set a:name to named arguments.
22707 * Set a:N to the "..." arguments.
22708 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022709 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022710 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022711 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000022712 (varnumber_T)lastline);
22713 for (i = 0; i < argcount; ++i)
22714 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022715 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000022716 if (ai < 0)
22717 /* named argument a:name */
22718 name = FUNCARG(fp, i);
22719 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000022720 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022721 /* "..." argument a:1, a:2, etc. */
22722 sprintf((char *)numbuf, "%d", ai + 1);
22723 name = numbuf;
22724 }
22725 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
22726 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022727 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022728 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22729 }
22730 else
22731 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022732 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22733 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000022734 if (v == NULL)
22735 break;
22736 v->di_flags = DI_FLAGS_RO;
22737 }
22738 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022739 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000022740
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022741 /* Note: the values are copied directly to avoid alloc/free.
22742 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022743 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022744 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022745
22746 if (ai >= 0 && ai < MAX_FUNC_ARGS)
22747 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022748 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
22749 fc->l_listitems[ai].li_tv = argvars[i];
22750 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022751 }
22752 }
22753
Bram Moolenaar071d4272004-06-13 20:20:40 +000022754 /* Don't redraw while executing the function. */
22755 ++RedrawingDisabled;
22756 save_sourcing_name = sourcing_name;
22757 save_sourcing_lnum = sourcing_lnum;
22758 sourcing_lnum = 1;
22759 sourcing_name = alloc((unsigned)((save_sourcing_name == NULL ? 0
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022760 : STRLEN(save_sourcing_name)) + STRLEN(fp->uf_name) + 13));
Bram Moolenaar071d4272004-06-13 20:20:40 +000022761 if (sourcing_name != NULL)
22762 {
22763 if (save_sourcing_name != NULL
22764 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
22765 sprintf((char *)sourcing_name, "%s..", save_sourcing_name);
22766 else
22767 STRCPY(sourcing_name, "function ");
22768 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
22769
22770 if (p_verbose >= 12)
22771 {
22772 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022773 verbose_enter_scroll();
22774
Bram Moolenaar555b2802005-05-19 21:08:39 +000022775 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022776 if (p_verbose >= 14)
22777 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022778 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022779 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022780 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022781 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782
22783 msg_puts((char_u *)"(");
22784 for (i = 0; i < argcount; ++i)
22785 {
22786 if (i > 0)
22787 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022788 if (argvars[i].v_type == VAR_NUMBER)
22789 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022790 else
22791 {
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022792 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
22793 if (s != NULL)
22794 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022795 if (vim_strsize(s) > MSG_BUF_CLEN)
22796 {
22797 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22798 s = buf;
22799 }
22800 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022801 vim_free(tofree);
22802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022803 }
22804 }
22805 msg_puts((char_u *)")");
22806 }
22807 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022808
22809 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022810 --no_wait_return;
22811 }
22812 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000022813#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022814 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022815 {
22816 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
22817 func_do_profile(fp);
22818 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022819 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022820 {
22821 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022822 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022823 profile_zero(&fp->uf_tm_children);
22824 }
22825 script_prof_save(&wait_start);
22826 }
22827#endif
22828
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022830 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022831 save_did_emsg = did_emsg;
22832 did_emsg = FALSE;
22833
22834 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022835 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000022836 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
22837
22838 --RedrawingDisabled;
22839
22840 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022841 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022843 clear_tv(rettv);
22844 rettv->v_type = VAR_NUMBER;
22845 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022846 }
22847
Bram Moolenaar05159a02005-02-26 23:04:13 +000022848#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022849 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022850 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000022851 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000022852 profile_end(&call_start);
22853 profile_sub_wait(&wait_start, &call_start);
22854 profile_add(&fp->uf_tm_total, &call_start);
22855 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022856 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022857 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022858 profile_add(&fc->caller->func->uf_tm_children, &call_start);
22859 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000022860 }
22861 }
22862#endif
22863
Bram Moolenaar071d4272004-06-13 20:20:40 +000022864 /* when being verbose, mention the return value */
22865 if (p_verbose >= 12)
22866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022868 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000022871 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022872 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000022873 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022874 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000022875 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000022876 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000022877 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000022878 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000022879 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022880 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000022881
Bram Moolenaar555b2802005-05-19 21:08:39 +000022882 /* The value may be very long. Skip the middle part, so that we
22883 * have some idea how it starts and ends. smsg() would always
22884 * truncate it at the end. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022885 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022886 if (s != NULL)
22887 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010022888 if (vim_strsize(s) > MSG_BUF_CLEN)
22889 {
22890 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
22891 s = buf;
22892 }
22893 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000022894 vim_free(tofree);
22895 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022896 }
22897 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022898
22899 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022900 --no_wait_return;
22901 }
22902
22903 vim_free(sourcing_name);
22904 sourcing_name = save_sourcing_name;
22905 sourcing_lnum = save_sourcing_lnum;
22906 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000022907#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000022908 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000022909 script_prof_restore(&wait_start);
22910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911
22912 if (p_verbose >= 12 && sourcing_name != NULL)
22913 {
22914 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022915 verbose_enter_scroll();
22916
Bram Moolenaar555b2802005-05-19 21:08:39 +000022917 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022918 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000022919
22920 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022921 --no_wait_return;
22922 }
22923
22924 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022925 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022926 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022927
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022928 /* If the a:000 list and the l: and a: dicts are not referenced we can
22929 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022930 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
22931 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
22932 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
22933 {
22934 free_funccal(fc, FALSE);
22935 }
22936 else
22937 {
22938 hashitem_T *hi;
22939 listitem_T *li;
22940 int todo;
22941
22942 /* "fc" is still in use. This can happen when returning "a:000" or
22943 * assigning "l:" to a global variable.
22944 * Link "fc" in the list for garbage collection later. */
22945 fc->caller = previous_funccal;
22946 previous_funccal = fc;
22947
22948 /* Make a copy of the a: variables, since we didn't do that above. */
22949 todo = (int)fc->l_avars.dv_hashtab.ht_used;
22950 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
22951 {
22952 if (!HASHITEM_EMPTY(hi))
22953 {
22954 --todo;
22955 v = HI2DI(hi);
22956 copy_tv(&v->di_tv, &v->di_tv);
22957 }
22958 }
22959
22960 /* Make a copy of the a:000 items, since we didn't do that above. */
22961 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
22962 copy_tv(&li->li_tv, &li->li_tv);
22963 }
22964}
22965
22966/*
22967 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022968 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022969 */
22970 static int
22971can_free_funccal(fc, copyID)
22972 funccall_T *fc;
22973 int copyID;
22974{
22975 return (fc->l_varlist.lv_copyID != copyID
22976 && fc->l_vars.dv_copyID != copyID
22977 && fc->l_avars.dv_copyID != copyID);
22978}
22979
22980/*
22981 * Free "fc" and what it contains.
22982 */
22983 static void
22984free_funccal(fc, free_val)
22985 funccall_T *fc;
22986 int free_val; /* a: vars were allocated */
22987{
22988 listitem_T *li;
22989
22990 /* The a: variables typevals may not have been allocated, only free the
22991 * allocated variables. */
22992 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
22993
22994 /* free all l: variables */
22995 vars_clear(&fc->l_vars.dv_hashtab);
22996
22997 /* Free the a:000 variables if they were allocated. */
22998 if (free_val)
22999 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
23000 clear_tv(&li->li_tv);
23001
23002 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003}
23004
23005/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023006 * Add a number variable "name" to dict "dp" with value "nr".
23007 */
23008 static void
23009add_nr_var(dp, v, name, nr)
23010 dict_T *dp;
23011 dictitem_T *v;
23012 char *name;
23013 varnumber_T nr;
23014{
23015 STRCPY(v->di_key, name);
23016 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
23017 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
23018 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023019 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000023020 v->di_tv.vval.v_number = nr;
23021}
23022
23023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023024 * ":return [expr]"
23025 */
23026 void
23027ex_return(eap)
23028 exarg_T *eap;
23029{
23030 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023031 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032 int returning = FALSE;
23033
23034 if (current_funccal == NULL)
23035 {
23036 EMSG(_("E133: :return not inside a function"));
23037 return;
23038 }
23039
23040 if (eap->skip)
23041 ++emsg_skip;
23042
23043 eap->nextcmd = NULL;
23044 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023045 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023046 {
23047 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023048 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023049 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023050 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023051 }
23052 /* It's safer to return also on error. */
23053 else if (!eap->skip)
23054 {
23055 /*
23056 * Return unless the expression evaluation has been cancelled due to an
23057 * aborting error, an interrupt, or an exception.
23058 */
23059 if (!aborting())
23060 returning = do_return(eap, FALSE, TRUE, NULL);
23061 }
23062
23063 /* When skipping or the return gets pending, advance to the next command
23064 * in this line (!returning). Otherwise, ignore the rest of the line.
23065 * Following lines will be ignored by get_func_line(). */
23066 if (returning)
23067 eap->nextcmd = NULL;
23068 else if (eap->nextcmd == NULL) /* no argument */
23069 eap->nextcmd = check_nextcmd(arg);
23070
23071 if (eap->skip)
23072 --emsg_skip;
23073}
23074
23075/*
23076 * Return from a function. Possibly makes the return pending. Also called
23077 * for a pending return at the ":endtry" or after returning from an extra
23078 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000023079 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023080 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023081 * FALSE when the return gets pending.
23082 */
23083 int
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023084do_return(eap, reanimate, is_cmd, rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085 exarg_T *eap;
23086 int reanimate;
23087 int is_cmd;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023088 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089{
23090 int idx;
23091 struct condstack *cstack = eap->cstack;
23092
23093 if (reanimate)
23094 /* Undo the return. */
23095 current_funccal->returned = FALSE;
23096
23097 /*
23098 * Cleanup (and inactivate) conditionals, but stop when a try conditional
23099 * not in its finally clause (which then is to be executed next) is found.
23100 * In this case, make the ":return" pending for execution at the ":endtry".
23101 * Otherwise, return normally.
23102 */
23103 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
23104 if (idx >= 0)
23105 {
23106 cstack->cs_pending[idx] = CSTP_RETURN;
23107
23108 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023109 /* A pending return again gets pending. "rettv" points to an
23110 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000023111 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023112 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023113 else
23114 {
23115 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023116 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023118 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023119
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023120 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121 {
23122 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023123 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023124 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023125 else
23126 EMSG(_(e_outofmem));
23127 }
23128 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023129 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023130
23131 if (reanimate)
23132 {
23133 /* The pending return value could be overwritten by a ":return"
23134 * without argument in a finally clause; reset the default
23135 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023136 current_funccal->rettv->v_type = VAR_NUMBER;
23137 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138 }
23139 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023140 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 }
23142 else
23143 {
23144 current_funccal->returned = TRUE;
23145
23146 /* If the return is carried out now, store the return value. For
23147 * a return immediately after reanimation, the value is already
23148 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023149 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023150 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023151 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000023152 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023153 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023154 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023155 }
23156 }
23157
23158 return idx < 0;
23159}
23160
23161/*
23162 * Free the variable with a pending return value.
23163 */
23164 void
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023165discard_pending_return(rettv)
23166 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023167{
Bram Moolenaar33570922005-01-25 22:26:29 +000023168 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169}
23170
23171/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023172 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173 * is an allocated string. Used by report_pending() for verbose messages.
23174 */
23175 char_u *
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023176get_return_cmd(rettv)
23177 void *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023178{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023179 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023180 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023181 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023182
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023183 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023184 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023185 if (s == NULL)
23186 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023187
23188 STRCPY(IObuff, ":return ");
23189 STRNCPY(IObuff + 8, s, IOSIZE - 8);
23190 if (STRLEN(s) + 8 >= IOSIZE)
23191 STRCPY(IObuff + IOSIZE - 4, "...");
23192 vim_free(tofree);
23193 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194}
23195
23196/*
23197 * Get next function line.
23198 * Called by do_cmdline() to get the next line.
23199 * Returns allocated string, or NULL for end of function.
23200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023201 char_u *
23202get_func_line(c, cookie, indent)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023203 int c UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023204 void *cookie;
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023205 int indent UNUSED;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023206{
Bram Moolenaar33570922005-01-25 22:26:29 +000023207 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023208 ufunc_T *fp = fcp->func;
23209 char_u *retval;
23210 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023211
23212 /* If breakpoints have been added/deleted need to check for it. */
23213 if (fcp->dbg_tick != debug_tick)
23214 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023215 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023216 sourcing_lnum);
23217 fcp->dbg_tick = debug_tick;
23218 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000023219#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023220 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000023221 func_line_end(cookie);
23222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023223
Bram Moolenaar05159a02005-02-26 23:04:13 +000023224 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023225 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
23226 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227 retval = NULL;
23228 else
23229 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023230 /* Skip NULL lines (continuation lines). */
23231 while (fcp->linenr < gap->ga_len
23232 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
23233 ++fcp->linenr;
23234 if (fcp->linenr >= gap->ga_len)
23235 retval = NULL;
23236 else
23237 {
23238 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
23239 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023240#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000023241 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023242 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023243#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023245 }
23246
23247 /* Did we encounter a breakpoint? */
23248 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
23249 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000023250 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023251 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000023252 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023253 sourcing_lnum);
23254 fcp->dbg_tick = debug_tick;
23255 }
23256
23257 return retval;
23258}
23259
Bram Moolenaar05159a02005-02-26 23:04:13 +000023260#if defined(FEAT_PROFILE) || defined(PROTO)
23261/*
23262 * Called when starting to read a function line.
23263 * "sourcing_lnum" must be correct!
23264 * When skipping lines it may not actually be executed, but we won't find out
23265 * until later and we need to store the time now.
23266 */
23267 void
23268func_line_start(cookie)
23269 void *cookie;
23270{
23271 funccall_T *fcp = (funccall_T *)cookie;
23272 ufunc_T *fp = fcp->func;
23273
23274 if (fp->uf_profiling && sourcing_lnum >= 1
23275 && sourcing_lnum <= fp->uf_lines.ga_len)
23276 {
23277 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023278 /* Skip continuation lines. */
23279 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
23280 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023281 fp->uf_tml_execed = FALSE;
23282 profile_start(&fp->uf_tml_start);
23283 profile_zero(&fp->uf_tml_children);
23284 profile_get_wait(&fp->uf_tml_wait);
23285 }
23286}
23287
23288/*
23289 * Called when actually executing a function line.
23290 */
23291 void
23292func_line_exec(cookie)
23293 void *cookie;
23294{
23295 funccall_T *fcp = (funccall_T *)cookie;
23296 ufunc_T *fp = fcp->func;
23297
23298 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23299 fp->uf_tml_execed = TRUE;
23300}
23301
23302/*
23303 * Called when done with a function line.
23304 */
23305 void
23306func_line_end(cookie)
23307 void *cookie;
23308{
23309 funccall_T *fcp = (funccall_T *)cookie;
23310 ufunc_T *fp = fcp->func;
23311
23312 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
23313 {
23314 if (fp->uf_tml_execed)
23315 {
23316 ++fp->uf_tml_count[fp->uf_tml_idx];
23317 profile_end(&fp->uf_tml_start);
23318 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023319 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000023320 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
23321 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000023322 }
23323 fp->uf_tml_idx = -1;
23324 }
23325}
23326#endif
23327
Bram Moolenaar071d4272004-06-13 20:20:40 +000023328/*
23329 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023330 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000023331 */
23332 int
23333func_has_ended(cookie)
23334 void *cookie;
23335{
Bram Moolenaar33570922005-01-25 22:26:29 +000023336 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023337
23338 /* Ignore the "abort" flag if the abortion behavior has been changed due to
23339 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023340 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341 || fcp->returned);
23342}
23343
23344/*
23345 * return TRUE if cookie indicates a function which "abort"s on errors.
23346 */
23347 int
23348func_has_abort(cookie)
23349 void *cookie;
23350{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023351 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352}
23353
23354#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
23355typedef enum
23356{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023357 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
23358 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
23359 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023360} var_flavour_T;
23361
23362static var_flavour_T var_flavour __ARGS((char_u *varname));
23363
23364 static var_flavour_T
23365var_flavour(varname)
23366 char_u *varname;
23367{
23368 char_u *p = varname;
23369
23370 if (ASCII_ISUPPER(*p))
23371 {
23372 while (*(++p))
23373 if (ASCII_ISLOWER(*p))
23374 return VAR_FLAVOUR_SESSION;
23375 return VAR_FLAVOUR_VIMINFO;
23376 }
23377 else
23378 return VAR_FLAVOUR_DEFAULT;
23379}
23380#endif
23381
23382#if defined(FEAT_VIMINFO) || defined(PROTO)
23383/*
23384 * Restore global vars that start with a capital from the viminfo file
23385 */
23386 int
23387read_viminfo_varlist(virp, writing)
23388 vir_T *virp;
23389 int writing;
23390{
23391 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023392 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000023393 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394
23395 if (!writing && (find_viminfo_parameter('!') != NULL))
23396 {
23397 tab = vim_strchr(virp->vir_line + 1, '\t');
23398 if (tab != NULL)
23399 {
23400 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023401 switch (*tab)
23402 {
23403 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023404#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023405 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023406#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023407 case 'D': type = VAR_DICT; break;
23408 case 'L': type = VAR_LIST; break;
23409 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023410
23411 tab = vim_strchr(tab, '\t');
23412 if (tab != NULL)
23413 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023414 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023415 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023416 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000023417 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023418#ifdef FEAT_FLOAT
23419 else if (type == VAR_FLOAT)
23420 (void)string2float(tab + 1, &tv.vval.v_float);
23421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023422 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023423 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023424 if (type == VAR_DICT || type == VAR_LIST)
23425 {
23426 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
23427
23428 if (etv == NULL)
23429 /* Failed to parse back the dict or list, use it as a
23430 * string. */
23431 tv.v_type = VAR_STRING;
23432 else
23433 {
23434 vim_free(tv.vval.v_string);
23435 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010023436 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023437 }
23438 }
23439
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023440 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023441
23442 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023443 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023444 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
23445 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446 }
23447 }
23448 }
23449
23450 return viminfo_readline(virp);
23451}
23452
23453/*
23454 * Write global vars that start with a capital to the viminfo file
23455 */
23456 void
23457write_viminfo_varlist(fp)
23458 FILE *fp;
23459{
Bram Moolenaar33570922005-01-25 22:26:29 +000023460 hashitem_T *hi;
23461 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023462 int todo;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023463 char *s;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023464 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023465 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023466 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467
23468 if (find_viminfo_parameter('!') == NULL)
23469 return;
23470
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020023471 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000023472
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023473 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023474 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023475 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023476 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023478 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023479 this_var = HI2DI(hi);
23480 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023481 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023482 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000023483 {
23484 case VAR_STRING: s = "STR"; break;
23485 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023486#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023487 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023488#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020023489 case VAR_DICT: s = "DIC"; break;
23490 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara7043832005-01-21 11:56:39 +000023491 default: continue;
23492 }
Bram Moolenaar33570922005-01-25 22:26:29 +000023493 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000023494 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023495 if (p != NULL)
23496 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000023497 vim_free(tofree);
23498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499 }
23500 }
23501}
23502#endif
23503
23504#if defined(FEAT_SESSION) || defined(PROTO)
23505 int
23506store_session_globals(fd)
23507 FILE *fd;
23508{
Bram Moolenaar33570922005-01-25 22:26:29 +000023509 hashitem_T *hi;
23510 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000023511 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023512 char_u *p, *t;
23513
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023514 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000023515 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023516 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023517 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023519 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000023520 this_var = HI2DI(hi);
23521 if ((this_var->di_tv.v_type == VAR_NUMBER
23522 || this_var->di_tv.v_type == VAR_STRING)
23523 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000023524 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023525 /* Escape special characters with a backslash. Turn a LF and
23526 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000023527 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000023528 (char_u *)"\\\"\n\r");
23529 if (p == NULL) /* out of memory */
23530 break;
23531 for (t = p; *t != NUL; ++t)
23532 if (*t == '\n')
23533 *t = 'n';
23534 else if (*t == '\r')
23535 *t = 'r';
23536 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000023537 this_var->di_key,
23538 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23539 : ' ',
23540 p,
23541 (this_var->di_tv.v_type == VAR_STRING) ? '"'
23542 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000023543 || put_eol(fd) == FAIL)
23544 {
23545 vim_free(p);
23546 return FAIL;
23547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023548 vim_free(p);
23549 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023550#ifdef FEAT_FLOAT
23551 else if (this_var->di_tv.v_type == VAR_FLOAT
23552 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
23553 {
23554 float_T f = this_var->di_tv.vval.v_float;
23555 int sign = ' ';
23556
23557 if (f < 0)
23558 {
23559 f = -f;
23560 sign = '-';
23561 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010023562 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023563 this_var->di_key, sign, f) < 0)
23564 || put_eol(fd) == FAIL)
23565 return FAIL;
23566 }
23567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568 }
23569 }
23570 return OK;
23571}
23572#endif
23573
Bram Moolenaar661b1822005-07-28 22:36:45 +000023574/*
23575 * Display script name where an item was last set.
23576 * Should only be invoked when 'verbose' is non-zero.
23577 */
23578 void
23579last_set_msg(scriptID)
23580 scid_T scriptID;
23581{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023582 char_u *p;
23583
Bram Moolenaar661b1822005-07-28 22:36:45 +000023584 if (scriptID != 0)
23585 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000023586 p = home_replace_save(NULL, get_scriptname(scriptID));
23587 if (p != NULL)
23588 {
23589 verbose_enter();
23590 MSG_PUTS(_("\n\tLast set from "));
23591 MSG_PUTS(p);
23592 vim_free(p);
23593 verbose_leave();
23594 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000023595 }
23596}
23597
Bram Moolenaard812df62008-11-09 12:46:09 +000023598/*
23599 * List v:oldfiles in a nice way.
23600 */
Bram Moolenaard812df62008-11-09 12:46:09 +000023601 void
23602ex_oldfiles(eap)
Bram Moolenaaraf0167f2009-05-16 15:31:32 +000023603 exarg_T *eap UNUSED;
Bram Moolenaard812df62008-11-09 12:46:09 +000023604{
23605 list_T *l = vimvars[VV_OLDFILES].vv_list;
23606 listitem_T *li;
23607 int nr = 0;
23608
23609 if (l == NULL)
23610 msg((char_u *)_("No old files"));
23611 else
23612 {
23613 msg_start();
23614 msg_scroll = TRUE;
23615 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
23616 {
23617 msg_outnum((long)++nr);
23618 MSG_PUTS(": ");
23619 msg_outtrans(get_tv_string(&li->li_tv));
23620 msg_putchar('\n');
23621 out_flush(); /* output one line at a time */
23622 ui_breakcheck();
23623 }
23624 /* Assume "got_int" was set to truncate the listing. */
23625 got_int = FALSE;
23626
23627#ifdef FEAT_BROWSE_CMD
23628 if (cmdmod.browse)
23629 {
23630 quit_more = FALSE;
23631 nr = prompt_for_number(FALSE);
23632 msg_starthere();
23633 if (nr > 0)
23634 {
23635 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
23636 (long)nr);
23637
23638 if (p != NULL)
23639 {
23640 p = expand_env_save(p);
23641 eap->arg = p;
23642 eap->cmdidx = CMD_edit;
23643 cmdmod.browse = FALSE;
23644 do_exedit(eap, NULL);
23645 vim_free(p);
23646 }
23647 }
23648 }
23649#endif
23650 }
23651}
23652
Bram Moolenaar071d4272004-06-13 20:20:40 +000023653#endif /* FEAT_EVAL */
23654
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023656#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023657
23658#ifdef WIN3264
23659/*
23660 * Functions for ":8" filename modifier: get 8.3 version of a filename.
23661 */
23662static int get_short_pathname __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23663static int shortpath_for_invalid_fname __ARGS((char_u **fname, char_u **bufp, int *fnamelen));
23664static int shortpath_for_partial __ARGS((char_u **fnamep, char_u **bufp, int *fnamelen));
23665
23666/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023667 * Get the short path (8.3) for the filename in "fnamep".
23668 * Only works for a valid file name.
23669 * When the path gets longer "fnamep" is changed and the allocated buffer
23670 * is put in "bufp".
23671 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
23672 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 */
23674 static int
23675get_short_pathname(fnamep, bufp, fnamelen)
23676 char_u **fnamep;
23677 char_u **bufp;
23678 int *fnamelen;
23679{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023680 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023681 char_u *newbuf;
23682
23683 len = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684 l = GetShortPathName(*fnamep, *fnamep, len);
23685 if (l > len - 1)
23686 {
23687 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023688 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023689 newbuf = vim_strnsave(*fnamep, l);
23690 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023691 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023692
23693 vim_free(*bufp);
23694 *fnamep = *bufp = newbuf;
23695
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023696 /* Really should always succeed, as the buffer is big enough. */
23697 l = GetShortPathName(*fnamep, *fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698 }
23699
23700 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023701 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702}
23703
23704/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023705 * Get the short path (8.3) for the filename in "fname". The converted
23706 * path is returned in "bufp".
23707 *
23708 * Some of the directories specified in "fname" may not exist. This function
23709 * will shorten the existing directories at the beginning of the path and then
23710 * append the remaining non-existing path.
23711 *
23712 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020023713 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023714 * bufp - Pointer to an allocated buffer for the filename.
23715 * fnamelen - Length of the filename pointed to by fname
23716 *
23717 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000023718 */
23719 static int
23720shortpath_for_invalid_fname(fname, bufp, fnamelen)
23721 char_u **fname;
23722 char_u **bufp;
23723 int *fnamelen;
23724{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023725 char_u *short_fname, *save_fname, *pbuf_unused;
23726 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023727 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023728 int old_len, len;
23729 int new_len, sfx_len;
23730 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023731
23732 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023733 old_len = *fnamelen;
23734 save_fname = vim_strnsave(*fname, old_len);
23735 pbuf_unused = NULL;
23736 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023737
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023738 endp = save_fname + old_len - 1; /* Find the end of the copy */
23739 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023741 /*
23742 * Try shortening the supplied path till it succeeds by removing one
23743 * directory at a time from the tail of the path.
23744 */
23745 len = 0;
23746 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023747 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023748 /* go back one path-separator */
23749 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
23750 --endp;
23751 if (endp <= save_fname)
23752 break; /* processed the complete path */
23753
23754 /*
23755 * Replace the path separator with a NUL and try to shorten the
23756 * resulting path.
23757 */
23758 ch = *endp;
23759 *endp = 0;
23760 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000023761 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023762 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
23763 {
23764 retval = FAIL;
23765 goto theend;
23766 }
23767 *endp = ch; /* preserve the string */
23768
23769 if (len > 0)
23770 break; /* successfully shortened the path */
23771
23772 /* failed to shorten the path. Skip the path separator */
23773 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023774 }
23775
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023776 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023777 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023778 /*
23779 * Succeeded in shortening the path. Now concatenate the shortened
23780 * path with the remaining path at the tail.
23781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023782
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023783 /* Compute the length of the new path. */
23784 sfx_len = (int)(save_endp - endp) + 1;
23785 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023786
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023787 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023788 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023789 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023790 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023791 /* There is not enough space in the currently allocated string,
23792 * copy it to a buffer big enough. */
23793 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023794 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023795 {
23796 retval = FAIL;
23797 goto theend;
23798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023799 }
23800 else
23801 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023802 /* Transfer short_fname to the main buffer (it's big enough),
23803 * unless get_short_pathname() did its work in-place. */
23804 *fname = *bufp = save_fname;
23805 if (short_fname != save_fname)
23806 vim_strncpy(save_fname, short_fname, len);
23807 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023808 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023809
23810 /* concat the not-shortened part of the path */
23811 vim_strncpy(*fname + len, endp, sfx_len);
23812 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023813 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023814
23815theend:
23816 vim_free(pbuf_unused);
23817 vim_free(save_fname);
23818
23819 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023820}
23821
23822/*
23823 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023824 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023825 */
23826 static int
23827shortpath_for_partial(fnamep, bufp, fnamelen)
23828 char_u **fnamep;
23829 char_u **bufp;
23830 int *fnamelen;
23831{
23832 int sepcount, len, tflen;
23833 char_u *p;
23834 char_u *pbuf, *tfname;
23835 int hasTilde;
23836
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023837 /* Count up the path separators from the RHS.. so we know which part
23838 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023840 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023841 if (vim_ispathsep(*p))
23842 ++sepcount;
23843
23844 /* Need full path first (use expand_env() to remove a "~/") */
23845 hasTilde = (**fnamep == '~');
23846 if (hasTilde)
23847 pbuf = tfname = expand_env_save(*fnamep);
23848 else
23849 pbuf = tfname = FullName_save(*fnamep, FALSE);
23850
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023851 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023853 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
23854 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855
23856 if (len == 0)
23857 {
23858 /* Don't have a valid filename, so shorten the rest of the
23859 * path if we can. This CAN give us invalid 8.3 filenames, but
23860 * there's not a lot of point in guessing what it might be.
23861 */
23862 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023863 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
23864 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023865 }
23866
23867 /* Count the paths backward to find the beginning of the desired string. */
23868 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023869 {
23870#ifdef FEAT_MBYTE
23871 if (has_mbyte)
23872 p -= mb_head_off(tfname, p);
23873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 if (vim_ispathsep(*p))
23875 {
23876 if (sepcount == 0 || (hasTilde && sepcount == 1))
23877 break;
23878 else
23879 sepcount --;
23880 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023882 if (hasTilde)
23883 {
23884 --p;
23885 if (p >= tfname)
23886 *p = '~';
23887 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023888 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023889 }
23890 else
23891 ++p;
23892
23893 /* Copy in the string - p indexes into tfname - allocated at pbuf */
23894 vim_free(*bufp);
23895 *fnamelen = (int)STRLEN(p);
23896 *bufp = pbuf;
23897 *fnamep = p;
23898
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023899 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900}
23901#endif /* WIN3264 */
23902
23903/*
23904 * Adjust a filename, according to a string of modifiers.
23905 * *fnamep must be NUL terminated when called. When returning, the length is
23906 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000023907 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023908 * When there is an error, *fnamep is set to NULL.
23909 */
23910 int
23911modify_fname(src, usedlen, fnamep, bufp, fnamelen)
23912 char_u *src; /* string with modifiers */
23913 int *usedlen; /* characters after src that are used */
23914 char_u **fnamep; /* file name so far */
23915 char_u **bufp; /* buffer for allocated file name or NULL */
23916 int *fnamelen; /* length of fnamep */
23917{
23918 int valid = 0;
23919 char_u *tail;
23920 char_u *s, *p, *pbuf;
23921 char_u dirname[MAXPATHL];
23922 int c;
23923 int has_fullname = 0;
23924#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020023925 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926 int has_shortname = 0;
23927#endif
23928
23929repeat:
23930 /* ":p" - full path/file_name */
23931 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
23932 {
23933 has_fullname = 1;
23934
23935 valid |= VALID_PATH;
23936 *usedlen += 2;
23937
23938 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
23939 if ((*fnamep)[0] == '~'
23940#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
23941 && ((*fnamep)[1] == '/'
23942# ifdef BACKSLASH_IN_FILENAME
23943 || (*fnamep)[1] == '\\'
23944# endif
23945 || (*fnamep)[1] == NUL)
23946
23947#endif
23948 )
23949 {
23950 *fnamep = expand_env_save(*fnamep);
23951 vim_free(*bufp); /* free any allocated file name */
23952 *bufp = *fnamep;
23953 if (*fnamep == NULL)
23954 return -1;
23955 }
23956
23957 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000023958 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 {
23960 if (vim_ispathsep(*p)
23961 && p[1] == '.'
23962 && (p[2] == NUL
23963 || vim_ispathsep(p[2])
23964 || (p[2] == '.'
23965 && (p[3] == NUL || vim_ispathsep(p[3])))))
23966 break;
23967 }
23968
23969 /* FullName_save() is slow, don't use it when not needed. */
23970 if (*p != NUL || !vim_isAbsName(*fnamep))
23971 {
23972 *fnamep = FullName_save(*fnamep, *p != NUL);
23973 vim_free(*bufp); /* free any allocated file name */
23974 *bufp = *fnamep;
23975 if (*fnamep == NULL)
23976 return -1;
23977 }
23978
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020023979#ifdef WIN3264
23980# if _WIN32_WINNT >= 0x0500
23981 if (vim_strchr(*fnamep, '~') != NULL)
23982 {
23983 /* Expand 8.3 filename to full path. Needed to make sure the same
23984 * file does not have two different names.
23985 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
23986 p = alloc(_MAX_PATH + 1);
23987 if (p != NULL)
23988 {
23989 if (GetLongPathName(*fnamep, p, MAXPATHL))
23990 {
23991 vim_free(*bufp);
23992 *bufp = *fnamep = p;
23993 }
23994 else
23995 vim_free(p);
23996 }
23997 }
23998# endif
23999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024000 /* Append a path separator to a directory. */
24001 if (mch_isdir(*fnamep))
24002 {
24003 /* Make room for one or two extra characters. */
24004 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
24005 vim_free(*bufp); /* free any allocated file name */
24006 *bufp = *fnamep;
24007 if (*fnamep == NULL)
24008 return -1;
24009 add_pathsep(*fnamep);
24010 }
24011 }
24012
24013 /* ":." - path relative to the current directory */
24014 /* ":~" - path relative to the home directory */
24015 /* ":8" - shortname path - postponed till after */
24016 while (src[*usedlen] == ':'
24017 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
24018 {
24019 *usedlen += 2;
24020 if (c == '8')
24021 {
24022#ifdef WIN3264
24023 has_shortname = 1; /* Postpone this. */
24024#endif
24025 continue;
24026 }
24027 pbuf = NULL;
24028 /* Need full path first (use expand_env() to remove a "~/") */
24029 if (!has_fullname)
24030 {
24031 if (c == '.' && **fnamep == '~')
24032 p = pbuf = expand_env_save(*fnamep);
24033 else
24034 p = pbuf = FullName_save(*fnamep, FALSE);
24035 }
24036 else
24037 p = *fnamep;
24038
24039 has_fullname = 0;
24040
24041 if (p != NULL)
24042 {
24043 if (c == '.')
24044 {
24045 mch_dirname(dirname, MAXPATHL);
24046 s = shorten_fname(p, dirname);
24047 if (s != NULL)
24048 {
24049 *fnamep = s;
24050 if (pbuf != NULL)
24051 {
24052 vim_free(*bufp); /* free any allocated file name */
24053 *bufp = pbuf;
24054 pbuf = NULL;
24055 }
24056 }
24057 }
24058 else
24059 {
24060 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
24061 /* Only replace it when it starts with '~' */
24062 if (*dirname == '~')
24063 {
24064 s = vim_strsave(dirname);
24065 if (s != NULL)
24066 {
24067 *fnamep = s;
24068 vim_free(*bufp);
24069 *bufp = s;
24070 }
24071 }
24072 }
24073 vim_free(pbuf);
24074 }
24075 }
24076
24077 tail = gettail(*fnamep);
24078 *fnamelen = (int)STRLEN(*fnamep);
24079
24080 /* ":h" - head, remove "/file_name", can be repeated */
24081 /* Don't remove the first "/" or "c:\" */
24082 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
24083 {
24084 valid |= VALID_HEAD;
24085 *usedlen += 2;
24086 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000024087 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024088 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024089 *fnamelen = (int)(tail - *fnamep);
24090#ifdef VMS
24091 if (*fnamelen > 0)
24092 *fnamelen += 1; /* the path separator is part of the path */
24093#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000024094 if (*fnamelen == 0)
24095 {
24096 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
24097 p = vim_strsave((char_u *)".");
24098 if (p == NULL)
24099 return -1;
24100 vim_free(*bufp);
24101 *bufp = *fnamep = tail = p;
24102 *fnamelen = 1;
24103 }
24104 else
24105 {
24106 while (tail > s && !after_pathsep(s, tail))
24107 mb_ptr_back(*fnamep, tail);
24108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024109 }
24110
24111 /* ":8" - shortname */
24112 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
24113 {
24114 *usedlen += 2;
24115#ifdef WIN3264
24116 has_shortname = 1;
24117#endif
24118 }
24119
24120#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020024121 /*
24122 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024123 */
24124 if (has_shortname)
24125 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024126 /* Copy the string if it is shortened by :h and when it wasn't copied
24127 * yet, because we are going to change it in place. Avoids changing
24128 * the buffer name for "%:8". */
24129 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130 {
24131 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020024132 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133 return -1;
24134 vim_free(*bufp);
24135 *bufp = *fnamep = p;
24136 }
24137
24138 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020024139 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140 if (!has_fullname && !vim_isAbsName(*fnamep))
24141 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024142 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143 return -1;
24144 }
24145 else
24146 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024147 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148
Bram Moolenaardc935552011-08-17 15:23:23 +020024149 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024150 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024151 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024152 return -1;
24153
24154 if (l == 0)
24155 {
Bram Moolenaardc935552011-08-17 15:23:23 +020024156 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000024158 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024159 return -1;
24160 }
24161 *fnamelen = l;
24162 }
24163 }
24164#endif /* WIN3264 */
24165
24166 /* ":t" - tail, just the basename */
24167 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
24168 {
24169 *usedlen += 2;
24170 *fnamelen -= (int)(tail - *fnamep);
24171 *fnamep = tail;
24172 }
24173
24174 /* ":e" - extension, can be repeated */
24175 /* ":r" - root, without extension, can be repeated */
24176 while (src[*usedlen] == ':'
24177 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
24178 {
24179 /* find a '.' in the tail:
24180 * - for second :e: before the current fname
24181 * - otherwise: The last '.'
24182 */
24183 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
24184 s = *fnamep - 2;
24185 else
24186 s = *fnamep + *fnamelen - 1;
24187 for ( ; s > tail; --s)
24188 if (s[0] == '.')
24189 break;
24190 if (src[*usedlen + 1] == 'e') /* :e */
24191 {
24192 if (s > tail)
24193 {
24194 *fnamelen += (int)(*fnamep - (s + 1));
24195 *fnamep = s + 1;
24196#ifdef VMS
24197 /* cut version from the extension */
24198 s = *fnamep + *fnamelen - 1;
24199 for ( ; s > *fnamep; --s)
24200 if (s[0] == ';')
24201 break;
24202 if (s > *fnamep)
24203 *fnamelen = s - *fnamep;
24204#endif
24205 }
24206 else if (*fnamep <= tail)
24207 *fnamelen = 0;
24208 }
24209 else /* :r */
24210 {
24211 if (s > tail) /* remove one extension */
24212 *fnamelen = (int)(s - *fnamep);
24213 }
24214 *usedlen += 2;
24215 }
24216
24217 /* ":s?pat?foo?" - substitute */
24218 /* ":gs?pat?foo?" - global substitute */
24219 if (src[*usedlen] == ':'
24220 && (src[*usedlen + 1] == 's'
24221 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
24222 {
24223 char_u *str;
24224 char_u *pat;
24225 char_u *sub;
24226 int sep;
24227 char_u *flags;
24228 int didit = FALSE;
24229
24230 flags = (char_u *)"";
24231 s = src + *usedlen + 2;
24232 if (src[*usedlen + 1] == 'g')
24233 {
24234 flags = (char_u *)"g";
24235 ++s;
24236 }
24237
24238 sep = *s++;
24239 if (sep)
24240 {
24241 /* find end of pattern */
24242 p = vim_strchr(s, sep);
24243 if (p != NULL)
24244 {
24245 pat = vim_strnsave(s, (int)(p - s));
24246 if (pat != NULL)
24247 {
24248 s = p + 1;
24249 /* find end of substitution */
24250 p = vim_strchr(s, sep);
24251 if (p != NULL)
24252 {
24253 sub = vim_strnsave(s, (int)(p - s));
24254 str = vim_strnsave(*fnamep, *fnamelen);
24255 if (sub != NULL && str != NULL)
24256 {
24257 *usedlen = (int)(p + 1 - src);
24258 s = do_string_sub(str, pat, sub, flags);
24259 if (s != NULL)
24260 {
24261 *fnamep = s;
24262 *fnamelen = (int)STRLEN(s);
24263 vim_free(*bufp);
24264 *bufp = s;
24265 didit = TRUE;
24266 }
24267 }
24268 vim_free(sub);
24269 vim_free(str);
24270 }
24271 vim_free(pat);
24272 }
24273 }
24274 /* after using ":s", repeat all the modifiers */
24275 if (didit)
24276 goto repeat;
24277 }
24278 }
24279
24280 return valid;
24281}
24282
24283/*
24284 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
24285 * "flags" can be "g" to do a global substitute.
24286 * Returns an allocated string, NULL for error.
24287 */
24288 char_u *
24289do_string_sub(str, pat, sub, flags)
24290 char_u *str;
24291 char_u *pat;
24292 char_u *sub;
24293 char_u *flags;
24294{
24295 int sublen;
24296 regmatch_T regmatch;
24297 int i;
24298 int do_all;
24299 char_u *tail;
24300 garray_T ga;
24301 char_u *ret;
24302 char_u *save_cpo;
24303
24304 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
24305 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024306 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024307
24308 ga_init2(&ga, 1, 200);
24309
24310 do_all = (flags[0] == 'g');
24311
24312 regmatch.rm_ic = p_ic;
24313 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
24314 if (regmatch.regprog != NULL)
24315 {
24316 tail = str;
24317 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
24318 {
24319 /*
24320 * Get some space for a temporary buffer to do the substitution
24321 * into. It will contain:
24322 * - The text up to where the match is.
24323 * - The substituted text.
24324 * - The text after the match.
24325 */
24326 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
24327 if (ga_grow(&ga, (int)(STRLEN(tail) + sublen -
24328 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
24329 {
24330 ga_clear(&ga);
24331 break;
24332 }
24333
24334 /* copy the text up to where the match is */
24335 i = (int)(regmatch.startp[0] - tail);
24336 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
24337 /* add the substituted text */
24338 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
24339 + ga.ga_len + i, TRUE, TRUE, FALSE);
24340 ga.ga_len += i + sublen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024341 /* avoid getting stuck on a match with an empty string */
24342 if (tail == regmatch.endp[0])
24343 {
24344 if (*tail == NUL)
24345 break;
24346 *((char_u *)ga.ga_data + ga.ga_len) = *tail++;
24347 ++ga.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024348 }
24349 else
24350 {
24351 tail = regmatch.endp[0];
24352 if (*tail == NUL)
24353 break;
24354 }
24355 if (!do_all)
24356 break;
24357 }
24358
24359 if (ga.ga_data != NULL)
24360 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
24361
Bram Moolenaar473de612013-06-08 18:19:48 +020024362 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363 }
24364
24365 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
24366 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000024367 if (p_cpo == empty_option)
24368 p_cpo = save_cpo;
24369 else
24370 /* Darn, evaluating {sub} expression changed the value. */
24371 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372
24373 return ret;
24374}
24375
24376#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */